gpt4 book ai didi

javascript - 似乎无法在页面加载时滚动到 div 的底部(angularJS)

转载 作者:行者123 更新时间:2023-11-30 13:58:04 25 4
gpt4 key购买 nike

我想在页面加载时滚动到 div 的底部,但它没有这样做。页面加载后我就可以滚动了。

CSS(隐藏滚动条)

.scrollable {
position: absolute;
width: 100%;
height: 100%;
top: 0;
bottom: 0;
left: 0;
right: 0;
}
.scrollable-hidden-parent {
width: 100%;
height: 100%;
overflow: hidden;
}

.scrollable-hidden-child{
width: 100%;
height: 100%;
overflow-y: scroll;
padding-right: 17px;
box-sizing: content-box;
}

HTML - 使用 init 函数尝试在加载时滚动到底部

<div class="scrollable" ng-controller="MatchDetailController as matchdetail"
ng-init="init()">
<div class="scrollable-hidden-parent" >
<div class="scrollable-hidden-child" id="scrollable-hidden">
<div ng-repeat="message in chats>
<b>{{message.name}}</b>&nbsp;&nbsp;{{message.message}}
</div>
</div>
</div>
</div>

Controller

app.controller('MatchDetailController', ['$scope', '$http', '$location', '$rootScope', '$window', function MatchDetailController($scope,$http,$location,$rootScope, $window) {

$scope.init = function() {
var objDiv = document.getElementById("scrollable-hidden");
objDiv.scrollTop = objDiv.scrollHeight;
};

}]);

最佳答案

在上面的帮助下,这就是我最终做的:

指令

//automatically scrolls to the bottom of the list on load
app.directive('scrollToBottom', function($timeout) {
return {
link: function(scope, element, attr) {
if (scope.$last === true) {
$timeout(function() {
var scroll_id = attr.scrollId;
var objDiv = document.getElementById(scroll_id);
objDiv.scrollTop = objDiv.scrollHeight;
}, 0);
}
}
};
});

HTML

<div class="scrollable-hidden-parent">
<div class="scrollable-hidden-child" id="scrollable-hidden">
<div ng-repeat="message in chats" scroll-to-bottom scroll-id="scrollable-hidden">
<div>{{message.name}}&nbsp;&nbsp;{{message.message}}</div>
</div>
</div>
</div>

CSS

.scrollable-hidden-parent {
width: 100%;
height: 100%;
overflow: hidden;
}

.scrollable-hidden-child{
width: 100%;
height: 100%;
overflow-y: scroll;
padding-right: 17px;
box-sizing: content-box;
}

关于javascript - 似乎无法在页面加载时滚动到 div 的底部(angularJS),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56827025/

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