jQuery function must be executed only on mobile

Asked

Viewed 1,673 times

3

I think the title is self-explanatory. I need a particular function in jQuery to "activate" only when I’m at a resolution less than 1024px.

  • would be like a media queries only with jquery?

1 answer

7

You can detect the screen size and perform the function:

screen.width and screen.height respectively store the width and height of the screen.

if(screen.width < 1024){ 
   //executa a funcao para ativar
}

Another way I found here javascript is like this:

The function returns true if the browser used is mobile and false otherwise.

function isMobile() {
  var userAgent = navigator.userAgent.toLowerCase();
  if (userAgent.search(/(android|avantgo|blackberry|bolt|boost|cricket|docomo|fone|hiptop|mini|mobi|palm|phone|pie|tablet|up\.browser|up\.link|webos|wos)/i) != -1) {
    return true;
  }
  return false;
}
  • It was just that. It didn’t come to my mind at the time. Thank you.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.