gpt4 book ai didi

html - 滚动时如何根据可见的内部 div 高度调整外部 div 高度

转载 作者:行者123 更新时间:2023-11-28 05:52:36 25 4
gpt4 key购买 nike

我有以下示例 html。文件变量是一个简单的整数数组,如 [1,2,3,4,5,6]

<!doctype html>
<html ng-app="Application">
<head>
<title></title>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.5/angular.min.js"></script>
<!--<script src="../app.js"></script>
<script src="../Controller/applicationCtrl.js"></script>-->

<script>
var app = angular.module('Application', []);
var applicationCtrl = function ($scope) {
$scope.files = [1,2,3,4,5,6];
$scope.showAll = false;
};

app.controller('vm', applicationCtrl);
</script>
<style>
.J1 {
overflow-y: scroll;
height: 70px;
}
</style>
</head>
<body ng-controller="vm">
<div style="width: 100px;" ng-class="{'J1': (files.length > 3) && !showAll}">
<div ng-repeat="file in files" style="border-width: 1px; border-style: solid;">
<span>{{file}}</span>
<input type="button" value="{{'btn' + file}}"></input>
</div>
</div>
<br />
<div>
<a href ng-click="showAll = !showAll;">Show {{ showAll ? 'less':'more'}}</a>
</div>
</body>
</html>

如果文件数组的长度超过 3,那么它将溢出的 css 添加到 div。但是我也将div的高度设置为一个固定值。我想做的是根据前 3 个 div 的高度动态设置 div 的高度,而不为其设置固定值。我怎样才能做到这一点?

最佳答案

你可以试试这个。我已经添加了 jquery CDN。不过,您可以使用原始 javascript 执行此操作。

<!doctype html>
<html ng-app="Application">
<head>
<title></title>
<script src="https://code.jquery.com/jquery-2.2.3.min.js" integrity="sha256-a23g1Nt4dtEYOj7bR+vTu7+T8VP13humZFBJNIYoEJo=" crossorigin="anonymous"></script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.5/angular.min.js"></script>
<script>
var app = angular.module('Application', []);
var applicationCtrl = function ($scope) {
$scope.files = [1,2,3,4,5,6];

/* Set the parent div's height */
$(function(){
$('.J1').height($('.each-button').outerHeight() * 3);
});

};

app.controller('vm', applicationCtrl);
</script>
<style>
.J1 {
overflow-y: scroll;
}
</style>
</head>
<body ng-controller="vm">
<div style="width: 100px;" class="J1">
<!-- new class has been added -->
<div class="each-button" ng-repeat="file in files" style="border-width: 1px; border-style: solid;">
<span>{{file}}</span>
<input type="button" value="{{'btn' + file}}"></input>
</div>
</div>
<br />
<div>
<a href>Show more</a>
</div>
</body>
</html>

关于html - 滚动时如何根据可见的内部 div 高度调整外部 div 高度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37239613/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com