{ filter: '*' }
{ sortBy: '' }
<script src="https://unpkg.com/isotope-layout@3/dist/isotope.pkgd.js"></script>
<div class="item-wrap"> // 预留辅助节点 <div class="item web h5"><img alt="" src="/utils/view/view0.jpg" /></div> <div class="item web css"><img alt="" src="/utils/view/view2.jpg" /></div> <div class="item web js"><img alt="" src="/utils/view/view3.jpg" /></div> <div class="item css"><img alt="" src="/utils/view/view4.jpg" /></div> <div class="item h5"><img alt="" src="/utils/view/view5.jpg" /></div> <div class="item js"><img alt="" src="/utils/view/view7.jpg" /></div> <div class="item css"><img alt="" src="/utils/view/view9.jpg" /></div> </div>
<div class="btns-wrap"> <button class="btn active" data-filter="*">web</button> <button class="btn" data-filter=".h5">html5</button> <button class="btn" data-filter=".css">css</button> <button class="btn" data-filter=".js">javascript</button> </div>
这里采用 fitRows,适合等高的元素项
const iso = new Isotope( '.item-wrap', { itemSelector: '.item', layoutMode: 'fitRows' });
可以为待选项指定多个样式类来均匀分布;如js有2项,可以占用48%
<div class="item item48 web js"><img alt="" src="/utils/view/view3.jpg" /></div>
let btns = document.querySelectorAll('.btn'); btns.forEach(btn => { btn.addEventListener('click', function (e) { console.log(e); let filter = e.target.getAttribute('data-filter'); iso.arrange({ filter: filter }); }) })
如果不指定间隔,默认情况下,各项挨在一起
为了分开,要计算:每个项的百分比和间隔 gutter
gutter默认单位是像素;可以指定一个预置类来使用百分比;同时显式的配置为使用百分比;调整配置如下
const iso = new Isotope( '.item-wrap', { itemSelector: '.item', layoutMode: 'fitRows', percentPosition: true, fitRows: { gutter: '.gutter-sizer' }, });
.gutter-sizer { width: 4%; }
为了应用分隔,待选项结构上要增加一个辅助子节点 - 重要
<div class="gutter-sizer"></div>
为了垂直方向分隔项目,各项指定一定量的padding;结合 gutter 和每行个数而定
.item { width: calc(97% / 4); aspect-ratio: 16/9; padding: 1%; } .gutter-sizer { width: 1%; }