2012-03-15 5 views
0

ので、私はjqueryの同位体で遊んで一つのボタンに二つのオプションを組み合わせることしようとしているjqueryの同位体

私は結果をフィルタリングのリンクのいずれかのセットを持っています、リンクの他のセットがレイアウトモードを変更します。私は、フィルタリングだけでなく、レイアウトを変更するリンクをご希望です。

私がデフォルトから変更しているレイアウトモードであるspine documentationがあります。

私は問題がdata-option-keyにあると思います。フィルタに設定され、もう一方はlayoutModeに設定されているので、異なる種類のdata-option-keyを持つ2つの異なるdata-option-内に?ここ

は、スクリプトが続くHTMLです: 私もjqueryのを使用するのではなく、別のアプローチをしようとしてきた:

<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> 

は右方向のどのnudgesに

おかげ

編集に感謝しますリンクがクリックされたときにコンテナのサイズを変更します。これには独自の問題があります。私はそれをサイズ変更することができますが、フィルターが変わったら、コンテナ内の画像を保持しません。代わりに、再度クリックして内側に収まるように、新たに形成された小さなコンテナの外に置きます。とても近い!

私はこのためにしようとしていたコード:

// 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'); 
    } 

答えて

0

それはlooooong時間後に働いてしまいました。

私のオプションはこのように見てしまったので、私は、各リンクにおける2つの異なる値を許可するように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!

関連する問題