图像轮播 - 移动端和桌面端使用不同的图片

3
我在使用 SlidesJS。图片轮播在桌面端运行良好,但我现在想为移动设备加载不同的图片集。我查看了 SlidesJS 论坛和官网,但没有找到任何提示,我还看了 SO 上的各种问题,但都无效。因为 SlidesJS 提供了默认代码如下:

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
    }

我尝试了不同的方法来让滑块在移动设备上更换图片,例如

将class名称赋予给<img src="xyz.jpg">,并使用CSS中的媒体查询声明不同的图像

在HTML中尝试if else语句的使用

如果我能够更改图像,则还需要更改SlidesJS JS部分的默认高度和宽度。

请指导我如何为图像滑块更改不同屏幕尺寸的不同图像。

请注意,当我没有展示它时,我不想加载所有不同分辨率的图像。这会影响网站的性能。

1个回答

0

如果您通过JavaScript加载所需的图像,则可以使用jQuery,这将仅加载代码的特定部分,从而不影响网站的性能。

jQuery

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

mobile.js/ Desktop.js - 存储所有移动设备/桌面设备中用于显示的图像位置,如下所示

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>

祝您愉快 :)


这个技巧已经在 SO 上提到过,但你也知道它不利于性能。感谢您的反馈。 - Ritu
如果您可以使用JS加载图像,则可以使用jQuery仅加载代码的特定部分,从而不影响网站的性能。 - Karan Sethi
1
以上代码对我不起作用。附言:我不擅长JavaScript。 - Ritu

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接