我可以在不滚动网页的情况下更新 window.location.hash 吗?

发布于 2024-07-15 05:49:04 字数 234 浏览 8 评论 0原文

使用JavaScript,有没有办法在不滚动网页的情况下更新window.location.hash?

我有可点击的标题元素,可以切换它们正下方的 div 的可见性。 我希望在单击标题时在历史记录中显示 /foo#bar,但不希望页面滚动。 因此,当离开 /foo#bar 时,我将能够使用后退按钮,并让 ID 位于 window.location.hash 中的 div 在返回时可见。

这种行为可能吗?

Using JavaScript, is there a way to update window.location.hash without scrolling the web page?

I have clickable title elements that toggle the visibility of a div directly beneath them. I want the /foo#bar in the history when clicking titles but don't want the page scrolling about. So when navigating away from /foo#bar I'll be able to use the back button and have the div whose ID is in window.location.hash be visible upon return.

Is this behavior possible?

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

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

发布评论

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

评论(6

む无字情书 2024-07-22 05:49:04

要在不重新加载/滚动页面的情况下更改哈希值,您现在只需使用 html5 history.pushState 即可。

history.pushState(null,null,'#hashexample');

所有主要浏览器均支持它:

http://caniuse.com/history

MDN:

https://developer.mozilla.org /en-US/docs/Web/Guide/API/DOM/Manipulated_the_browser_history#The_pushState().C2.A0method

另请注意,我们在这里使用的最后一个 url 参数可以是任何 url,因此它不限于哈希值。

To change the hash without having the page reload/scroll, you can now simply use html5 history.pushState.

history.pushState(null,null,'#hashexample');

It's supported by all the major browsers:

http://caniuse.com/history

MDN:

https://developer.mozilla.org/en-US/docs/Web/Guide/API/DOM/Manipulating_the_browser_history#The_pushState().C2.A0method

Also note that the last url parameter we're using here can be any url, so it's not limited to hashes.

柠檬色的秋千 2024-07-22 05:49:04

尽可能简单

var scrollmem = $('html,body').scrollTop();
window.location.hash = hash;
$('html,body').scrollTop(scrollmem);

As easy as it get

var scrollmem = $('html,body').scrollTop();
window.location.hash = hash;
$('html,body').scrollTop(scrollmem);
予囚 2024-07-22 05:49:04

您可以尝试的另一件事是暂时更改哈希指向的元素的 id。 为我工作!

function changeHashWithoutScrolling(hash) {
  const id = hash.replace(/^.*#/, "")
  const elem = document.getElementById(id)
  if (elem) {
    elem.id = id + "-tmp"
    window.location.hash = hash
    elem.id = id
  }
}

Another thing you could try is changing the id of the element the hash points to temporarily. Worked for me!

function changeHashWithoutScrolling(hash) {
  const id = hash.replace(/^.*#/, "")
  const elem = document.getElementById(id)
  if (elem) {
    elem.id = id + "-tmp"
    window.location.hash = hash
    elem.id = id
  }
}
爱人如己 2024-07-22 05:49:04

这种行为是很有可能的。 您应该研究一些为您提供此功能而开发的库。

非常简单的历史记录:http://code.google.com/p/reallysimplehistory/
SWFAddress: http://www.asual.com/swfaddress/

This behavior is very much possible. You should look into some of the libraries that have been developed to give you this functionality.

Really Simple History: http://code.google.com/p/reallysimplehistory/
SWFAddress: http://www.asual.com/swfaddress/

生生不灭 2024-07-22 05:49:04

根据 Sunny 的回答,我创建了这个函数,它也避免了未定义和空值:

    function changeHashWithoutScrolling(hash) {
        var id;
        var elem;

        id = hash.replace(/^.*#/, '');

        if (id) {
            elem = document.getElementById(id);

            if (elem) {
                elem.id = id + '-tmp';
                window.location.hash = hash;
                elem.id = id;
            }
        }
    }

Based on the answer from Sunny I made this function that also avoids undefined and nulls:

    function changeHashWithoutScrolling(hash) {
        var id;
        var elem;

        id = hash.replace(/^.*#/, '');

        if (id) {
            elem = document.getElementById(id);

            if (elem) {
                elem.id = id + '-tmp';
                window.location.hash = hash;
                elem.id = id;
            }
        }
    }
开始看清了 2024-07-22 05:49:04

想向凯瑟琳的答案添加评论,但我还没有代表 -

很好的解决方案,但它在 Chrome 中对我不起作用,因为 $('html').scrollTop() 总是返回 0 - 一个小的编辑解决了问题:

scrollmem = $('html').scrollTop() || $('body').scrollTop();
window.location.hash = hash;
$('html,body').scrollTop(scrollmem);

Wanted to add a comment to Catherines answer but I don't have the rep yet -

Great solution however it wasn't working for me in Chrome as $('html').scrollTop() always returns 0 - a minor edit resolves the issue:

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