jQuery image Grid System

故事扮演 提交于 2019-12-05 10:46:50

Since you'll be creating the HTML dynamically you should remove the .photo-row container but keep .photo-item like so:

        <div class="photo-item">
            <a href="..."><img src="..." /></a>
        </div>
        <div class="photo-item">
            <a href="..."><img src="..." /></a>
        </div>
        <div class="photo-item">
            <a href="..."><img src="..." /></a>
        </div>
        ...

Then what you can do is wrap your elements with .photo-row on page load. First starting with various sets of two:

var imgGrab  = $('.photo-item'); //photos
var imgLength = imgGrab.length; //number of photos

for ( i=0; i<imgLength; i=i+3 ) {
   imgGrab.eq(i+1).add( imgGrab.eq(i+1) ).add( imgGrab.eq(i+2) ).wrapAll('<div class="photo-row"></div>'); //wrap photos
}

Then find the remanding ones and wrap those with .photo-row as well:

$(".photo-item").each(function(){
   if($(this).parent().is(":not(.photo-row)")){
     $(this).wrap('<div class="photo-row"></div>');
   }
});

This will wrap your images dynamically and let the CSS do its job regardless of the number of them:

CODEPEN

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!