HTML:创建一个链接到位于页面中间居中的锚点
我的 html 页面上有一个锚链接。单击链接时,它会导致页面滚动到锚点,以便锚点位于页面可见部分的最顶部。如何使页面滚动以使锚点位于页面中间?
I have a link to an anchor on my html page. When the link is clicked it causes the page to scroll to the anchor so that the anchor is at the very top of the visible part of the page. How can I make the page scroll so that the anchor will be in the middle of the page?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(11)
我找到了 Phillip 发布的解决方案带有固定标题的锚链接 ,他是一名网页设计师。菲利普添加了一个新的空跨度作为锚位置。
然后输入以下CSS代码
谢谢,Phillip!
I found a solution Anchor Links With A Fixed Header posted by Phillip, he is a web designer. Phillip added a new EMPTY span as the anchor position.
then put the following css code
Thanks, Phillip!
放置一个
然后设置
anchor
类的样式,如下所示: w3schools.com/cssref/css_units.asp" rel="noreferrer">
-50vh
锚点将在屏幕中间滚动。Place a
<span class="anchor" id="X">
then style theanchor
class like:With
-50vh
the anchor will scroll in the middle of the screen.html:
css:
对我来说效果很好
html:
css:
worked fine for me
在纯 html\css 中没有直接的方法可以做到这一点。尽管使用js,通过获取元素的顶部位置并将页面的顶部位置设置为该元素的位置减去页面高度的一半是可能的。
There is no straight way of doing this in pure html\css. It is possible though using js, by getting the element's top location and setting the page's top position to that element's position minus half of the page's height.
如果你想要没有空格,请像这样:
但是,你仍然需要通过 javascript 来调节高度
if you want without blank space do it like this:
<h2 id="jump_tag" style="padding-top: 500pt; margin-top: -500pt;"></h2>
but, you'll still have to regulate height from javascript
扩展 charles.cc.hsu 的答案,我们可以使用
vh< 对任意高度的元素执行此操作/code> CSS 单位,相对于视口的高度。
HTML:
CSS:
vh
在 IE9 及更高版本中受支持,因此使用起来非常安全。Extending off of charles.cc.hsu's answer, we can do this for elements of arbitrary height using the
vh
CSS unit, which is relative to the viewport's height.HTML:
CSS:
vh
is supported in in IE9 and up, so it's pretty safe to use.确定您实际想要将页面的哪一部分放在顶部,然后将锚点放在那里。您将无法更改浏览器解释锚点的方式 - 至少不能在不打扰您的用户的情况下。
Determine what part of your page you actually want at the top, and place the anchor there instead. You won't be able to change the way browsers interpret anchors - at least not without upsetting your users.
由于“页面中间”在任何给定时间都相对于用户屏幕和窗口的大小,因此您将必须使用 Javascript 来实现此目的,因为纯 HTML/CSS 中无法获得垂直屏幕宽度。
Since "middle of the page" is relative to the size of the user's screen and window at any given time, you are going to have to use Javascript to achieve this, as there is no way in pure HTML/CSS to get the vertical screen width.
我会尝试在目标锚标记上放置等于页面高度一半的负边距(或其他定位方法)。
I'd try putting a negative margin (or other positioning method) equal to half the page height on the target anchor tag.
Firefox 支持在指定锚点上设置 padding-top 属性。从那里,您可以通过 javascript 设置一个包含浏览器尺寸的 cookie,并相应地在服务器端调整您的 padding-top。对于最终用户来说,它看起来像是纯 html/css,但 noam 是正确的,因为需要一点点 JS 来获取浏览器的尺寸,因为它不会在没有一点强制的情况下为您提供此信息。
Firefox supports setting a padding-top property on the named anchor. From there, you could set a cookie via javascript that contains the browser's dimensions, and adjust your padding-top accordingly server-side. To the end user, it would look like its pure html/css, but noam is correct in that a wee bit of JS is needed to get the browser's dimensions, since it doesn't give you this information without a little coercion.