dynamic dynamic - 1 year ago 109
jQuery Question

jquery smooth scroll to an anchor?

is there any way to scroll down to an anchor link with jquery?




Answer Source

Here is how I do it:

    var hashTagActive = "";
    $(".scroll").on("click touchstart" , function (event) {
        if(hashTagActive != this.hash) { //this will prevent if the user click several times the same link to freeze the scroll.
            //calculate destination place
            var dest = 0;
            if ($(this.hash).offset().top > $(document).height() - $(window).height()) {
                dest = $(document).height() - $(window).height();
            } else {
                dest = $(this.hash).offset().top;
            //go to destination
                scrollTop: dest
            }, 2000, 'swing');
            hashTagActive = this.hash;

Then you just need to create your anchor like this:

<a class="scroll" href="#destination1">Destination 1</a>

You can see it on my website.
A demo is also available here: http://jsfiddle.net/YtJcL/

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download