我如何使用JavaScript滚动到一个元素? [英] How do I scroll to an element using JavaScript?

查看:68
本文介绍了我如何使用JavaScript滚动到一个元素?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试将网页移至< div> 元素。

I am trying to move the page to a <div> element.

下一个代码无效:

document.getElementById("divFirst").style.visibility = 'visible';
document.getElementById("divFirst").style.display = 'block';


推荐答案

您可以使用锚来聚焦div 。 I.e:

You can use an anchor to "focus" the div. I.e:

<div id="myDiv"></div>

然后使用以下javascript:

and then use the following javascript:

// the next line is required to work around a bug in WebKit (Chrome / Safari)
location.href = "#";
location.href = "#myDiv";

这篇关于我如何使用JavaScript滚动到一个元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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