2011-11-16 67 views
0

我从这个页面中实现如下代码:http://tympanus.net/codrops/2010/06/02/smooth-vertical-or-horizontal-page-scrolling-with-jquery/jQuery的滚动

$(document).ready(function() { 

      $('ul.navone li a, ul.navtwo li a,a.toplink, a.bodylink').bind('click',function(event){ 
       var $anchor = $(this); 

       $('html, body, header').stop().animate({ 
        scrollTop: $($anchor.attr('href')).offset().top 
       }, 1500,'easeInOutExpo'); 

       event.preventDefault(); 
      }); 
     }); 

这一切都正常工作。

但是,在我的布局中,我有一个固定的标题div(即它在用户滚动时保持原位)。因此,我需要为117像素的滚动脚本设置偏移量。

请问我该怎么做?

回答

2

将是类似的东西:

$(document).ready(function() { 

      $('ul.navone li a, ul.navtwo li a,a.toplink, a.bodylink').bind('click',function(event){ 
       var $anchor = $(this); 

       $('html, body, header').stop().animate({ 
        scrollTop: ($($anchor.attr('href')).offset().top + 117) 
       }, 1500,'easeInOutExpo'); 

       event.preventDefault(); 
      }); 
     }); 

只需添加+117至scrollTop的位置

+0

优秀的,我其实需要的是-117,我改变.offset()顶部+ 117)至 - 117它的工作很好。非常感谢 –

+0

嗯,这似乎有移动Safari浏览器的问题 – K2xL