页面加载时似乎无法滚动到 div 的底部 (angularJS)
Can't seem to scroll to bottom of div on page load (angularJS)
我想在页面加载时滚动到 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> {{message.message}}
</div>
</div>
</div>
</div>
控制器
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}} {{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;
}
我想在页面加载时滚动到 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> {{message.message}}
</div>
</div>
</div>
</div>
控制器
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}} {{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;
}