如何将溢出的div滚动到某个#标签(锚点)? [英] How do I scroll an overflowed div to a certain hashtag (anchor)?

查看:483
本文介绍了如何将溢出的div滚动到某个#标签(锚点)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的网页上,我有一个溢出的div(即垂直滚动条)。在div里面,我有ids的锚点。当我在URL(mypage.html#id)中放入其中一个ID时,我希望div而不是页面滚动到该锚点。

On my webpage I have an overflowed div (i.e. with the vertical scrollbar). Inside the div, I have anchors with ids. When I put one of these ids in the URL (mypage.html#id), I want the div, not the page, to scroll to that anchor.

如何做我这样做,最好用简单的JavaScript?如果它太复杂了,我将使用jQuery,但我不会在这个项目中使用它来做任何其他事情。

How do I do that, preferably with plain JavaScript? If it's too complex, I'll go with jQuery, but I'm not using it in this project for anything else.

推荐答案

$('.overflow').scrollTop($('#anchor').offset().top);

没有理由你不能将它转换为标准的javascript。

There is no reason at all you can't convert this to standard javascript.

请注意,如果锚元素上有边距,则滚动将关闭。

Note that the scroll will be off if there is a margin on the anchor element.

这篇关于如何将溢出的div滚动到某个#标签(锚点)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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