2014-04-03 63 views
0

我正在使用jQuery script滾動頁面(div)。如果您在網址中輸入#+項目(數字),您將獲得所需的頁面(div)。但是,當瀏覽菜單時,url不會相應地改變。jQuery腳本#url更改

當我通過菜單導航腳本時,如何獲取url,以相應地進行更改?

另外,有沒有辦法讓我從url(#item02)中刪除「item」,並且只留下數字? 甚至更​​好,每個頁面(div)都有自定義的#名稱?

<script> 
function resizePanel(){ 
    width = $(window).width(); 
    height = $(window).height(); 
    mask_width = width*$(".item").length; 

    $("#debug").html(); 
    $(".wrapper, .item").css({}); 
    $(".mask").css({}); 
    $(".wrapper").scrollTo($("a.selected").attr("href"),0)} 

    $(document).ready(function(){ 
     $("a.panel").click(function(){ 
      $("a.panel").removeClass("selected"); 
      $(this).addClass("selected"); 
      current = $(this); 
      $(".wrapper").scrollTo($(this).attr("href"),800); 
      return false; 
     }); 

     $(window).resize(function(){resizePanel()}) 
    }) 
</script> 
+0

當你正在繞過正常的書籤鏈接的行爲,你需要使用類似History.js管理瀏覽器歷史和哈希鏈接。 –

回答

1

在你的函數clickhandler你可以哈希添加到您的位置,這樣的:

$("a[href=#item4]").unbind().click(function(e){ 
    e.preventDefault(); 

    // scroll to desired container 
    $(".wrapper").scrollTo($(this).attr("href"),800, function(){ 
     // update location 
     var url = window.location.origin + window.location.pathname,  
     hash = '#item4'; 
     window.location.href = url + hash; 
    }); 
}); 
+0

似乎無法得到它的工作。 '' – Borsn

+0

是的,因爲您的選擇器是錯誤的。試試這個:$(「a [href =#item4]」)。click(function(){var url = window.top.location.origin + window.top.location.pathname,hash ='#/ contact'; \t window.location.href = url + hash;});' – nils

+0

它現在可以工作。但是它會造成另一個問題。 ** [看看。](http://pad.ample.pw/ucc/)**動畫不會工作。 – Borsn