开发者

jQuery Scrollable Dynamic

开发者 https://www.devze.com 2022-12-31 13:02 出处:网络
I am doing a site where I need to change dynamically the amount of items in one slide depending on the resolution. I\'m using the Jquery Tools scrollable

I am doing a site where I need to change dynamically the amount of items in one slide depending on the resolution. I'm using the Jquery Tools scrollable

For better understanding, here is the basic markup

<div class="scrollable">   

   <!-- root element for the items -->
   <div class="items">

      <!-- 1-5 -->
      <div>
         <img src="http://farm1.static.flickr.com/143/321464099_a7cfcb95cf_t.jpg" />
         <img src="http://farm4.static.flickr.com/3089/2796719087_c3ee89a730_t.jpg" />
         <img src="http://farm1.static.flickr.com/79/244441862_08ec9b6b49_t.jpg" />
         <img src="http://farm1.static.flickr.com/28/66523124_b468cf4978_t.jpg" />
         <img src="http://farm1.static.flickr.com/164/399223606_b875ddf797_t.jpg" />
      </div>

      <!-- 5-10 -->
      <div>
         <img src="http://farm1.static.flickr.com/163/399223609_db47d35b7c_t.jpg" />
         <img src="http://farm1.static.flickr.com/135/321464104_c010dbf34c_t.jpg" />
         <img src="http://farm1.static.flickr.com/40/117346184_9760f3aabc_t.jpg" />
         <img src="http://farm1.static.flickr.com/153/399232237_6928a527c1_t.jpg" />
         <img src="http://farm1.static.flickr.com/50/117346182_1fded507fa_t.jpg" />
      </div>

      <!-- 10-15 -->
      <div>
         <img src="http://farm4.static.flickr.com/3629/3323896446_3b87a8bf75_t.jpg" />
         <img src="http://farm4.static.flickr.com/3023/3323897466_e61624f6de_t.jpg" />
         <img src="http://farm4.static.flickr.com/3650/3323058611_d35c894fab_t.jpg" />
         <img src="http://farm4.static.flickr.com/3635/3323893254_3183671257_t.jpg" />
         <img src="http://farm4.static.flickr.com/3624/3323893148_8318838fbd_t.jpg" />
      </div>

   </div>

</div>

Ok and now I would like to set, that if I have a resolution bellow 1440, I would show only e.g. 3 images

<div class="scrollable">   
       <!-- root element for the items -->    <div class="items">

      <!-- 1-3 -->
      <div>
         <img src="http://farm1.static.flickr.com/143/321464099_a7cfcb95cf_t.jpg" />
         <img src="http://farm4.static.flickr.com/3089/2796719087_c3ee89a730_t.jpg" />
         <img src="http://farm1.static.flickr.com/79/244441862_08ec9b6b49_t.jpg" />
      </div>

      <!-- 3-6 -->
      <div>
         <img src="http://farm1.static.flickr.com/163/399223609_db47d35b7c_t.jpg" />
         <img src="http://farm1.static.flickr.com/135/321464104_c010dbf34c_t.jpg" />
         <img src="http://farm1.static.flickr.com/40/117346184_9760f3aabc_t.jpg" />
      </div>

     ..etc 
          </div>
    </div>

开发者_StackOverflowI know that I should use the screen.width(); function but how to slice and parse it depending on the resolution?

Thanks for your comments!


You can use the JQuery's :gt selector here:

$(function(){
 if (screen.width() < 1440)
 {
  $('div.scrollable img:gt(2)').hide(); // hide other images and show only three
 }
 // you can setup similar if conditions for other cases.
});


if that is your only rule, then you would want to set:

​$(document).ready(function() {
    if (screen.width < 1440){
        var i = $("img").clone();
        $('.items div').remove();
        for (var a = 0; a <= i.length/3; a++){
            var d = $('<div></div>');
            d.append(i.slice(a * 3 - 3, a * 3));
            $('.items').append(d);
        }
    }
});​

I think this fixes the boat. http://jsfiddle.net/NBf2u/ play space provided.

0

精彩评论

暂无评论...
验证码 换一张
取 消