2014-04-03 51 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