2012-03-15 7 views
0

그래서 나는 JQuery와 동위 원소와 장난 하나 개의 버튼으로 두 가지 옵션을 결합하기 위해 노력하고있어JQuery와 동위 원소

나는 결과를 필터링 링크 중 하나 개 세트가 , 다른 링크 세트가 레이아웃 모드를 변경합니다. 필터링 링크뿐만 아니라 레이아웃을 변경하려면 필터 링크 싶습니다.

저는 기본적으로 변경하려는 레이아웃 모드 인 spine documentation입니다.

나는 data-option-key에 문제가 있다고 생각하는데, 하나는 필터로, 다른 하나는 layoutMode로 설정되어 있기 때문에 다른 종류의 data-option-key를 두 가지 데이터 옵션 값으로 가질 수있다. 이내에? 여기

스크립트 다음에 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'); 
    } 

답변

0

는 looooong 시간 이후에 작업을 얻었다. 그때라고 모두 같은 스크립트에서 다른 하나 값

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

: I는 각 링크에서 두 개의 서로 다른 값을 허용하는 JSON을 사용하여 관심이 누구를위한

그래서 내 옵션은 다음과 같이보고 결국 :

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

관련 문제