Image Slider - different images for mobile and desktop

Viewed 2270

I am using SlidesJS. Image slider it working fine for the desktop but now i want to load different set of images for mobile devices. I looked into SlidesJS forum and it's website but i can't see find any hint there, I also looked at various questions on SO but none works. As slidesJS provides default code like this

Javascript

 <!-- SlidesJS Required: Initialize SlidesJS with a jQuery doc ready -->
  <script>
    $(function() {
      $('#gallery-slides').slidesjs({
        width: 800,
        height: 450,
        play: {
          active: true,
          auto: true,
          interval: 4000,
          swap: true
        }
      });
    });

  </script>

HTML

<div id="gallery-slides">
 <img src="images/nearby_page_concept.jpg" >
 <img src="images/location_gif2-min.gif" >
 <img src="images/smart_search_page.jpg" >
 <img src="images/privacy_control_concept.jpg" >
 <img src="images/messaging_page_concept.jpg" >
 <img src="images/user_search_page_concept.jpg" >   

 </div>

CSS

#gallery-slides {
      display: none
    }



    #gallery-slides .slidesjs-navigation {
      margin-top:5px;
    }

    a.slidesjs-next,
    a.slidesjs-previous,
    a.slidesjs-play,
    a.slidesjs-stop {
      background-image: url(../images/btns-next-prev.png);
      background-repeat: no-repeat;
      display:block;
      width:12px;
      height:18px;
      overflow: hidden;
      text-indent: -9999px;
      float: left;
      margin-right:5px;
    }

    a.slidesjs-next {
      margin-right:10px;
      background-position: -12px 0;
    }

    a:hover.slidesjs-next {
      background-position: -12px -18px;
    }

    a.slidesjs-previous {
      background-position: 0 0;
    }

    a:hover.slidesjs-previous {
      background-position: 0 -18px;
    }

    a.slidesjs-play {
      width:15px;
      background-position: -25px 0;
    }

    a:hover.slidesjs-play {
      background-position: -25px -18px;
    }

    a.slidesjs-stop {
      width:18px;
      background-position: -41px 0;
    }

    a:hover.slidesjs-stop {
      background-position: -41px -18px;
    }

    .slidesjs-pagination {
      margin: 7px 0 0;
      float: right;
      list-style: none;
    }

    .slidesjs-pagination li {
      float: left;
      margin: 0 1px;
    }

    .slidesjs-pagination li a {
      display: block;
      width: 13px;
      height: 0;
      padding-top: 13px;
      background-image: url(../images/pagination.png);
      background-position: 0 0;
      float: left;
      overflow: hidden;
    }

    .slidesjs-pagination li a.active,
    .slidesjs-pagination li a:hover.active {
      background-position: 0 -13px
    }

    .slidesjs-pagination li a:hover {
      background-position: 0 -26px
    }

    #gallery-slides a:link,
    #gallery-slides a:visited {
      color: #333
    }

    #gallery-slides a:hover,
    #gallery-slides a:active {
      color: #9e2020
    }

    .gallery-navbar {
      overflow: hidden
    }

     #gallery-slides {
      display: none
    }

    .containerslides {
      margin: 0 auto
    }

I tried different approach to make slider change image on mobile device like

Giving class name to instead of <img src="xyz.jpg"> and declare different images with the help of media quires in css

Tried if else statement of javascript in HTML

I also need to figure out if i can change image then i also need to change SlidesJS's default height and width of JS part.

Please guide me how to change different images for different screen sizes for Image Slider.

Please Note i don't want to load all different resolutions images when i am not showing it. It affects the performance of website.

1 Answers

You can use jquery, if you load the images you want through javascript this will load only a specific part of the code thus not effecting the performance of the website

jQuery

<script>if (window.matchMedia("(max-width: 480px)").matches) {
        $.getScript("mobile.js");
    } else { 
        $.getScript("Desktop.js");
    }
    </script>

mobile.js/ Desktop.js - Stores location/s of all the images for displaying in mobile/desktop respectively it goes like this

JS

var _img1 = document.getElementById('id1');
var _img2 = document.getElementById('id2');
var _img3 = document.getElementById('id3');

var newImg = new Image;
newImg.onload = function() {
    _img1.src = 'https://whateversource';
    _img2.src = 'https://whateversource';
    _img3.src = 'https://whateversource';
    wherever you want to return it goes here
}

HTML

<div id="gallery-slides">
 <img id="id1">
 <img id="id2">
 <img id="id3"> 
 </div>

Enjoy :)

Related