最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - jQuery .scrollTop() not scrolling to li elements as expected - Stack Overflow

programmeradmin4浏览0评论

I essentially have an unordered list of list items in a div that doesn't scroll to the next element properly. 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. 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.

I have the following jsfiddle: /

The elements are:

  • A scrolling div with an id of photos-div
  • An unordered list with an id of photos-li (whoops, leaving it for now)
  • List items with incrementing ids of photo-li-X where X is a number

The code being used to scroll the div is:

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

The i variable is being incremented, as you can see.

I essentially have an unordered list of list items in a div that doesn't scroll to the next element properly. 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. 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.

I have the following jsfiddle: http://jsfiddle/YD9s5/9/

The elements are:

  • A scrolling div with an id of photos-div
  • An unordered list with an id of photos-li (whoops, leaving it for now)
  • List items with incrementing ids of photo-li-X where X is a number

The code being used to scroll the div is:

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

The i variable is being incremented, as you can see.

Share Improve this question edited Jun 12, 2013 at 15:41 taco asked Jun 12, 2013 at 15:29 tacotaco 1,38317 silver badges32 bronze badges 1
  • stackoverflow./questions/34068396/… – Abdo-Host Commented Jun 2, 2016 at 7:37
Add a ment  | 

2 Answers 2

Reset to default 6

The problem is that .offset() gets the position of an element relative to the entire document, and that position is constantly moving up. 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.

Add console.log( $('#photo-li-0').offset() ); to the top of your scrollToElement() function and you'll see what I mean. 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:

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

http://jsfiddle/mblase75/x4hQP/ (incorporates other improvements)

It appears that .offset() only measures from the top of the document. You are interested in how far the element is from the top of #photos-div. Here's a working version where I track the position in the div manually. Notice in the console that when I log the value of .offset() it's relative to the document rather than the scrolling div.

Seems that there should be a way to do this without carrying the position of that div in state. Working on it...

Here's my more programatic solution.

发布评论

评论列表(0)

  1. 暂无评论