0

I have read this post: scrolling to li element - jquery, and used the jquery code, namely this.

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. The LI has padding and margin set.

The scroll code above is not working. The scroll is being set much further than the actual item. The offset is calculated incorrectly.

Community
  • 1
  • 1
Jim
  • 14,952
  • 15
  • 80
  • 167

1 Answers1

2

You ul has multiple list items thus the variable "li" has multiple li elements. Try this instead

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

Good Luck !!

Kundan Singh Chouhan
  • 13,952
  • 4
  • 27
  • 32
  • I appreciate you trying to help. Only one of the li elements has a class of .item, so this is not really applicable. I am going to investigate Barlas' comment and see if it's not a body property. – Jim Sep 25 '12 at 16:01