[英]How to force the page to always scroll to bottom?
I am using angular js to build a chat app. 我正在使用angular js构建聊天应用程序。 The problem is when the page loads or a new chat comes in, it doesn't properly scroll to the bottom of the page.
问题是当页面加载或出现新的聊天时,它无法正确滚动到页面底部。 This is the code that handles the scroll to bottom of page
这是处理滚动到页面底部的代码
var app = angular.module('chatApp', ['ui.bootstrap','mwl.confirm']);
app.directive('myRepeatDirective', function() {
return function(scope, element, attrs) {
if (scope.$last) {
document.getElementById("main-container").scrollTo(0, document.getElementById("main-container").scrollHeight);
}
};
})
Here #main-container
is the entire chat box in which chat happens. 这里的
#main-container
是其中发生聊天的整个聊天框。 Now I found some solutions on the web and this one seems to kind of work. 现在,我在网上找到了一些解决方案,而这似乎是一种工作。
var app = angular.module('chatApp', ['ui.bootstrap','mwl.confirm']);
app.directive('myRepeatDirective', function() {
return function(scope, element, attrs) {
if (scope.$last) {
var objDiv = document.getElementById("main-container");
objDiv.scrollTop = objDiv.scrollHeight;
}
};
})
But the problem is some chat may have a link which opens up in the card. 但是问题是某些聊天可能在卡中打开了一个链接。 So after implementing this solution, when I refresh the page, it does scrolls to the bottom but as soon as it encounters a card with html link which opens up, it stops scrolling down and stops there.
因此,在实施此解决方案之后,当我刷新页面时,它确实会滚动到底部,但是一旦遇到带有打开的html链接的卡片,它就会停止向下滚动并停止在该位置。
Is there a way to force the page to scroll to bottom or maybe a timer which would scroll to bottom every other second. 有没有一种方法可以迫使页面滚动到底部,或者可能是计时器每隔一秒钟滚动到底部。 (Not sure if this is a good solution)?
(不确定这是否是一个好的解决方案)?
If you're already binding to a chat message collection inside your template, you can simply craft a directive which watches for changes to that collection in order to immediately perform scroll adjustments. 如果您已经绑定到模板中的聊天消息集合,则可以简单地编写一条指令来监视该集合的更改,以便立即执行滚动调整。 This way, you avoid having an arbitrary interval between scroll adjustments.
这样,您可以避免滚动调整之间有任意间隔。
Here's a distilled example of how you might go about this: 这是您可能如何做的一个简要示例:
angular .module('app', []) .controller('ctrl', function ($scope, $interval) { $scope.messages = []; // Simulate new chat messages being appended $interval(() => $scope.messages.push(Date.now()), 1000); }) .directive('autoScrollBottom', function () { return function (scope, el, attrs) { scope.$watchCollection(attrs.autoScrollBottom, function () { el[0].scrollTop = el[0].scrollHeight; }); }; });
.chat { height: 100px; overflow-y: auto; }
<div ng-app="app" ng-controller="ctrl"> <div class="chat" auto-scroll-bottom="messages"> <div ng-repeat="m in messages">{{m}}</div> </div> </div> <script src="https://unpkg.com/angular@1.7.8/angular.min.js"></script>
You can create a directive for this: 您可以为此创建一个指令:
.directive('scrollDown', function () {
return {
scope: {
scrollDown: "="
},
link: function (scope, element) {
scope.$watchCollection('scrollDown', function (newValue) {
if (newValue)
{
$(element).scrollTop($(element)[0].scrollHeight);
}
});
}
}
})
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.