[英]To Reset scroll position to newly added element
在容器中,元素将被添加并动态排序。 在添加按钮的按钮上,我将向列表添加一个新元素。 我想将滚动位置设置为最近添加的元素。 请帮助我。
<div ng-repeat="items in listItems | orderby" style="height:500px;overflow-y:auto"> <p id="scrollPosition{{items}}"> {{items}}</p> </div>
当您添加新元素时,只需为其指定一个唯一的ID,然后只需使用此javascript
location.href = "#";
location.href = "#myDiv";
您可以将jquery动画功能与$("div#YourContentDiv p:last-child").offset()
在Plunkr中也有一个角度动画示例:
http://plnkr.co/edit/yz1EHB8ad3C59N6PzdCD?p=preview :
$("body, html").animate({ scrollTop: $("div#YourContentDiv p:last-child").offset().top }, 600);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.3/jquery.min.js"></script> <div id="YourContentDiv"> <p>new text</p> <p>new text</p> <p>new text</p> <p>new text</p> <p>new text</p> <p>new text</p> <p>new text</p> <p>new text</p> <p>new text</p> <p>new text</p> <p>new text</p> <p>new text</p> <p>new text</p> <p>new text</p> <p>new text</p> <p style="color:red">new line</p> </div>
<div ng-repeat="items in listItems | orderby" style="height:500px;overflow-y:auto">
<p id="scrollPosition{{items}}"> {{items}}</p>
//控制器
$scope.add = function(item){ $location.hash('scrollPosition'+item); $anchorScroll(); }
上面的Angular锚滚动解决了我的问题。
这肯定会解决您的问题。 我已经为自己创建了这种方法。
请注意,这是根据数据表滚动进行的。 您需要根据您的代码更新“ .dataTables_scrollBody”之类的类。
var scrollPosition =0;
function saveScrollPosition ( container ) {
if ( $( container ) != undefined && $( container ).find( '.dataTables_scrollBody' ) != undefined ) {
scrollPosition = $( container ).find( '.dataTables_scrollBody' ).scrollTop();
} else {
scrollPosition = 0;
}
}
function setScrollPosition ( container ) {
if ( $( container ) != undefined && $( container ).find( '.dataTables_scrollBody' ) != undefined ) {
$( container ).find( '.dataTables_scrollBody' ).scrollTop( scrollPosition );
} else {
scrollPosition = 0;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.