同位素过滤按钮状态不起作用

时间:2017-01-21 08:27:08

标签: jquery html css jquery-isotope lightgallery

我正在创建一个投资组合库,我使用LightGallery和同位素来过滤catergories,但是当按下1个过滤器按钮时,我想要按下该按钮的活动状态,即" is-checked& #34;要添加到按钮的类。

jQuery代码如下。和我的CodePen

$('.button').each( function( i, buttonGroup ) {
   var $buttonGroup = $( buttonGroup );
   $buttonGroup.on( 'click', 'button', function() {
    $buttonGroup.find('.is-checked').removeClass('is-checked');
     $( this ).addClass('is-checked');
  });
});

请帮忙。谢谢。



$gallery = $('#gallery'); 

$gallery.lightGallery({
     mode: 'lg-fade',
     hash: true	,
     download: true,
     enableDrag: true,
     enableSwipe: true,
     thumbnail:true,
     animateThumb: true,
     showThumbByDefault: true
   });



//isotope Code
$('#gallery').isotope({
  // options
  itemSelector: '.revGallery-anchor',
  layoutmode: 'fitrows'
});
$('button').on( 'click', function() {
    var filterValue = $(this).attr('data-filter');
    $('#gallery').isotope({ filter: filterValue });
    $gallery.data('lightGallery').destroy(true);
    $gallery.lightGallery({
      selector: filterValue.replace('*','')
    });
});

//button active mode
$('.button').each( function( i, buttonGroup ) {
  var $buttonGroup = $( buttonGroup );
  $buttonGroup.on( 'click', 'button', function() {
    $buttonGroup.find('.is-checked').removeClass('is-checked');
    $( this ).addClass('is-checked');
  });
});






//CSS Gram Filters on Mouse enter
$("#gallery a .nak-gallery-poster").addClass("inkwell");

$("#gallery a").on({
  mouseenter : function() {
      $(this).find(".nak-gallery-poster").removeClass("inkwell").addClass("xpro2");
  },
  mouseleave : function() {
      $(this).find(".nak-gallery-poster").removeClass("xpro2").addClass("inkwell");
  }
}); 

.revGallery-anchor, .gal-overlay, .nak-gallery-poster{
  -webkit-box-sizing: border-box;
  -moz-box-sizing: border-box;
  -ms-box-sizing: border-box;
  box-sizing: border-box;
}


.revGallery-anchor{
  overflow: hidden;
  position: relative;
  width: 25%;
  display: block;
  float: left;
  border: 5px solid #e9e9e9;
}

.gal-overlay{
  display: block;
  width: 100%;
  height: 100%;
  background: rgba(27,27,27, 0.6);
  position: absolute;
  top: 0;
  left: 0;
  transition: background .4s ease;
  -webkit-transition: background .4s ease;
}
		
.revGallery-anchor:hover .gal-overlay{
  background: rgba(27,27,27, 0);
}

.nak-gallery {
  display: block;
  width: 100%;
  position: relative;
  margin-top: 50px;
  -webkit-box-sizing: border-box;
  -moz-box-sizing: border-box;
  -ms-box-sizing: border-box;
  box-sizing: border-box;
}

.nak-gallery-poster{
  padding-bottom:100%;
  transform-origin: 50% 50%;
  -webkit-transform-origin: 50% 50%;
  -ms-transform-origin: 50% 50%;
  transform: scale(1, 1);
  -webkit-transform: scale(1, 1);
  -ms-transform: scale(1, 1);
  transition: all .4s ease;
  -webkit-transition: all .4s ease;
}

.revGallery-anchor:hover .nak-gallery-poster{
  transform: scale(1.1, 1.1);
  -webkit-transform: scale(1.1, 1.1);
  -ms-transform: scale(1.1, 1.1);
}

.img-responsive{
  display:none;
}

.button{
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;

  -webkit-transition: all .4s ease;
  -moz-transition: all .4s ease;
  -o-transition: all .4s ease;
  transition: all .4s ease;
  width: 200px;
  height: 48px;
  border: 1px solid rgba(0,169,157,1);
  background-color: rgba(0,169,157,1);
  border-radius: 2px;
  color: #fff;
  letter-spacing: 2px;
}

.button:hover {
  background-color: #363636;
  text-shadow: 0 1px hsla(0, 0%, 100%, 0.5);
  color: #fff;
}

.button:active,
.button.is-checked {
  background-color: #28F;
}

.button.is-checked {
  color: white;
  text-shadow: 0 -1px hsla(0, 0%, 0%, 0.8);
}

.button:active {
  box-shadow: inset 0 1px 10px hsla(0, 0%, 0%, 0.8);
}

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button type="button" class="button is-checked" data-filter="*">ALL</button>
<button type="button" class="button" data-filter=".design">DESIGN</button>
<button type="button" class="button" data-filter=".branding">BRANDING</button>

<div class="nak-gallery nlg1" id="gallery">
  <a href="http://unsplash.com/photos/GYNxcQvBNzA/download" class="revGallery-anchor design">
    <img class="img-responsive" src="http://unsplash.com/photos/GYNxcQvBNzA/download">
    <div style="overflow:hidden">
      <div class="nak-gallery-poster" style="background-image:url('http://unsplash.com/photos/GYNxcQvBNzA/download');background-size:cover;background-repeat:no-repeat;background-position:center center;display: block;width: 100%;height: 0;"></div>
    </div>
    <div class="gal-overlay">
      <div class="photo"></div>
    </div>
  </a>	
  <a href="http://unsplash.com/photos/ssAcdlJRsI4/download" class="revGallery-anchor branding">
    <img class="img-responsive" src="http://unsplash.com/photos/ssAcdlJRsI4/download">
    <div style="overflow:hidden">
      <div class="nak-gallery-poster" style="background-image:url('http://unsplash.com/photos/ssAcdlJRsI4/download');background-size:cover;background-repeat:no-repeat;background-position:center center;display: block;width: 100%;height: 0;"></div>
    </div>
    <div class="gal-overlay">
      <div class="photo"></div>
    </div>
  </a>	
  <a href="http://unsplash.com/photos/EKIyHUrUHWU/download" class="revGallery-anchor design">
    <img class="img-responsive" src="http://unsplash.com/photos/EKIyHUrUHWU/download">
    <div style="overflow:hidden">
      <div class="nak-gallery-poster" style="background-image:url('http://unsplash.com/photos/EKIyHUrUHWU/download');background-size:cover;background-repeat:no-repeat;background-position:center center;display: block;width: 100%;height: 0;"></div>
    </div>
    <div class="gal-overlay">
      <div class="photo"></div>
    </div>
  </a>	
  <a href="http://unsplash.com/photos/CFi7_hCXecU/download" class="revGallery-anchor branding">
    <img class="img-responsive" src="http://unsplash.com/photos/CFi7_hCXecU/download">
    <div style="overflow:hidden">
      <div class="nak-gallery-poster img-responsive" style="background-image:url('http://unsplash.com/photos/CFi7_hCXecU/download');background-size:cover;background-repeat:no-repeat;background-position:center center;display: block;width: 100%;height: 0;"></div>
    </div>
    <div class="gal-overlay">
      <div class="photo"></div>
    </div>
  </a>	
</div>
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:2)

你可以试试这个:codepen demo http://codepen.io/anon/pen/bgWNpZ

$('.button').click(function(){
    $('.button').removeClass('is-checked');
    $(this).addClass('is-checked');
});