为什么页面锚点有时会丢失?

发布于 2024-07-12 07:51:45 字数 642 浏览 9 评论 0 原文

在 HTML 页面上,像这样的链接:

<a href="#pagelocation">Location on Page</a>

...应该导航到页面上的这个位置:

<a name="pagelocation">

但根据我的经验,它有时会丢失 - 特别是从另一个页面链接时(例如 )。 我所说的“未命中”是指它滚动到页面上的错误位置 - 也许接近,也许不是。

通常,目标位置最终位于屏幕顶部。 我知道如果锚点下方没有足够的空间将其滚动到屏幕顶部,这可能会失败。

不然为什么会失败呢? 它完全取决于布局吗? 我该如何解决这个问题?

(我保留这个一般性的内容是因为我想要一个包罗万象的参考答案。)

更新 1

感谢您迄今为止有关非明确图像尺寸的指示。 但是在所有元素都具有明确大小的页面上又如何呢? (我现在正在处理一个。)

On an HTML page, a link like this:

<a href="#pagelocation">Location on Page</a>

...should navigate to this spot on the page:

<a name="pagelocation">

But in my experience, it sometimes misses - especially when linking from another page (like <a href="somepage.html#pagelocation">). By "misses," I mean it scrolls to the wrong spot on the page - maybe close, maybe not.

Normally, the target location ends up at the top of the screen. I know this can fail if there's not enough room below the anchor to scroll it to the top of the screen.

Why else would it fail? Does it depend on layout at all? How can I fix it?

(I'm keeping this general because I'd like a catch-all reference answer.)

Update 1

Thanks for the pointers so far about non-explicit image sizes. But what about on a page where all the elements have explicit size? (I'm dealing with one now.)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(7

峩卟喜欢 2024-07-19 07:51:45

通常,滚动可能会在页面完成加载之前发生。 如果您的图像没有宽度和高度,页面将跳转,然后加载图像并重新布局本身,使您之前跳转的位置看起来是错误的。

编辑:任何其他可以改变页面布局的东西也应该被怀疑......这包括未在 中加载的 javascript 和 CSS(不用介意所有 CSS 应该< /em> 被加载到头部;但并不总是如此)。

如果页面通过重定向弹回,我相信 IE 会滚动结束页面,但 Firefox 不会。

Quite often the scrolling can occur before the page has finished loading. If you have images without widths and heights, the page will jump, then load the image and re-layout itself, making the place you previously jumped to seem wrong.

Edit: Anything else that can change page layout should also be considered with suspicion... this include javascript and CSS that's not loaded in the <head> (never mind that all CSS should be loaded in the head; it isn't always).

If the page is bounced through a redirect, I believe IE will scroll the end page but Firefox won't.

歌入人心 2024-07-19 07:51:45

JS 解决方案

在文档准备好后运行此函数。

 function goToAnchor() { 
    hash = document.location.hash;
    if (hash !="") {
        setTimeout(function() {
            if (location.hash) {
                window.scrollTo(0, 0);
                window.location.href = hash;
            }
        }, 1);
    }
    else {
        return false;
    }
}

JS Solution

Run this function on document ready.

 function goToAnchor() { 
    hash = document.location.hash;
    if (hash !="") {
        setTimeout(function() {
            if (location.hash) {
                window.scrollTo(0, 0);
                window.location.href = hash;
            }
        }, 1);
    }
    else {
        return false;
    }
}
愁杀 2024-07-19 07:51:45

我相信您所看到的行为是浏览器在所有图像加载完成之前定位到页面上该位置的结果。 一旦图像完成加载,页面的布局就会发生变化(例如,页面可能垂直更长),导致锚点的位置发生变化 - 但浏览器仍然认为它已经导航到该位置锚。

I believe the behavior you are seeing is the result of the browser locating to that spot on the page before all images have finished loading. Once the images finish loading, then the layout of the page has changed (the page is likely longer vertically, for example), causing the location of where the anchor should be to have changed - but the browser still thinks it has already navigated to that anchor.

天煞孤星 2024-07-19 07:51:45

如上所述,这可能是由于图像渲染较晚并在加载时“调整”布局。

如果您可以指定图像的大小,那么可以在渲染之前分配足够的空间,这应该可以防止出现问题。

作为旁注,我以前遇到过这个问题,形式是在足够多的页面之间使用前进/后退,图像需要重新加载,导致我在渲染后最终出现在错误的位置。

As mentioned above, this is probably due to images being rendered late and 'adjusting' the layout as they load.

If you can specify the size of the images then that much room can be allocated before they render, which should prevent the problem.

As a side note I've had this problem before in the form of using forward/back between enough pages that the images needed reloading, causing me to end up in the wrong place after they had rendered.

锦欢 2024-07-19 07:51:45

当 JavaScript 在页面顶部创建下拉菜单时,我也看到过这种情况发生。 然后,一旦菜单完成,它就会被隐藏,并向上滚动下面的内容。

与此同时,浏览器已经将目标位置设置在窗口顶部。 隐藏页面最顶部的菜单会将目标位置从窗口顶部向上移动。

I have also seen this happen when JavaScript creates a drop-down menu at the top of a page. Then, once the menu has been finished, it is hidden, scrolling up the content below.

In the meantime, the browser has already set the target location at the top of the window. Hiding the menu a the very top of the page moves the target location up off the top of the window.

合约呢 2024-07-19 07:51:45

请注意,您可以将 id="pagelocation" 添加到几乎任何 HTML 元素中,以获得相同的结果,这样您就无需为链接目标添加额外的锚点。

Note that you can add id="pagelocation" to just about any HTML element, for the same result, which saves you adding the additional anchors for link destinations.

無心 2024-07-19 07:51:45

好的。 我认为这是新的。 使用 HTML5 的 autofocus 会导致失火,jQuery 的 focus() 方法也是如此。 经过 90 分钟的反复试验才发现这一点,因为我认为问题与图像相关:)

OK. I think this is new. Using HTML5's autofocus will cause a misfire, as will jQuery's focus() method. Took 90 minutes of trial and error to discover this because I thought the issue was image related :)

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