0

I have a slide where I want to show photos specific for each device. iPhone, iPad, Desktop.

I would like to use something like this to filter each out. Obviously display:none isn't going to work since it's JavaScript, but this is just a general idea of how it should work:

<style type="text/css">
@media only screen and (min-width: 800px) {
    #slide-desktop{display:block;}
    #slide-ipad{display:none;}
    #slide-iphone{display:none;}
}
@media only screen and (min-width: 481px) and (max-width: 799px) {
    #slide-desktop{display:none;}
    #slide-ipad{display:block;}
    #slide-iphone{display:none;}
}
@media only screen and (max-width: 480px) {
    #slide-desktop{display:none;}
    #slide-ipad{display:none;}
    #slide-iphone{display:block;}
}
</style>
<div id="slide-desktop">
    <script>
        $.vegas('slideshow', {
        delay:6000,
        backgrounds:[
        { src:'images/slide/desktop.jpg', fade:600 },
        ]
        })('overlay');
    </script>
</div>
<div id="slide-ipad">
    <script>
        $.vegas('slideshow', {
        delay:6000,
        backgrounds:[
        { src:'images/slide/ipad.jpg', fade:600 },
        ]
        })('overlay');
    </script>
</div>
<div id="slide-iphone">
    <script>
        $.vegas('slideshow', {
        delay:6000,
        backgrounds:[
        { src:'images/slide/iphone.jpg', fade:600 },
        ]
        })('overlay');
    </script>
</div>
Wolf Cat
  • 171
  • 1
  • 3
  • 16

2 Answers2

1

If you want to run different scripts based on a media query you can use enquire.js

enquire.js is a lightweight, pure JavaScript library for responding to CSS media queries.

So, you no need to put anything in a style at all.

andbas
  • 571
  • 3
  • 7
1

If you want to check by the device width you can use simple javascript like this

   if(window.innerWidth >= 800)
   {
     alert('desktop?');
   }
   else if(window.innerWidth >= 481 && window.innerWidth < 799)
   {
     alert('ipad?');
   }
   else if(window.innerWidth < 480)
   {
    alert('iphone?');
   }

But I would not go for this technique. Check out

What is the best way to detect a mobile device in jQuery?

Community
  • 1
  • 1
oBo
  • 974
  • 2
  • 13
  • 26