我有一个砖石同位素网格,有n行,有两个列大小:160px到160px和320px到320px,我想为每行的第一个和最后一个元素分配不同的样式。我的行可以有4个元素到7个元素。我一直在努力解决这个问题,并想知道它是否可行。
HTML
<div id="grid" style="position: relative; overflow: hidden; height: 960px;"
class="isotope">
<div class="one_by_one">
<img class="thumb" src="https://s3.amazonaws.com/stitch-images/products/gucci.png"
/>
</div>
<div class="one_by_one">
<img class="thumb" src="https://s3.amazonaws.com/stitch-images/products/gucci.png"
/>
</div>
<div class="one_by_one">
<img class="thumb" src="https://s3.amazonaws.com/stitch-images/products/gucci.png"
/>
</div>
<div class="one_by_one">
<img class="thumb" src="https://s3.amazonaws.com/stitch-images/products/gucci.png"
/>
</div>
<div class="one_by_one">
<img class="thumb" src="https://s3.amazonaws.com/stitch-images/products/gucci.png"
/>
</div>
<div class="one_by_one">
<img class="thumb" src="https://s3.amazonaws.com/stitch-images/products/gucci.png"
/>
</div>
<div class="two_by_two">
<img class="thumb" src="https://s3.amazonaws.com/stitch-images/products/gucci.png"
/>
</div>
<div class="one_by_one">
<img class="thumb" src="https://s3.amazonaws.com/stitch-images/products/gucci.png"
/>
</div>
<div class="one_by_two">
<img class="thumb" src="https://s3.amazonaws.com/stitch-images/products/gucci.png"
/>
</div>
<div class="one_by_one">
<img class="thumb" src="https://s3.amazonaws.com/stitch-images/products/gucci.png"
/>
</div>
<div class="one_by_one">
<img class="thumb" src="https://s3.amazonaws.com/stitch-images/products/gucci.png"
/>
</div>
<div class="one_by_one">
<img class="thumb" src="https://s3.amazonaws.com/stitch-images/products/gucci.png"
/>
</div>
<div class="two_by_two">
<img class="thumb" src="https://s3.amazonaws.com/stitch-images/products/gucci.png"
/>
</div>
<div class="one_by_one">
<img class="thumb" src="https://s3.amazonaws.com/stitch-images/products/gucci.png"
/>
</div>
<div class="one_by_one">
<img class="thumb" src="https://s3.amazonaws.com/stitch-images/products/gucci.png"
/>
</div>
<div class="one_by_one">
<img class="thumb" src="https://s3.amazonaws.com/stitch-images/products/gucci.png"
/>
</div>
<div class="one_by_two">
<img class="thumb" src="https://s3.amazonaws.com/stitch-images/products/gucci.png"
/>
</div>
<div class="one_by_one">
<img class="thumb" src="https://s3.amazonaws.com/stitch-images/products/gucci.png"
/>
</div>
<div class="one_by_one">
<img class="thumb" src="https://s3.amazonaws.com/stitch-images/products/gucci.png"
/>
</div>
<div class="two_by_two">
<img class="thumb" src="https://s3.amazonaws.com/stitch-images/products/gucci.png"
/>
</div>
<div class="one_by_one">
<img class="thumb" src="https://s3.amazonaws.com/stitch-images/products/gucci.png"
/>
</div>
<div class="one_by_one">
<img class="thumb" src="https://s3.amazonaws.com/stitch-images/products/gucci.png"
/>
</div>
<div class="one_by_two">
<img class="thumb" src="https://s3.amazonaws.com/stitch-images/products/gucci.png"
/>
</div>
<div class="one_by_one">
<img class="thumb" src="https://s3.amazonaws.com/stitch-images/products/gucci.png"
/>
</div>
</div>
CSS
#grid {
margin:auto;
margin-top:55px;
margin-bottom:200px;
width:1140px
}
#grid .thumb {
width:97%;
height:97%
}
#grid .one_by_one {
width:160px;
height:160px;
background:url(https://s3.amazonaws.com/stitch-images/assets/cell_1x1.png);
cursor:pointer
}
#grid .one_by_two {
width:160px;
height:320px;
background:url(https://s3.amazonaws.com/stitch-images/assets/cell_1x2.png);
cursor:pointer
}
#grid .two_by_two {
width:320px;
height:320px;
background:url(https://s3.amazonaws.com/stitch-images/assets/cell_2x2.png);
cursor:pointer
}
JS
$("#grid").isotope masonry: layoutMode: 'fitRows'
查看我的Jsfiddle http://jsfiddle.net/TDma4/
答案 0 :(得分:11)
这是解决问题的一种方法。
工作解决方案: http://jsbin.com/ufuleb/21/
<强> CSS 强>
.first, .last {
border:2px dashed blue;
opacity:.75;
}
<强>的JavaScript 强>
$("#grid").isotope(
{ layoutMode : 'fitRows' });
// Last div element
$("#grid div:last-child").addClass("last");
var maxWidth = 0;
// Use max to handle last div
$("#grid div").each(function (i) {
matrix = matrixToArray($(this).css("-transform"));
// identify first elements
if ( parseInt(matrix[4],10) == 0 ) {
$(this).addClass("first");
}
// identify last elements
if ( parseInt(matrix[4],10) > parseInt(maxWidth,10) ) {
maxWidth = matrix[4];
} else {
$(this).prev().addClass("last");
maxWidth = 0;
}
});
// Util function for parsing -webkit-transform
function matrixToArray(matrix) {
return matrix.substr(7, matrix.length-8).split(', ');
}
循环显示每个div
并跟踪当前xT
值(CSS -webkit-transform)。每当传递最大值时,只需更新前一个值,该值应该是每行的最后一个元素。最后一个元素由:last-child
处理。 请注意,此解决方案还会处理整个#grid
宽度更改。
示例输出
这可能会进一步优化,但至少可以提供一个起点。
我从这个答案得到了一些帮助:https://stackoverflow.com/a/5968313/1085891
供参考:
答案 1 :(得分:7)
Isotope有一个itemPositionDataEnabled
选项,可以公开每个元素的位置。使用它和onLayout
处理程序,您可以计算每行的第一个和最后一个元素(demo):
$('#grid').isotope({
itemPositionDataEnabled: true,
onLayout: function (elems, instance) {
var items, rows, numRows, row, prev, i;
// gather info for each element
items = elems.map(function () {
var el = $(this), pos = el.data('isotope-item-position');
return {
x: pos.x,
y: pos.y,
w: el.width(),
h: el.height(),
el: el
};
});
// first pass to find the first and last items of each row
rows = [];
i = {};
items.each(function () {
var y = this.y, r = i[y];
if (!r) {
r = {
y: y,
first: null,
last: null
};
rows.push(r);
i[y] = r;
}
if (!r.first || this.x < r.first.x) {
r.first = this;
}
if (!r.last || this.x > r.last.x) {
r.last = this;
}
});
rows.sort(function (a, b) { return a.y - b.y; });
numRows = rows.length;
// compare items for each row against the previous row
for (prev = rows[0], i = 1; i < numRows; prev = row, i++) {
row = rows[i];
if (prev.first.x < row.first.x &&
prev.first.y + prev.first.h > row.y) {
row.first = prev.first;
}
if (prev.last.x + prev.last.w > row.last.x + row.last.w &&
prev.last.y + prev.last.h > row.y) {
row.last = prev.last;
}
}
// assign classes to first and last elements
elems.removeClass('first last');
$.each(rows, function () {
this.first.el.addClass('first');
this.last.el.addClass('last');
});
}
});
更新:基于JSuar反馈的固定算法
更新#2:修正了项目高于2行的问题
答案 2 :(得分:1)
您希望在.each块中执行的任何其他属性
_fitRowsLayout : function( $elems ) {
var instance = this,
containerWidth = this.element.width(),
props = this.fitRows;
$elems.each( function() {
var $this = $(this),
atomW = $this.outerWidth(true),
atomH = $this.outerHeight(true);
if ( props.x !== 0 && atomW + props.x > containerWidth ) {
// if this element cannot fit in the current row
props.x = 0;
props.y = props.height;
}
// position the atom
instance._pushPosition( $this, props.x, props.y );
props.height = Math.max( props.y + atomH, props.height );
props.x += atomW;
});
},
答案 3 :(得分:1)
使用这个: http://jsfiddle.net/86WVw/1/
修改强>: 同位素现在使用translate3d,编辑后使用translate n translate3d http://jsfiddle.net/86WVw/3/
额外的css:
[style*="translate(0px"], [style*="translate3d(0px"], .border { // .border is class for end of row item, you can change it to whatever you want
border: 3px dotted #f00;
}
additioan js:
gw = $('#grid').width(); // get grid width
w = $('#grid').width(); // to find smallest width, take a big number first
$('#grid>div').each(function(){ // iterate through tiles
if ($(this).width()<w) // if width smaller then assumed smallest width
w = $(this).width(); // change smallest width
});
var x;
for (x = w; x < gw; x += w) // iterate translated tiles from smallest width to grid width
{
var elms = $('[style*="translate3d('+x+'px"], [style*="translate('+x+'px"]');
$(elms).each(function(){
if ((x+$(this).width())>(gw-w)) // if tiles left + tile width reach end of grid
$(this).addClass('border'); // apply class
});
}
答案 4 :(得分:0)
问题比看起来简单得多。
盒子的大小是一个共同矩阵的倍数。
这是我的方法。
var columnsWidth = 100; // the base matrix width
var columns = 8; // the number of columns available (container.width / columnsWidth)
$.each(box, function() {
var box = $(this) // the current box
var boxSize = [2,2] // obtained subdividing size by matrix
var maximumXChord = (columns - boxSize[0]) * columnsWidth // the maximum X available
if (box.x == maximumXChord) { // check if the current X Chords is equal to the maximum
console.log('the box is the last of this row!')
} else if (box.x == 0) { // check if the the X is 0
console.log('the box is the first of this row!')
}
})
这就是全部!