从单独的url返回链接自动滚动到主页(根)页面的特定部分

问题描述 投票:0回答:1

我的网站主页(myexample.com)上有此基本代码:

setTimeout(function(){
  document.getElementById("pizza3").scrollIntoView( {behavior: 'smooth'} );
}, 2000);

现在,滚动效果将在每次调用主页(myexample.com)时触发。实际上,当主页的url字符串显式读取为<< myexample.com#pizza3

时,我实际上希望触发此脚本片段,并且仅触发该脚本片段如何修改基本代码以实现?

干杯!

javascript getelementbyid js-scrollintoview
1个回答
0
投票
这应该捕获URL的最后7位数字,并检查它们的Pizza标签:

let url = window.location.href; let tagStr = url.substr(url.length - 7); if (tagStr == '#pizza3') { setTimeout(function(){ document.getElementById("pizza3").scrollIntoView( {behavior: 'smooth'} ); }, 2000); }

© www.soinside.com 2019 - 2024. All rights reserved.