页面加载后需要javascript自动滚动到目标html元素 [英] Need javascript to auto-scroll to the target html element after page loads

查看:47
本文介绍了页面加载后需要javascript自动滚动到目标html元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是后端开发人员,是javascript的新手。任何人都可以提供几行脚本,允许页面在页面加载后自动滚动到target元素

I'm backend developer, new to javascript. Can anyone provide a few lines of script that will allow the page to auto-scroll to the "target" element after the page loads

<html>
<bod>

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

</bod>
</html>

谢谢

推荐答案

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

You can use scrollIntoView on the element in window.onload event..

在你的情况下你会做:

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

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

Good docs can be found here: MDN scrollIntoView

这篇关于页面加载后需要javascript自动滚动到目标html元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