jquery-isotope如何使用一个链接来执行过滤器和布局功能

时间:2012-03-15 22:54:19

标签: jquery jquery-isotope

所以我正在玩jquery同位素并尝试将两个选项组合成一个按钮

我有一组过滤结果的链接,另一组链接改变了布局模式。我想过滤链接以更改布局以及过滤。

继承人spine documentation这是我从默认情况下改变的布局模式。

我认为问题出在data-option-key上,因为一个设置为filter,另一个设置为layoutMode,是否可以有两个不同的data-option-key,其中包含不同类型的data-option-values ?

这里是脚本后面的html:

<section id="options">    
    <ul id="filters" class="option-set clearfix" data-option-key="filter">
        <li><a href="#filter" data-option-value="*:not(.before)" class="selected">everything</a></li>
        <li><a href="#filter" data-option-value=".modern">modern</a></li>
        <li><a href="#filter" data-option-value=".traditional">traditional</a></li>
        <li><a href="#filter" data-option-value=".commercial">commercial</a></li>
        <li><a href="#filter" data-option-value=".automotive">automotive</a></li>
        <li><a href="#filter" data-option-value=".bespoke">bespoke</a></li>
        <li><a href="#filter" data-option-value=".before, .after">before <span style="font-size:10px;">&</span> after</a></li>
    </ul>

    <ul id="layouts" class="option-set clearfix" data-option-key="layoutMode">
        <li><a href="#filter" data-option-value="masonry" class="selected">normal</a></li>
        <li><a href="#filter" data-option-value="spineAlign">spine</a></li>
    </ul>
</section> <!-- # O P T I O N S -->


<div id="result"></div>
<div id="container" class="clickable clearfix">
    <div class="element portrait before">
        <a href="img/gallery/large/beforeandafter/image1_before.jpg" rel="shadowbox[traditional]"><img src="img/gallery/thumbs/beforeandafter/image1_before.jpg" alt="chair" /></a>
    </div>
    <div class="element portrait after bespoke">
        <a href="img/gallery/large/beforeandafter/image1_after.jpg" rel="shadowbox[traditional]"><img src="img/gallery/thumbs/beforeandafter/image1_after.jpg" alt="chair" /></a>
    </div>
    <div class="element portrait modern">
        <a href="img/gallery/large/modern/image1.jpg" rel="shadowbox[traditional]"><img src="img/gallery/thumbs/modern/image1.jpg" alt="chair" /></a>
    </div>
    <div class="element landscape modern">
        <a href="img/gallery/large/modern/image2.jpg" rel="shadowbox[traditional]"><img src="img/gallery/thumbs/modern/image2.jpg" alt="chair" /></a>
    </div>
</div> <!-- # C O N T A I N E R -->


    <script>
    $(function(){

    var $container = $('#container');

    $container.isotope({
        itemSelector : '.element',
        filter: '*:not(.before)'
    });

    // custom layout mode spineAlign
    $.Isotope.prototype._spineAlignReset = function() {
        this.spineAlign = {
            colA: 0,
            colB: 1
        };
    };

    $.Isotope.prototype._spineAlignLayout = function( $elems ) {
        var instance = this,
        props = this.spineAlign,
        gutterWidth = Math.round( this.options.spineAlign && this.options.spineAlign.gutterWidth ) || 0,
        centerX = Math.round(this.element.width() / 2);

        $elems.each(function(){
            var $this = $(this),
            isColA = props.colB > props.colA,
            x = isColA ?
            centerX - ( $this.outerWidth(true) + gutterWidth / 2 ) : // left side
            centerX + gutterWidth / 2, // right side
            y = isColA ? props.colA : props.colB;
            instance._pushPosition( $this, x, y );
            props[( isColA ? 'colA' : 'colB' )] += $this.outerHeight(true);
        });
    };

    $.Isotope.prototype._spineAlignGetContainerSize = function() {
        var size = {};
        size.height = this.spineAlign[( this.spineAlign.colB > this.spineAlign.colA ? 'colB' : 'colA' )];
        return size;
    };

    $.Isotope.prototype._spineAlignResizeChanged = function() {
        return true;
    };  

    $(function(){

        var $container = $('#container');

        $container.isotope({
            itemSelector : '.element',
            layoutMode: 'spineAlign',
            spineAlign: {
                gutterWidth: 20
            }
        });


        var $optionSets = $('#options .option-set'),
        $optionLinks = $optionSets.find('a');

        $optionLinks.click(function(){
            var $this = $(this);
            // don't proceed if already selected
            if ( $this.hasClass('selected') ) {
                return false;
            }
            var $optionSet = $this.parents('.option-set');
            $optionSet.find('.selected').removeClass('selected');
            $this.addClass('selected');

            // make option object dynamically, i.e. { filter: '.my-filter-class' }
            var options = {},
            key = $optionSet.attr('data-option-key'),
            value = $this.attr('data-option-value');
            // parse 'false' as false boolean
            value = value === 'false' ? false : value;
            options[ key ] = value;
            if ( key === 'layoutMode' && typeof changeLayoutMode === 'function' ) {
            // changes in layout modes need extra logic
                changeLayoutMode( $this, options )
            } 
            else {
                // otherwise, apply new options
                $container.isotope( options );
            }

            return false;
        });

    });

    // filter buttons for shadowbox
    $('#filters a').click(function(){
        var selector = $(this).attr('data-option-value');
        $container.isotope({ filter: selector });

        // don't proceed if no Shadowbox yet
        if ( !Shadowbox ) {
            return false;
        }

        Shadowbox.clearCache();

        $container.data('isotope').$filteredAtoms.each(function(){
            Shadowbox.addCache( $(this).find('a[rel^="shadowbox"]')[0] );
        });

        return false;
    }); 


});
    </script>

