更改具有不同宽度的项目

时间:2014-04-08 09:31:34

标签: javascript jquery html css media-queries

我有一个卡鲁塞尔(Sorgalla'卡鲁塞尔),上面有一系列物品。我希望如果窗口的宽度例如小于1024px,则它在滚动时定位3个项目,如果宽度小于640px则定位2个项目,如果宽度小于320px,则定位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'
        });


    }

});

1 个答案:

答案 0 :(得分:0)