2012-07-22 52 views
2

我加載了這個jQuery-snippet通過anchorLinks滑動頁面。jQuery:通過anchorlink滑動

http://www.position-absolute.com/articles/better-html-anchor-a-jquery-script-to-slide-the-scrollbar/

在我來說,我有一個固定塊(位置:固定)在頁面的頂部。由於這個原因,我需要一個delta值來滑下。如果我不使用這種增量值,頁面將滑動到深處,以便我的anchorLink被固定塊隱藏起來。

有沒有人知道如何解決這個問題?

THX

回答

0

調用它必須有一個解決方案通過編輯這一行:

window.location.hash = elementClick 

更改持續時間值有助於滑動到合適的位置。但是,然後窗口在滑動結束時「跳轉」偏移值。

:我通過去除線固定的問題:

window.location.hash = elementClick 
0

原來的插件:

jQuery.fn.anchorAnimate = function(settings) { 

    settings = jQuery.extend({ 
     speed : 1100 
    }, settings); 

    return this.each(function(){ 
     var caller = this 
     $(caller).click(function (event) { 
      event.preventDefault() 
      var locationHref = window.location.href 
      var elementClick = $(caller).attr("href") 

      var destination = $(elementClick).offset().top; 
      $("html:not(:animated),body:not(:animated)").animate({ scrollTop: destination}, settings.speed, function() { 
       window.location.hash = elementClick 
      }); 
      return false; 
     }) 
    }) 
} 

修改

jQuery.fn.anchorAnimate = function(settings) { 

    settings = jQuery.extend({ 
     speed : 1100, 
      offset: 0 
    }, settings); 

    return this.each(function(){ 
     var caller = this 
     $(caller).click(function (event) { 
      event.preventDefault() 
      var locationHref = window.location.href 
      var elementClick = $(caller).attr("href") 

      var destination = $(elementClick).offset().top; 
      $("html:not(:animated),body:not(:animated)").animate({ scrollTop: destination+settings.offset}, settings.speed, function() { 
       window.location.hash = elementClick 
      }); 
      return false; 
     }) 
    }) 
} 

請注意,我只是增加了一個offset選項。所以,如果你的固定div有60像素的高度,然後只需用$('#whatever').anchorAnimate({offset: 60});

+0

謝謝!!!!我試圖通過添加一個靜態值,例如60像素。但我認爲「window.location.hash = elementClick」是問題所在。 – Vivid 2012-07-22 07:27:19

+0

您的更改不能解決問題。 :-( – Vivid 2012-07-22 07:37:24