2017-04-04 24 views
1

我使用下面的代碼來平滑鏈接之間的滾動,並且它完美地工作。手機上的不同滾動偏移量

我目前的偏移量爲93px,但希望使其更小,如果不能在小設備上移除。我想知道是否可以更改較小分辨率的偏移值。誰能幫忙?

var jump=function(e) 
    { 
     if (e){ 
      e.preventDefault(); 
      var target = $(this).attr("href"); 
     }else{ 
      var target = location.hash; 
     } 

     $('html,body').animate(
     { 
      scrollTop: $(target).offset().top-93 
     },2000,function() 
     { 
      location.hash = target; 
     }); 

    } 

    $('html, body').hide(); 

    $(document).ready(function() 
    { 
     $('a[href^=#]').bind("click", jump); 

     if (location.hash){ 
      setTimeout(function(){ 
       $('html, body').scrollTop(0).show(); 
       jump(); 
      }, 0); 
     }else{ 
      $('html, body').show(); 
     } 
    }); 

回答

2

您可以使用JQuery $(window).height()方法獲取當前窗口高度並檢測小設備。根據此值,您可以以不同的方式顯示您的偏移量。

此處瞭解詳情:https://api.jquery.com/height/