html - Combining jQuery filtering and Bootstrap rows -
i have simple bootstrap grid this
<div class="container-fluid"> <div id="parent"> <div class="row row-striped"> <div class="category1 col-xs-12"> //of course more cols , contents in them </div> </div> </div> </div>
and put filter on buttons ids (thanks https://codepen.io/terf/post/jquery-filter-divs)
<button class="btn btn-link" id="category1">category 1</button>
but because need background on every second row, decided add new row jquery every 2 items. causes problem filter, because whenever filter, new row being kept (and empty).
this javascript part:
<script type="text/javascript"> function aligndivs() { var $mainelem = $('.row.row-striped'), $parent = $mainelem.parent(), // div#parent $items = $mainelem.children(':gt(1)').detach(); // detach +1 items if ($items.length) { (var = 0; < $items.length; = + 2) { // 2 items per row var $row = $('<div class="row row-striped">').append($items.slice(i, + 2)); // new row detached items $parent.append($row); } } } aligndivs(); //initially align cols/rows $(document).ready(function () { var $btns = $('.btn').click(function () { if (this.id === 'all') { $('#parent').find('> .row > div').fadein(450); aligndivs(); //reload col/row layout } else { var $el = $('.' + this.id).fadein(450); $('#parent').find('> .row > div').not($el).hide(); aligndivs(); //reload col/row layout } $btns.removeclass('active'); $(this).addclass('active'); }); }); </script>
i tried stuff .on('change'), or removing before-last row (.eq(-2)) doesn't work expect to. when remove before-last row example, still deletes 1 item inside instead of empty one.
what want have 2 items in row , open new one, , if 2 items exist after filter, delete new row between them 1 row 2 items again - , not 1 item per row.
here jsfiddle current code, can see problem if click on website or online-shop: https://jsfiddle.net/a3e7xmho/1/
finally found solution.
the trick set background dynamically jquery cols instead of row , check :visible-state.
function setbackgroundpositions() { var width = $('.row-striped').width(); $('.col-project:visible').each(function(i) { if (i % 4 >= 2) { $(this).css('background-image', 'none'); } else { $(this) .css('background-image', 'url("/img/projects/image.png")') .css('background-position', ((i % 4) * -1 * (width/2)) + "px 0") .css('background-size', 'cover'); } }); }
Comments
Post a Comment