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?
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.
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.
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.
We can use the window. scrollTo method to scroll to the bottom of the page. window. scrollTo(0, document.
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?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With