以编程方式滚动到一个锚定标记 [英] Programmatically scroll to an Anchor Tag
本文介绍了以编程方式滚动到一个锚定标记的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
考虑以下code:
<a href="#label2">GoTo Label2</a>
... [content here] ...
<a name="label0"></a>More content
<a name="label1"></a>More content
<a name="label2"></a>More content
<a name="label3"></a>More content
<a name="label4"></a>More content
有没有一种方法来模拟点击转到Label2的链接至code滚动到相应的区域在网页上?
Is there a way to emulate clicking on the "GoTo Label2" link to scroll to the appropriate region on the page through code?
修改:可接受的替代方法是滚动到一个唯一ID,已经存在我的页面上的元素。我会添加锚标记,如果这是一个可行的解决方案。
EDIT: An acceptable alternative would be to scroll to an element with a unique-id, which already exists on my page. I would be adding the anchor tags if this is a viable solution.
推荐答案
本JS普遍运作良好,对我,如果你也把一个ID的元素:
This JS has generally worked well for me if you also put an ID on the element:
document.getElementById('MyID').scrollIntoView(true);
这是很好的,因为它也将使滚动的div等这样内容是可见的。
This is good as it will also position scrollable divs etc so that the content is visible.
麦克。
这篇关于以编程方式滚动到一个锚定标记的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文