点击过滤img类别

时间:2018-02-25 15:32:16

标签: javascript jquery

我遇到function的问题。

我有一个3 img classallphotologo的马赛克图片。 我希望在徽标链接上显示l click时,仅在我的马赛克中显示徽标img。

所有信息都在那里:

$(function() {
  var totalImg = $('#mosaic').find('img');
  $('#category').find('a').click(function(e) {
    e.preventDefault();
    var target = $(this).attr('class'),
      img = totalImg.attr('class');
    $('.activeCat').removeClass('activeCat');
    $(this).addClass('activeCat');
    target == img ? img.css('display', 'block') : img.css('display', 'none');
  });
});
* {
  padding: 0;
  margin: 0;
}

body {
  font: 1em 'Open Sans', sans-serif;
  background-color: #6e6e6e;
  color: #fff;
}

#category {
  margin: 3em auto 1.5em;
  display: -webkit-box;
  display: -ms-flexbox;
  display: flex;
  -ms-flex-pack: distribute;
  justify-content: space-around;
  width: 600px;
  list-style: none;
}

#category li a {
  opacity: 0.3;
  text-decoration: none;
  color: inherit;
  letter-spacing: 0.1em;
  font-size: 1.1em;
  -webkit-transition: all 0.3s ease-in-out;
  transition: all 0.3s ease-in-out;
}

#category li a.activeCat {
  opacity: 1;
}

#category li a:hover {
  cursor: pointer;
  opacity: 1;
}

#mosaic {
  -webkit-columns: 4;
  columns: 4;
  -webkit-column-gap: 0.5em;
  column-gap: 0.5em;
  width: 80%;
  margin: auto;
}

#mosaic li {
  list-style: none;
}

#mosaic img {
  width: 100%;
  -ms-flex-preferred-size: 25%;
  flex-basis: 25%;
  margin: 0.15em 0;
  -webkit-transition: all 0.5s ease-in-out;
  transition: all 0.5s ease-in-out;
}

#mosaic:hover img {
  cursor: pointer;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<ul id="category">
  <li><a class="activeCat all">All</a></li>
  <li><a class="logo">Logos</a></li>
  <li><a class="photo">Photos</a></li>
</ul>
<ul id="mosaic">
  <li><img class="all photo" src="https://picsum.photos/600/800" alt=""></li>
  <li><img class="all photo" src="https://picsum.photos/700/300" alt=""></li>
  <li><img class="all photo" src="https://picsum.photos/600/800" alt=""></li>
  <li><img class="all photo" src="https://picsum.photos/600/800" alt=""></li>
  <li><img class="all photo" src="https://picsum.photos/700/300" alt=""></li>
  <li><img class="all photo" src="https://picsum.photos/600/800" alt=""></li>
  <li><img class="all photo" src="https://picsum.photos/600/800" alt=""></li>
  <li><img class="all logo" src="http://lorempixel.com/900/500" alt=""></li>
  <li><img class="all logo" src="http://lorempixel.com/900/500" alt=""></li>
  <li><img class="all logo" src="http://lorempixel.com/700/900" alt=""></li>
  <li><img class="all logo" src="http://lorempixel.com/900/500" alt=""></li>
  <li><img class="all logo" src="http://lorempixel.com/400/200" alt=""></li>
  <li><img class="all logo" src="http://lorempixel.com/900/500" alt=""></li>
</ul>

感谢您的帮助。

1 个答案:

答案 0 :(得分:2)

您可以尝试此https://jsfiddle.net/6nfh128q/9/

$(function() {
    var  totalImg = $('#mosaic').find('img');
    $('#category').find('a').click(function(e) {
          e.preventDefault();
          var  target = $(this).attr('class'),
               img = totalImg.attr('class');
          $('.activeCat').removeClass('activeCat');
          $(totalImg).hide();
          $('.'+target).show();
          $(this).addClass('activeCat');            
     });
});