简体   繁体   中英

scroll to has on click and on page load

Hi i currently have a load of anchor links to different content on my homepage, when the user click the link it scrolls down nicely. (see code below what i am using)

Now what i would like to happen is someone can link to that content from another page lets say /#hello

currently when someone goes to /#hello (not clicking on the link) it does the default anchor state which is ok but i really need that offset in the code to happen on page loaded # link

if anyone knows a simple piece of JQuery to make this happen or can modify my code (or give me pointers) to make it happen on load too that would help me alot.

thanks

**// Scroll to
$(document).ready(function(){
    $(".scroll").bind('click.smoothscroll',function (e) {
        e.preventDefault();

        var target = this.hash,
        $target = $(target);

        $('html, body').stop().animate({
            'scrollTop': $target.offset().top-70
        }, 900, 'swing', function () {
            window.location.hash = target;
        });
    });
});**

on every page load you could check it like this :

if(location.hash){
   $('html, body').stop().animate({
        'scrollTop': $(location.hash).offset().top-120
    }, 900 );
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM