简体   繁体   English

滚动到点击和页面加载

[英]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 现在我想发生的事情是有人可以从另一个页面链接到该内容,说/#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 当前,当有人去/#hello(不单击链接)时,它会执行默认的锚点状态,这是可以的,但是我真的需要代码中的偏移发生在加载的页面上#链接

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. 如果有人知道一个简单的JQuery片段可以做到这一点,或者可以修改我的代码(或给我指针)来使其在加载时发生,那也对我有很大帮助。

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 );
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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