我的ul定义为
<div>
<ul id="mylist">
</ul>
</div>
在我的javascript中,我使用
将j项添加到此ul$("mylist").append("<li>Test</li>");
我希望我的ul能够自动向下滚动div,当我添加了很多li项目并且ul超过为其定义的高度时
我尝试过的事情:
答案 0 :(得分:12)
点击此JS fiddle。它会对你有所帮助。
Jquery的:
$('.add').click(function(){
$("#mylist").append("<li>Test</li>");
$('#mylist').animate({scrollTop: $('#mylist').prop("scrollHeight")}, 500);
});
HTML:
<div>
<ul id="mylist"></ul>
<button class="add">Add li</button>
</div>
的CSS:
#mylist {
width: 100px;
height: 200px;
padding: 20px;
background-color: #eeeeee;
overflow-y: auto;
}
答案 1 :(得分:4)
我使用了jQuery animate
来实现平滑滚动。我只是向ul
添加了一些元素,然后滚动到li
下的最后一个ul
元素。
<强> 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代码:
<div>
<ul id="mylist"></ul>
</div>
现场演示@ JSFiddle:http://jsfiddle.net/dreamweiver/0Lofm4mL/1/
答案 2 :(得分:4)
这也可以在Vanilla JS中完成,不需要JQuery:
items = document.querySelectorAll(".dummy");
last = items[items.length-1];
last.scrollIntoView();
另见下面的代码段:
//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();
}
&#13;
#mylist {
max-height: 200px;
overflow-y: auto;
}
.dummy {
min-height: 80px;
background-color: lightgrey;
}
&#13;
<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>
&#13;
答案 3 :(得分:3)
尝试类似
的内容
$('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>
答案 4 :(得分:0)
以下是一个更简单的答案:
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>
答案 5 :(得分:0)
有一种简单的方法可以做到这一点:
container = $('MyElement').get(0);
container.scrollTop = (container.scrollHeight + container.offsetHeight);