CharlyAnderson
CharlyAnderson

Reputation: 737

Different scroll offset on mobile

I am using the following code to smooth scroll between links and it works perfectly.

I currently have an offset of 93px but wish to make this smaller, if not remove on small devices. I am wondering if its possible to change the offset value for smaller resolutions.

   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();
       }
   });

Upvotes: 2

Views: 985

Answers (1)

Nicolas Form
Nicolas Form

Reputation: 51

You can use JQuery $( window ).height() method to get the current window height and detect small devices. Based on this value you can display your offset differently.

More info here : https://api.jquery.com/height/

Upvotes: 2

Related Questions