[英]How can I scroll down to the last li item in a dynamically added ul?
I have a ul defined like 我有一个ul定义的喜欢
<div>
<ul id="mylist">
</ul>
</div>
In my javascript I add li items to this ul with jquery using 在我的javascript中,我使用jquery将li项添加到此ul
$("mylist").append("<li>Test</li>");
I want my ul to automatically scroll down the div to when I have added a lot of li items and ul surpass the height defined for it 我希望我的ul自动向下滚动div,当我添加了很多li项目并且ul超过为它定义的高度时
Things I have tried: 我尝试过的事情:
Click this JS fiddle . 点击这个JS小提琴 。 It will help you. 它会对你有所帮助。
Jquery: jQuery的:
$('.add').click(function(){
$("#mylist").append("<li>Test</li>");
$('#mylist').animate({scrollTop: $('#mylist').prop("scrollHeight")}, 500);
});
Html: HTML:
<div>
<ul id="mylist"></ul>
<button class="add">Add li</button>
</div>
Css: CSS:
#mylist {
width: 100px;
height: 200px;
padding: 20px;
background-color: #eeeeee;
overflow-y: auto;
}
I have used jQuery animate
to function smooth scrolling. 我使用jQuery animate
来实现平滑滚动。 Im just adding few elements to the ul
and then scrolling to the last li
element under ul
. 我只是在ul
添加了一些元素,然后滚动到ul
下的最后一个li
元素。
JSCode: JSCode:
$(function () {
$("#mylist").append("<li>Test</li><li>Test</li><li>Test</li><li>Test</li><li>Test</li>");
$('div').animate({
scrollTop: $("#mylist li").last().offset().top
},
'slow');
});
HTML Code: HTML代码:
<div>
<ul id="mylist"></ul>
</div>
Live Demo @ JSFiddle : http://jsfiddle.net/dreamweiver/0Lofm4mL/1/ 现场演示@ JSFiddle: http : //jsfiddle.net/dreamweiver/0Lofm4mL/1/
This can be done also in Vanilla JS no need for JQuery: 这也可以在Vanilla JS中完成,不需要JQuery:
items = document.querySelectorAll(".dummy");
last = items[items.length-1];
last.scrollIntoView();
See also snippet below: 另见下面的代码段:
//this adds the new element at the bottom button = document.querySelector("button") button.onclick = function() { list = document.querySelector("#mylist"); var li = document.createElement("li"); li.textContent = "Added with Vanilla JS" list.appendChild(li); //this jumps to the last element on the bottom items = document.querySelectorAll(".dummy"); console.log(items); last = items[items.length-1]; last.scrollIntoView(); }
#mylist { max-height: 200px; overflow-y: auto; } .dummy { min-height: 80px; background-color: lightgrey; }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> <button>Add</button> <div> <ul id="mylist"> <li class="dummy">Test</li> <li class="dummy">Test</li> <li class="dummy">Test</li> <li class="dummy">Test</li> <li class="dummy">Test</li> </ul> </div>
Try something like 尝试类似的东西
$('button').click(function() { var $last = $("<li>Test</li>").appendTo("#mylist"); $("#mylist").stop().animate({ scrollTop: $last.offset().top }, '500', 'swing', function() {}); })
#mylist { max-height: 200px; overflow-y: auto; } .dummy { min-height: 80px; background-color: lightgrey; }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> <button>Add</button> <div> <ul id="mylist"> <li class="dummy">Test</li> <li class="dummy">Test</li> <li class="dummy">Test</li> <li class="dummy">Test</li> <li class="dummy">Test</li> </ul> </div>
A much simpler answer is the following: 一个更简单的答案如下:
function setItem(){ // We go through all items and change the value of the id attribute to empty $('#mylist li').each(function (i, v) { $(v).attr('id', ''); }); // We add the items $('#mylist').append('<li id="last">test</li>'); // Currently there is only one identifier called "last" window.location.href = '#last'; }
#mylist{ overflow-y: scroll; height: 100px; }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <button onclick="setItem();">Add</button> <div id="mylist"></div>
There is one simple way to do this: 有一种简单的方法可以做到这一点:
container = $('MyElement').get(0);
container.scrollTop = (container.scrollHeight + container.offsetHeight);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.