2015-04-12 32 views
0

我使用这个代码smoothscroll目标链接偏移值,但我不能设置偏移值top100px无法设置smoothscroll的jQuery

$(function() { 
    $('a[href*=#]:not([href=#])').click(function() { 
    if (location.pathname.replace(/^\//,'') == this.pathname.replace(/^\//,'') && location.hostname == this.hostname) { 
     var target = $(this.hash); 
    target = target.length ? target : $('[name=' + this.hash.slice(1) +']'); 
     if (target.length) { 
     $('html,body').animate({ 
     scrollTop: target.offset().top 
     }, 1000); 
     return false; 
     } 
    } 
    }); 
}); 
+0

哦,伙计我刚刚弄明白了我自己。 –

回答

0

我只是理解了它自己。所以我想分享它。

$(function() { 
    $('a[href*=#]:not([href=#])').click(function() { 
    if (location.pathname.replace(/^\//,'') == this.pathname.replace(/^\//,'') && location.hostname == this.hostname) { 
     var target = $(this.hash); 
     var target_pos = (target.length > 0) ? target.offset().top : 0; 
     var rhd = $('.navtop'); 
     var rhd_height = (rhd.length > 0) ? rhd.outerHeight() : 190;//change this value to get your desired offset value 
       target = target.length ? target : $('[name=' + this.hash.slice(1) +']'); 
       target_pos -= rhd_height; 

     if (target_pos > 0) { 

      $('html,body').animate({ 

       scrollTop: target_pos 

      }, 1000); 

      return false; 

     } 
    } 
    }); 
}); 

我试图设置偏移值到我的固定导航bar.It工作对我来说很好。