0

I have a jquery issue which I need some help with and can't seem to find any results which solve my issue. I have a 1 page site which uses the jquery below to smoothly scroll to anchor links. The only issue is that when it's on mobile, I need to adjust the scroll to have a top -170px deficit. How do I only target mobile queries using the same function below?

$(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
    }, 700);
    return false;
  }
}
});
});
Jim B
  • 165
  • 1
  • 10
  • Targeting mobiles? http://stackoverflow.com/questions/3514784/what-is-the-best-way-to-detect-a-mobile-device-in-jquery – Striker May 17 '16 at 11:01

3 Answers3

1

You can check screen width and if it's less than certain width, say 320, then you can take into consideration the extra scroll offset required:

$(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) {
              var winWidth = $(window).width();
              if( winWidth > 320 )
              {
                  $('html, body').animate({
                      scrollTop: target.offset().top
                    }, 700);
              }
              else
              {
                    $('html, body').animate({
                      scrollTop: target.offset().top - 170
                    }, 700);
              }
            return false;
          }
        }
    });//click
});//ready
Mohit Bhardwaj
  • 8,005
  • 3
  • 29
  • 60
1

I can provide 2 options for you:

You can load a JS library to check which browser/device you are on. https://github.com/rafaelp/css_browser_selector. This loads a class on the HTML element which you can check in your function like this:

    $(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) {
                    if($('html').hasClass('mobile')) {
                        $('html, body').animate({
                            scrollTop: target.offset().top - 170
                        }, 700);
                    } else {
                        $('html, body').animate({
                            scrollTop: target.offset().top
                        }, 700);
                     }
                     return false;
                 }
             }
        });
    });

Or you can check the window size to check if it's lower then tablet:

    $(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) {
                    if($(window).width() < 768) {
                        $('html, body').animate({
                            scrollTop: target.offset().top - 170
                        }, 700);
                    } else {
                        $('html, body').animate({
                            scrollTop: target.offset().top
                        }, 700);
                     }
                     return false;
                 }
             }
        });
    });
0

This works fine for me,Pass the id properly , it works properly

ele.on('click',function(){

            $('html, body').animate({scrollTop: $("#"+id).offset().top}, 750);

        });
Ashok Mandal
  • 280
  • 4
  • 13