.scrollto only only on desktop with offset

111 views Asked by At

So after trying a few things I came across a few posts and found out that this code was working for me to automatically scroll to the desired location as soon as I clicked it. But on mobile I dont need the offset, thats why I want it to be removed. (basically media queries in javascript are needed).

$('.show-more').on('click', function () {
   $.scrollTo($('#scrollto_shortcut'), {
    duration: 0,
    offset: -60 
       //Only -60 on desktop with max-width is 1006px, on mobile no offset
    });
});

Can you guys help me out?

1

There are 1 answers

0
l2aelba On

Detect isMobile then set shorthand if else statement for offset

var isMobile = /Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent);
$('.show-more').on('click', function () {
   $.scrollTo($('#scrollto_shortcut'), {
       duration: 0,
       offset: !isMobile ? -60 : 0
   });
});