HTML:创建一个链接到位于页面中间居中的锚点

发布于 2024-08-04 19:58:08 字数 78 浏览 5 评论 0原文

我的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(11

疯狂的代价 2024-08-11 19:58:09

我找到了 Phillip 发布的解决方案带有固定标题的锚链接 ,他是一名网页设计师。菲利普添加了一个新的空跨度作为锚位置。

<span class="anchor" id="section1"></span>
<div class="section"></div>

然后输入以下CSS代码

.anchor{
  display: block;
  height: 115px; /*same height as header*/
  margin-top: -115px; /*same height as header*/
  visibility: hidden;
}

谢谢,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.

<span class="anchor" id="section1"></span>
<div class="section"></div>

then put the following css code

.anchor{
  display: block;
  height: 115px; /*same height as header*/
  margin-top: -115px; /*same height as header*/
  visibility: hidden;
}

Thanks, Phillip!

披肩女神 2024-08-11 19:58:09

放置一个 然后设置 anchor 类的样式,如下所示

.anchor {
  position: absolute;
  transform: translateY(-50vh);
}

: w3schools.com/cssref/css_units.asp" rel="noreferrer">-50vh 锚点将在屏幕中间滚动。

Place a <span class="anchor" id="X"> then style the anchor class like:

.anchor {
  position: absolute;
  transform: translateY(-50vh);
}

With -50vh the anchor will scroll in the middle of the screen.

呆头 2024-08-11 19:58:09

html:

<a id="anchor">NEWS</a>

css:

#anchor{
position: relative;
padding-top: 100px; //(whatever distance from the top of the page you want)
}

对我来说效果很好

html:

<a id="anchor">NEWS</a>

css:

#anchor{
position: relative;
padding-top: 100px; //(whatever distance from the top of the page you want)
}

worked fine for me

删除→记忆 2024-08-11 19:58:09

在纯 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.

长亭外,古道边 2024-08-11 19:58:09

如果你想要没有空格,请像这样:

但是,你仍然需要通过 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

暮色兮凉城 2024-08-11 19:58:09

扩展 charles.cc.hsu 的答案,我们可以使用 vh< 对任意高度的元素执行此操作/code> CSS 单位,相对于视口的高度。

HTML:

<span class="anchor" id="section1"></span>
<div class="section"></div>

CSS:

.anchor{
    display: block;
    height: 50vh; /* 50% viewport height */
    margin-top: -50vh;
    visibility: hidden;
}

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:

<span class="anchor" id="section1"></span>
<div class="section"></div>

CSS:

.anchor{
    display: block;
    height: 50vh; /* 50% viewport height */
    margin-top: -50vh;
    visibility: hidden;
}

vh is supported in in IE9 and up, so it's pretty safe to use.

假装不在乎 2024-08-11 19:58:09

确定您实际想要将页面的哪一部分放在顶部,然后将锚点放在那里。您将无法更改浏览器解释锚点的方式 - 至少不能在不打扰您的用户的情况下。

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.

ぇ气 2024-08-11 19:58:09

由于“页面中间”在任何给定时间都相对于用户屏幕和窗口的大小,因此您将必须使用 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.

忆沫 2024-08-11 19:58:09
$(window).on("load", function () {
    var urlHash = window.location.href.split("#")[1];
    if (urlHash &&  $('#' + urlHash).length )
          $('html,body').animate({
              scrollTop: $('#' + urlHash).offset().top-100
          }, 1000);
});
$(window).on("load", function () {
    var urlHash = window.location.href.split("#")[1];
    if (urlHash &&  $('#' + urlHash).length )
          $('html,body').animate({
              scrollTop: $('#' + urlHash).offset().top-100
          }, 1000);
});
方觉久 2024-08-11 19:58:09

我会尝试在目标锚标记上放置等于页面高度一半的负边距(或其他定位方法)。

I'd try putting a negative margin (or other positioning method) equal to half the page height on the target anchor tag.

好菇凉咱不稀罕他 2024-08-11 19:58:09

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文