【问题标题】:How to change URL(hash) when user scroll or click on the link with jquery history plugin当用户滚动或单击带有 jquery 历史插件的链接时如何更改 URL(哈希)
【发布时间】:2013-06-03 10:40:47
【问题描述】:

我正在开发一个单页网站。我的单页中有很多部分。 我想在用户单击链接或上下滚动时更改 URL、哈希。

我希望网址是:

http://mysite.com/section1

http://mysite.com/section2

谁能帮我解决这个问题。

提前致谢。

【问题讨论】:

  • 我曾经尝试过这个,但我不知道我做错了什么,但它在 Firefox 上效果不佳。你能给我简单的演示页面吗?谢谢。
  • 它没有改变哈希值。当我尝试更改 hash 时,我在 firefox 上遇到了滚动问题。你能举个例子来改变哈希吗?

标签: javascript jquery


【解决方案1】:

试试这个,

var lastScrollTop = 0;
$(window).scroll(function(event){
   var st = $(this).scrollTop();
   if (st > lastScrollTop){
       // downscroll code
       window.location.hash='musection2';
   } else {
      // upscroll code
      window.location.hash='musection1';
   }
   lastScrollTop = st;
});

来自How can I determine the direction of a jQuery scroll event?

【讨论】:

    猜你喜欢
    • 2014-12-02
    • 2011-07-31
    • 1970-01-01
    • 2021-09-21
    • 2015-05-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-11-04
    相关资源
    最近更新 更多