2015-04-04 95 views
0

我想在不使用#的情況下滾動條時更改網址。這是我的代碼: -使用jquery/javascript滾動更改網址

<a class = "section" 
    href = "#first" > First paragraph < /a> <p> < /p> < a class = "section" 
    href = "#second" > Second paragraph < /a> <p> < /p> < a class = "section" 
    href = "#third" > Third paragraph < /a> <p> < /p> < a class = "section" 
    href = "#fourth" > Fourth paragraph < /a> 

    function isScrolledIntoView(elem) { 
     var docViewTop = $(window).scrollTop(); 
     var docViewBottom = docViewTop + $(window).height(); 
     var elemTop = $(elem).offset().top; 
     var elemBottom = elemTop + $(elem).height(); 
     return ((elemBottom <= docViewBottom) && (elemTop >= docViewTop)); 
    } 

    $(window).scroll(function(e) { 
     var anchors = $('.section'); 
     for (var i = 0; i < anchors.length; ++i) { 
      if (isScrolledIntoView(anchors[i])) { 
       var href = $(anchors[i]).attr('href'); 
       location.hash = href.slice(href.indexOf('#') + 1); 
       break; 
      } 
     } 
    }); 

    $(function() { 
     var hash = location.hash; 
     if (hash) { 
      // Scroll to the element with the given hash. 
      var anchors = $('.section'); 
      for (var i = 0; i < anchors.length; ++i) { 
       if ($(anchors[i]).attr('href') === hash) { 
        $(window).scrollTop($(anchors[i]).offset().top); 
        break; 
       } 
      } 
     } 
    }); 

當我運行這段代碼我的網址顯示是這樣的:

file:///C:/Users/jony/Desktop/demo.html#third 

,但我想說明這樣的:

file:///C:/Users/jony/Desktop/demo.html/third 
+0

這是不可能沒有''#你可以'#/ third'但不只是'/ third' – mohamedrias 2015-04-04 10:14:05

+0

@mohamedrias如果選中此網址https://baggu.com/這是確切的功能我想要的 – Mangita 2015-04-04 10:17:03

+0

爲什麼你想刪除散列? – 2015-04-04 10:27:01

回答