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

Automatically scroll to a specific part of the home (root) page from a separate url back link

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

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

现在,每次调用主页时都会触发滚动效果 (myexample.com)。我实际上希望此脚本片段触发,并且仅在主页的 url 字符串明确显示为:myexample.com#pizza3

时触发

如何修改基本代码来实现?

干杯!

这应该会捕获您的 URL 的最后 7 位数字并检查它们是否有披萨标签:

let url = window.location.href;
let tagStr = url.substr(url.length - 7);
if (tagStr == '#pizza3') {

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