【问题标题】:Need javascript to auto-scroll to the target html element after page loads页面加载后需要 javascript 自动滚动到目标 html 元素
【发布时间】:2011-02-16 06:40:34
【问题描述】:

我是后端开发人员,不熟悉 javascript。任何人都可以提供几行脚本来允许页面在页面加载后自动滚动到“目标”元素

<html>
<bod>

<p id="target">...</p> // auto-scroll here

</bod>
</html>

谢谢

【问题讨论】:

  • 能否请您标记一个正确的答案。

标签: javascript


【解决方案1】:

您可以在 window.onload 事件中的元素上使用 scrollIntoView..

在你的情况下,你会这样做:

window.onload = function() {
    var el = document.getElementById('target');
    el.scrollIntoView(true);
}

可以在这里找到好的文档: MDN scrollIntoView

【讨论】:

    【解决方案2】:

    还将你的身体标签更改为类似

    <body onload="ScrollToTarget">
    

    那么你的函数可以在标题中定义为

    function ScrollToTarget()
    {
         document.getElementById("target").scrollIntoView(true);
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-03-04
      • 1970-01-01
      • 1970-01-01
      • 2020-06-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-04-01
      相关资源
      最近更新 更多