简体   繁体   English

平滑滚动到页面上的div ID通过URL地址加载

[英]smooth scroll to div ID on page Load via URL address

I know how to create a smooth scroll to anchor on page load; 我知道如何创建平滑滚动来锚定页面加载; with something similar to the below: 类似于以下内容:

$(function(){
    $('html, body').animate({
        scrollTop: $( $('#anchor1').attr('href') ).offset().top
    }, 2000);
    return false;
});

And I have done it 17745005 times onClick as navigation to different areas of a vertical website -- 我已经完成了17745005次onClick导航到垂直网站的不同区域-

..But, what I am trying to do now IS, when a URL is accessed let's say http://www.cooltimes.com/#scrolltoCOOLplace <-- specifically that is typed into the address bar OR clicked on from an external link, then it smooth scrolls to #scrolltoCOOLplace div location within my website. ..但是,我现在要做的是,当访问URL时,我们说 http://www.cooltimes.com/#scrolltoCOOLplace <- 特别是在地址栏中输入的内容或从外部链接中单击的内容,然后将其平滑滚动到我网站内的#scrolltoCOOLplace div位置。

Essentially triggering smooth scroll to div element via URL address. 本质上触发通过URL地址平滑滚动到div元素。

Any suggestions on jQuery solutions that allow this kind of functionality? 关于允许这种功能的jQuery解决方案有什么建议吗?

Use var param = document.location.pathname; 使用var param = document.location.pathname; in your function then pass it to scrollTop. 在您的函数中,然后将其传递给scrollTop。

$(function(){
var param = document.location.pathname;
    $('html, body').animate({
        scrollTop: $(param.substr(1)).offset().top
    }, 2000);
    return false;
});

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

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