[英]how to scroll to bottom or move focus to bottom in angular?
I have a div in which I added element .I want to scroll to bottom of the div whenever element added in div.I know how to do in in jquery .But I did not know how to scroll using angular js 我在其中添加了元素的div。我想在div中添加元素时滚动到div的底部。我知道如何在jquery中进行操作。但是我不知道如何使用angular js滚动
testCaseContainer is id of div . testCaseContainer是div的ID。
var elem = document.getElementById('testCaseContainer'); // just to
// scroll down
// the line
elem.scrollTop = elem.scrollHeight;
I want to know how it is possible using angular ? 我想知道使用angular怎么可能?
http://plnkr.co/edit/M8tCL8Szc3h3FatFLycG?p=preview http://plnkr.co/edit/M8tCL8Szc3h3FatFLycG?p=preview
I added css in div container so that it scroll . 我在div容器中添加了CSS,以便它滚动。
.heightClass{
height:100px;
overflow :auto;
}
how to move focus below when item is added ..please post your answer.. 添加项目时如何将焦点移到下面..请发表您的答案..
Here is your solution. 这是您的解决方案。 It works for me . 这个对我有用 。
Template 模板
<div data-ng-controller="TestController">
<button data-ng-click="AddNewItem()">Add New Item</button>
<div class="heightClass" data-dr-scroll-to-bottom>
<div data-ng-repeat="divItem in divList track by $index">
{{divItem}}
</div>
</div>
</div>
Controller 调节器
app.controller("TestController", ["$scope", function ($scope) {
$scope.divList = [1, 2];
$scope.AddNewItem = function () {
$scope.divList.push(3);
};
}]);
Directive You have two options. 指令您有两个选择。
listen to DOMNodeInserted(This is deprecated. so don't you use it. learn more about it Listening to events such as adding new elements in JavaScript . For sake for convenience I have added the code . But don't use this code. Use Option 2). 侦听DOMNodeInserted(不建议使用。因此,您不要使用它。了解更多信息。 侦听诸如在JavaScript中添加新元素之类的事件 。为方便起见,我添加了代码。但是请不要使用此代码。选项2)。
app.directive("drScrollToBottom", function() { app.directive(“ drScrollToBottom”,function(){
var linkFunction = function(scope, iElement, iAttrs) { iElement.bind("DOMNodeInserted", function() { console.log(iElement.prop("scrollHeight")); iElement.scrollTop(iElement.prop("scrollHeight")); }); var linkFunction = function(scope,iElement,iAttrs){iElement.bind(“ DOMNodeInserted”,function(){console.log(iElement.prop(“ scrollHeight”)); iElement.scrollTop(iElement.prop(“ scrollHeight”) );}); }; };
return { restrict: "A", link: linkFunction } }); 返回{限制:“ A”,链接:linkFunction}});
use arrive.js (can be found here https://github.com/uzairfarooq/arrive/ ) 使用到达.js (可以在这里找到https://github.com/uzairfarooq/arrive/ )
app.directive("drScrollToBottom", function () { app.directive(“ drScrollToBottom”,函数(){
var linkFunction = function (scope, iElement, iAttrs) { iElement.arrive("div", function () { console.log(iElement.prop("scrollHeight")); iElement.scrollTop(iElement.prop("scrollHeight")); }); }; return { restrict: "A", link: linkFunction }
}); });
below is the directive to scroll to bottom of page 以下是滚动到页面底部的指令
csapp.directive('csScrollTop', ["$window", function ($window) {
var linkFunction = function (scope) {
scope.$on("$locationChangeSuccess", function () {
$window.scrollTo(0, document.body.scrollHeight);
//$("html, body").animate({ scrollTop: 0 }, "fast");
});
};
return {
restrict: 'E',
link: linkFunction
};
}]);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.