Подтвердить что ты не робот

Есть ли способ нацеливать первый и последний элемент в каждой строке с использованием изотопа jQuery?

У меня есть изотопная сетка из кладки, у которой есть 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/

4b9b3361

Ответ 1

Вот один из способов подойти к проблеме.

Рабочее решение: 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.

Результат

Example output

Вероятно, это может быть оптимизировано, но по крайней мере обеспечивает отправную точку.

Я получил некоторую помощь от этого ответа: fooobar.com/questions/173861/...

Для справки:

Ответ 2

Изотоп имеет параметр itemPositionDataEnabled, который раскрывает положение каждого элемента. Используя это, наряду с onLayout обработчик, вы можете вычислить первый и последний элементы каждой строки (демо):

$('#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: Исправлена ​​ошибка, когда элементы были выше двух строк

Ответ 3

Майк, Если вы чувствуете себя авантюристом, вы можете расширить Isotop, чтобы сделать это. Я вытащил из http://isotope.metafizzy.co/docs/extending-isotope.html

Любые дополнительные свойства, которые вы хотите сделать, вы можете сделать в блоке .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;

  });
},

Ответ 4

используйте это: http://jsfiddle.net/86WVw/1/

Edit: Изотоп теперь использует 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
    });
}

Ответ 5

Вопрос намного проще, чем кажется. Размер окна кратен общей матрице.

Это мой подход.

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!') 
    }
})

Что все!