我的网站主页(myexample.com)上有此基本代码:
setTimeout(function(){
document.getElementById("pizza3").scrollIntoView( {behavior: 'smooth'} );
}, 2000);
现在,滚动效果将在每次调用主页(myexample.com)时触发。实际上,当主页的url字符串显式读取为<< myexample.com#pizza3
时,我实际上希望触发此脚本片段,并且仅触发该脚本片段如何修改基本代码以实现?干杯!
let url = window.location.href;
let tagStr = url.substr(url.length - 7);
if (tagStr == '#pizza3') {
setTimeout(function(){
document.getElementById("pizza3").scrollIntoView( {behavior: 'smooth'} );
}, 2000);
}