欣赏正确方向的任何推动

感谢

编辑: 我也一直在尝试另一种方法,即在单击链接时使用jquery调整容器大小。但这有其自身的问题。我可以让它调整大小,但是当过滤器更改时,它不会将图像保留在容器内,而是将它们留在新形成的小容器之外,直到再次单击然后将它们重新定位在内部以适合。太近了!

我正在尝试的代码:

    // change the width of the container depending on the filter
    if ( $this.hasClass('thin') ) {
        $container
        .css({'width': '636px','margin-left': '132px'})
        .isotope('reLayout');
    }
    if ( $this.hasClass('thick') ) {
        $container
        .css({'width': '960px','margin-left': '0'})
        .isotope('reLayout');
    }

1 个答案:

答案 0 :(得分:0)

在漫长的时间之后让它工作。

对于任何有兴趣的人我使用JSON在每个链接中允许两个不同的值,所以我的选项最终看起来像这样:

<section id="options">    
    <ul id="filters" class="option-set clearfix" data-option-key='["filter", "layoutMode"]'>
        <li><a href="#filter" data-option-value='["*:not(.before)", "fitRows"]' class="selected">everything</a></li>
        <li><a href="#filter" data-option-value='[".modern", "fitRows"]'>modern</a></li>
        <li><a href="#filter" data-option-value='[".traditional", "fitRows"]'>traditional</a></li>
        <li><a href="#filter" data-option-value='[".commercial", "fitRows"]'>commercial</a></li>
        <li><a href="#filter" data-option-value='[".automotive", "fitRows"]'>automotive</a></li>
        <li><a href="#filter" data-option-value='[".bespoke", "fitRows"]'>bespoke</a></li>
        <li><a href="#filter" data-option-value='[".before, .after", "spineAlign"]'>before <span style="font-size:10px;">&</span> after</a></li>
    </ul>
</section> <!-- # O P T I O N S -->
然后我在脚本中一个接一个地调用这两个值,如:

// data strings which JSON will call to get attribute
dataValue = $this.attr('data-option-value');
dataKey = $optionSet.attr('data-option-key');

// C H A N G E   T H E   F I L T E R   O N   C L I C K
var filterOptions = {},
    key = ($.parseJSON(dataKey)[0]),
    value = ($.parseJSON(dataValue)[0]);

// parse 'false' as false boolean
value = value === 'false' ? false : value;
filterOptions[ key ] = value;
if ( key === 'layoutMode' && typeof changeLayoutMode === 'function' ) {
  // changes in layout modes need extra logic
  changeLayoutMode( $this, filterOptions )
} 
else {
  // otherwise, apply new options
  $container.isotope( filterOptions )
}


// C H A N G E   T H E   L A Y O U T   O N   C L I C K                  
var layoutOptions = {},
    key = ($.parseJSON(dataKey)[1]),
    value = ($.parseJSON(dataValue)[1]);

// parse 'false' as false boolean
value = value === 'false' ? false : value;
layoutOptions[ key ] = value;
if ( key === 'layoutMode' && typeof changeLayoutMode === 'function' ) {
  // changes in layout modes need extra logic
  changeLayoutMode( $this, layoutOptions )
} 
else {
  // otherwise, apply new options
  $container.isotope( layoutOptions )
}

我仍然是jquery的新手,所以它可能不是最好的方式,但它有效,WAHOO!