【问题标题】:Changing items target with different width更改具有不同宽度的项目目标
【发布时间】:2015-08-20 06:30:32
【问题描述】:

我有一个带有物品清单的旋转木马(Sorgalla's Carrousel)。我希望,例如,如果窗口的宽度小于 1024 像素,则它针对滚动时的 3 个项目,如果宽度小于 640 像素,则针对 2 个项目,如果宽度小于 320 像素,则针对 1 个项目。

我尝试使用此代码,但没有成功(每次调整大小都需要重新加载功能,以便 carrrousel 使用新的滚动选项重新加载):

HTML代码是:

<div class="box1"><a href="#addimg" class="plus2"></a><div id="addimg" style="width: 17.4%;height: 25px;"  class="white-popup mfp-hide">
       <input type="button" id="baddimg" style="float: left;" value="Añadir imágenes" onClick="addInput('dynamicInput');" />
       <div id="loading"></div>
</div><a href="#" class="jcarouselimgs-control-next arrowimg-next"></a><a href="#" class="jcarouselimgs-control-prev arrowimg-prev"></a><h2 class="infotitle">OUR PORTFOLIO</h2>
 <div class="jcarouselimgs"  data-jcarousel="true">
                    <ul  class="popupimg">
                        <li><a href="Imagenes/img1.png" class="image-link"><img src="Imagenes/img1.png" alt="Img1" title="Img1"></a></li>
                        <li><a href="Imagenes/img2.png" class="image-link"><img src="Imagenes/img2.png" alt="Img2" title="Img2"></a></li>
                        <li><a href="Imagenes/img3.png" class="image-link"><img src="Imagenes/img3.png" alt="Img3" title="Img3"></a></li>
                        <li><a href="Imagenes/img4.jpg" class="image-link"><img src="Imagenes/img4.jpg" alt="Img4" title="Img4"></a></li>
                        <li><a href="Imagenes/img5.jpg" class="image-link"><img src="Imagenes/img5.jpg" alt="Img5" title="Img5"></a></li>
                        <li><a href="Imagenes/img6.jpg" class="image-link"><img src="Imagenes/img6.jpg" alt="Img6" title="Img6"></a></li>

                    </ul>
              </div>
        </div>

还有 JQuery 代码,我尝试了两种方式,这个:

$(window).resize(function(){  
  if ($(".sampleClass").css("float") == "none" ){

         $('.jcarouselimgs').jcarousel('reload', {
           animation: 'fast'
        });
        $('.jcarouselimgs').jcarousel({
            // Core configuration goes here
        });

        $('.arrowimg-prev').jcarouselControl({
            target: '-=1'
        });

        $('.arrowimg-next').jcarouselControl({
            target: '+=1'
        });


    // your code here
  }
}); 

$(window).resize(function(){  
  if ($(".sampleClass2").css("float") == "none" ){

         $('.jcarouselimgs').jcarousel('reload', {
           animation: 'fast'
        });
        $('.jcarouselimgs').jcarousel({
            // Core configuration goes here
        });

        $('.arrowimg-prev').jcarouselControl({
            target: '-=3'
        });

        $('.arrowimg-next').jcarouselControl({
            target: '+=3'
        });


    // your code here
  }
}); 

管理一个示例 CSS 类来设置宽度:

.sampleClass {float:left;}
@media only screen and (max-width: 350px){
    .sampleClass {float:none;}

}
.sampleClass2 {float:left;}
@media only screen and (max-width: 1024px){
    .sampleClass2 {float:none;}

我也尝试过这种方式,都没有结果:

(function () {
    if ($(window).width() < 300) {
         $('.jcarouselimgs').jcarousel('reload', {
           animation: 'fast'
        });
        $('.jcarouselimgs').jcarousel({
            // Core configuration goes here
        });

        $('.arrowimg-prev').jcarouselControl({
            target: '-=1'
        });

        $('.arrowimg-next').jcarouselControl({
            target: '+=1'
        });
         $('h1').addClass('blue'); 

    }

     if ($(window).width() < 640) {
         $('.jcarouselimgs').jcarousel('reload', {
           animation: 'slow'
        });
        $('.jcarouselimgs').jcarousel({
            // Core configuration goes here
        });

        $('.arrowimg-prev').jcarouselControl({
            target: '-=2'
        });

        $('.arrowimg-next').jcarouselControl({
            target: '+=2'
        });


    }
         if ($(window).width() > 1024) {
              $('.jcarouselimgs').jcarousel('reload', {
           animation: 'fast'
        });

        $('.jcarouselimgs').jcarousel({
            // Core configuration goes here
        });

        $('.arrowimg-prev').jcarouselControl({
            target: '-=3'
        });

        $('.arrowimg-next').jcarouselControl({
            target: '+=3'
        });


    }

});

【问题讨论】:

    标签: javascript jquery html css media-queries


    【解决方案1】:

    使用这个旋转木马,更好:http://jssor.com/demos/introduction-slider.html

    【讨论】:

      猜你喜欢
      • 2014-04-02
      • 1970-01-01
      • 2013-09-20
      • 2018-10-08
      • 1970-01-01
      • 1970-01-01
      • 2014-02-16
      • 1970-01-01
      • 2019-01-12
      相关资源
      最近更新 更多