简体   繁体   中英

How can I detect whether or not a browser is mobile and display a video at different sizes with javascript “if else”?

So I need to include javascript and jquery in my html page that will check whether visitors are using mobile browsers, and display a video at different sizes depending accordingly.

I've tried several ways of doing this, but have yet to find something that actually works.

What I need is something like:

if (isMobile()) {
   //display larger
else {
   //display smaller
}

Can anyone help?

if (!('ontouchstart' in window)) {
  // is desktop
  elVideo.src= 'desktop.mp4';
} else {
  // is mobile
  elVideo.src= 'mobile.mp4';
}

You can detect like this

if(jQuery.browser.mobile)
    {
       //code to be executed when device is mobile
    }
    else
    {
       //code to be executed when device is not mobile
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM