【问题标题】:remove hash from url smoothscroll.js从 url smoothscroll.js 中删除哈希
【发布时间】:2015-08-13 14:42:34
【问题描述】:

我在使用 plagin (smoothscroll.js) 的单页滚动模板中遇到问题 用户单击链接后,我尝试从 url 中删除哈希,但不幸的是我不能这样做。例如

  • 关于我们
  • url 像这样http://localhost/template/#section2 我如何从网址中删除#section2

    这里是代码

    var linkHandler = function(ev) {
    ev.preventDefault();
    
    if (location.hash !== this.hash) window.history.pushState(null, null, this.hash);
    // using the history api to solve issue #1 - back doesn't work
    // most browser don't update :target when the history api is used:
    // THIS IS A BUG FROM THE BROWSERS.
    // change the scrolling duration in this call
    smoothScroll(document.getElementById(this.hash.substring(1)), 1000, function(el) {
        location.replace('#' + el.id);
        // this will cause the :target to be activated.
    });
    

    }

    【问题讨论】:

    标签: jquery html


    【解决方案1】:

    从 URL 中删除 section2:

    location.hash = '';
    

    编辑

    不知道为什么这不起作用,但这是另一种效率较低的方法:

    location.href = location.href.split('#')[0];
    

    【讨论】:

    • 在我在函数末尾添加这个之后,它会删除它,但是一旦滚动完成它就会再次出现。
    • 你能把你的代码放到jsfiddle或者codepen吗?这样会更容易提供帮助。
    • 亲爱的托德,如果可能的话,请检查一下。 github.com/alicelieutier/smoothScroll
    猜你喜欢
    • 2011-05-29
    • 2012-04-19
    • 2013-05-15
    • 2013-03-02
    • 2017-05-31
    • 2018-01-23
    • 1970-01-01
    相关资源
    最近更新 更多