如何将HTML页面滚动到给定的锚点? [英] How to scroll HTML page to given anchor?

查看:101
本文介绍了如何将HTML页面滚动到给定的锚点?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我已经指定了一个

我想让浏览器将页面滚动到给定的锚点,在我的HTML代码中使用 id 属性:

 < a name =anchorName> ..< / a> 

 < h1 id =anchorName2> ..< / h1> 

我想通过导航到<$ c $获得相同的效果C> http://server.com/path#anchorName 。页面应该滚动,以便锚点靠近页面可见部分的顶部。 code> function scrollTo(hash){
location.hash =#+ hash;



$ b

完全不需要jQuery!

I’d like to make the browser to scroll the page to a given anchor, just by using JavaScript.

I have specified a name or id attribute in my HTML code:

 <a name="anchorName">..</a>

or

 <h1 id="anchorName2">..</h1>

I’d like to get the same effect as you’d get by navigating to http://server.com/path#anchorName. The page should be scrolled so that the anchor is near the top of the visible part of the page.

解决方案

function scrollTo(hash) {
    location.hash = "#" + hash;
}

No jQuery required at all!

这篇关于如何将HTML页面滚动到给定的锚点?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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