简体   繁体   English

使用浮动div滚动到特定的li

[英]scroll to particular li with floating div

I have read this post: scrolling to li element - jquery , and used the jquery code, namely this. 我已经阅读过这篇文章: 滚动到li元素 - jquery ,并使用了jquery代码,即这个。

var ul = $('ul.myul');
var li = $('li.item', ul)
ul.scrollTop(li.position().top);

The UL is on a div that is floated, and the UL has a set height, and overflow set to auto. UL位于浮动的div上,UL具有设置高度,溢出设置为auto。 The LI has padding and margin set. LI具有填充和边距设置。

The scroll code above is not working. 上面的滚动代码不起作用。 The scroll is being set much further than the actual item. 滚动设置比实际项目更远。 The offset is calculated incorrectly. 偏移计算不正确。

You ul has multiple list items thus the variable "li" has multiple li elements. 你有多个列表项,因此变量“li”有多个li元素。 Try this instead 试试这个

var ul = $('ul.myul');
var li = $('li.item:eq(0)', ul);
ul.scrollTop(li.position().top);

Good Luck !! 祝好运 !!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM