简体   繁体   English

jQuery .scrollTop()不按预期滚动到li元素

[英]jQuery .scrollTop() not scrolling to li elements as expected

I essentially have an unordered list of list items in a div that doesn't scroll to the next element properly. 我本质上在div中有一个无序的列表项列表,不能正确滚动到下一个元素。 I'm just trying to have a button to scroll up and down the list. 我只是想要一个按钮来向上和向下滚动列表。

While the code does scroll, it doesn't match up with the li elements. 虽然代码会滚动,但它与li元素不匹配。 After every click, it scrolls up a little bit, then down on the next click. 每次单击后,它会向上滚动一点,然后在下一次单击时向下滚动。 I've tried walking the DOM and verifying the element it is scrolling to is an li element, but didn't see the issue. 我试过走DOM并验证它滚动到的元素是一个li元素,但没有看到问题。

I have the following jsfiddle : http://jsfiddle.net/YD9s5/9/ 我有以下jsfiddlehttp//jsfiddle.net/YD9s5/9/

The elements are: 要素是:

  • A scrolling div with an id of photos-div 一个带有id photos-div滚动photos-div
  • An unordered list with an id of photos-li (whoops, leaving it for now) 一个无序列表,其id为photos-li (哎呀,暂时离开)
  • List items with incrementing ids of photo-li-X where X is a number 列出具有photo-li-X递增ID的项目,其中X是数字

The code being used to scroll the div is: 用于滚动div的代码是:

        $('#photos-div').scrollTop($('#photo-li-' + i).offset().top);

The i variable is being incremented, as you can see. 正如您所见, i变量正在递增。

The problem is that .offset() gets the position of an element relative to the entire document , and that position is constantly moving up. 问题是.offset()获取元素相对于整个文档的位置,并且该位置不断向上移动。 So once you've scrolled to item 1, it returns the .offset().top that item 1 currently has in the document, which is now where item 0 used to be. 所以,一旦你滚动到第1项,它返回.offset().top该项目目前 1有文件,这是现在所在的项目0 曾经是。

Add console.log( $('#photo-li-0').offset() ); 添加console.log( $('#photo-li-0').offset() ); to the top of your scrollToElement() function and you'll see what I mean. scrollToElement()函数的顶部,你会看到我的意思。 Notice that the top offset keeps decreasing, quickly moving into the negative numbers as it moves off the top of the document. 请注意,顶部偏移量不断减小,当它从文档顶部移开时会快速移动到负数。

The fix is to take the difference between the offset of $('#photo-li-'+i) and $('#photo-li-0') (or the container $('#photos-li') itself) and scroll to that instead: 修复是取$('#photo-li-'+i)$('#photo-li-0') (或容器$('#photos-li')本身)的偏移量之间的差异然后滚动到那个:

var newpos = $('#photo-li-' + i).offset().top - $('#photo-li-0').offset().top;
$('#photos-div').scrollTop(newpos);

http://jsfiddle.net/mblase75/x4hQP/ (incorporates other improvements) http://jsfiddle.net/mblase75/x4hQP/ (包含其他改进)

It appears that .offset() only measures from the top of the document. 似乎.offset()仅从文档顶部开始测量。 You are interested in how far the element is from the top of #photos-div . 您感兴趣的是元素距离#photos-div的顶部有多远。 Here 'sa working version where I track the position in the div manually. 是一个工作版本,我手动跟踪div中的位置。 Notice in the console that when I log the value of .offset() it's relative to the document rather than the scrolling div. 请注意,在控制台中,当我记录.offset()的值时,它相对于文档而不是滚动div。

Seems that there should be a way to do this without carrying the position of that div in state. 似乎应该有一种方法来做到这一点,而不是在状态中携带该div的位置。 Working on it... 正在努力......

Here's my more programatic solution . 这是我更加编程的解决方案

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

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