Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jquery: Scroll to last <li>

I'm using the following to scroll to the last <li> in an unordered list:

$("html,body").animate({scrollTop: $('ul#cart-items li').offset().top});

How can I change this to scroll to the last <li> in the unordered list, but also offset it about 30px from the top?

like image 380
floatleft Avatar asked Nov 30 '11 23:11

floatleft


People also ask

How to scroll to bottom in jQuery?

To auto scroll a page from top to bottom we can use scrollTop() and height() method in jquery. In this method pass the document's height in scrollTop method to scroll.

How to scroll bottom to top in jQuery?

Answer: Use the scrollTop Property You can use the jQuery animate() method in combination with the scrollTop property to scroll to the top of the web page smoothly with an animation.

What is $( window scrollTop ()?

The scrollTop() method sets or returns the vertical scrollbar position for the selected elements. Tip: When the scrollbar is on the top, the position is 0. When used to return the position: This method returns the vertical position of the scrollbar for the FIRST matched element.

How to scroll to bottom of the page in js?

We can use the window. scrollTo method to scroll to the bottom of the page. window. scrollTo(0, document.


1 Answers

Have you tried this:

$("html,body").animate({scrollTop: $('ul#cart-items li:last').offset().top - 30});

but + or - the 30 at the end to achieve your desired offset?

like image 162
Kai Qing Avatar answered Sep 18 '22 13:09

Kai Qing