简体   繁体   English

如何向下滚动到动态添加的ul中的最后一个li项目?

[英]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: 我尝试过的事情:

  1. In CSS I set a max-height and overflow-y:scroll for the ul 在CSS中我设置了一个max-height和overflow-y:滚动ul
  2. In Javascript I did $("mylist li").last().focus(); 在Javascript中我做了$(“mylist li”)。last()。focus();

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.

相关问题 如何使用 jquery 在 ul 中删除动态添加的 li - how can I remove dynamically added li in ul using jquery 在附加之前,如何检查最后一个动态创建的li元素是否填充在ul中 - How do I check if the last dynamically created li element is filled in a ul before appending 使用箭头键向上和向下滚动<ul><li> - Scroll up and down with arrow key on <ul> <li> 我如何删除 <li> 上 <ul> 我是通过jQuery添加的? - How I can remove a <li> on <ul> which I added via jQuery? 如何滚动到动态添加的最后一个 div? - How to scroll to last div which is dynamically added? 我如何找到最后一个 <li></li> 在每一个 <ul></ul> (前端,html,css,jquery) - How do i find the last <li></li> in every <ul></ul> (Front-end, html, css , jquery) 如何删除最后一个文本 <li> 每一个 <ul> ? - How can I remove all text after the last <li> of every <ul>? 我怎样才能访问任何<ul>然后任何<li>它使用 puppeteer 的项目?</li></ul> - how can I access any <ul> and then any <li> item of its using puppeteer? 我如何滚动到Qooxdoo虚拟专家的最后一项 - How can i scroll to last item of Qooxdoo virtualist 5列表时如何触发事件<li>项目已添加到我动态生成的<ul>列表?</ul></li> - How to trigger event when 5 list <li> items have been added to my dynamically generated <ul> list?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM