Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JQuery smooth scroll to id from URL

Tags:

jquery

scroll

Let's say you have the following URL, (www.domain.com/page.php#_number).

I want to extract the id from the url**(_1)** and scroll to it on page load.

Your help will be appreciated.

Thanks for your help all.

The answer:-

$(document).ready(function() { 
    var $root = $('html, body');
    var ancloc = window.location.hash;
    event.preventDefault();
        $root.animate({
            scrollTop: $(ancloc).offset().top
        }, 500, function () {
            window.location.hash = href;
        });
        return false;
});
like image 875
user1852196 Avatar asked Jan 20 '26 09:01

user1852196


1 Answers

Why not just use an anchor <a href="http://www.domain.com/page.php#number"> and animate that. Check this link: Smooth scrolling when clicking an anchor link

like image 180
Casey Dwayne Avatar answered Jan 21 '26 21:01

Casey Dwayne



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!