手机上不同的滚动偏移量

时间:2017-04-04 16:19:11

标签: javascript jquery

我正在使用以下代码来平滑链接之间的滚动,它完美无缺。

我目前的偏移量为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();
       }
   });

1 个答案:

答案 0 :(得分:2)

您可以使用JQuery $( window ).height()方法获取当前窗口高度并检测小型设备。根据此值,您可以以不同方式显示偏移量。

此处有更多信息:https://api.jquery.com/height/