列表篩選功能

透過data-rel經鬆實現縮圖列表篩選功能 JQuery Simple Portfolio Filter

這個功能在許多版型裡面很常見,最常用在作品集裡面,長長的縮圖列表透過篩選器挑出分類裡的項目來顯示。這個寫法是我所接觸過的範例裡面覺得最容易容理解的,JS的部份也寫的很精簡,很實用很好做應用

來源:Simple Portfolio Filter-codepen

HTML結構:
按鈕的部份透過data-rel做為篩選的filter,縮圖的部份透過class的部份做分類

<div class="toolbar mb2 mt2">
  <button class="btn fil-cat" href="" data-rel="all">All</button>
  <button class="btn fil-cat" data-rel="web">Websites</button>
  <button class="btn fil-cat" data-rel="flyers">Flyers</button>
  <button class="btn fil-cat" data-rel="bcards">Business Cards</button>
</div> 
 
<div style="clear:both;"></div>   
<div id="portfolio">
  <div class="tile scale-anm web all">
        <img src="http://demo.themerain.com/charm/wp-content/uploads/2015/04/2-mon_1092-300x234.jpg" alt="" />
  </div>
  <div class="tile scale-anm bcards all">
    <img src="http://demo.themerain.com/charm/wp-content/uploads/2015/04/jti-icons_08-300x172.jpg" alt="" />
  </div>
  <div class="tile scale-anm web all">
    <img src="http://demo.themerain.com/charm/wp-content/uploads/2015/04/emi_haze-300x201.jpg" alt="" />
  </div>
  <div class="tile scale-anm web all">
            <img src="http://demo.themerain.com/charm/wp-content/uploads/2015/04/codystretch-300x270.jpg" alt="" />
  </div>
  <div class="tile scale-anm flyers all">
        <img src="https://placeholdit.imgix.net/~text?txtsize=33&txt=350%C3%97190&w=350&h=190" alt="" />
  </div>
  <div class="tile scale-anm bcards all">
            <img src="https://placeholdit.imgix.net/~text?txtsize=19&txt=200%C3%97290&w=200&h=290" alt="" />
  </div>
  <div class="tile scale-anm flyers all">
    <img src="http://demo.themerain.com/charm/wp-content/uploads/2015/04/jti-icons_08-300x172.jpg" alt="" />
  </div>
  <div class="tile scale-anm flyers all">
    <img src="http://demo.themerain.com/charm/wp-content/uploads/2015/04/transmission_01-300x300.jpg" alt="" />
  </div>
  <div class="tile scale-anm web all">
        <img src="https://placeholdit.imgix.net/~text?txtsize=19&txt=200%C3%97290&w=200&h=290" alt="" />
  </div>
  <div class="tile scale-anm flyers all">
           <img src="https://placeholdit.imgix.net/~text?txtsize=19&txt=200%C3%97290&w=200&h=290" alt="" /> 
  </div>
  <div class="tile scale-anm web all">
        <img src="http://demo.themerain.com/charm/wp-content/uploads/2015/04/the-ninetys-brand_02-300x300.jpg" alt="" />
  </div>
  <div class="tile scale-anm bcards all">
            <img src="http://demo.themerain.com/charm/wp-content/uploads/2015/04/15-dia_1092-1-300x300.jpg" alt="" />
  </div>
  <div class="tile scale-anm web all">
       <img src="https://placeholdit.imgix.net/~text?txtsize=33&txt=350%C3%97190&w=350&h=190" alt="" /> 
  </div>
  <div class="tile scale-anm bcards all">
          <img src="http://demo.themerain.com/charm/wp-content/uploads/2015/04/emi_haze-300x201.jpg" alt="" />  
  </div>
  <div class="tile scale-anm web all">
            <img src="http://demo.themerain.com/charm/wp-content/uploads/2015/04/transmission_01-300x300.jpg" alt="" />
  </div> 
  <div class="tile scale-anm web all">
      <img src="https://placeholdit.imgix.net/~text?txtsize=33&txt=350%C3%97190&w=350&h=190" alt="" />  
  </div> 
  <div class="tile scale-anm bcards all">     
            <img src="https://placeholdit.imgix.net/~text?txtsize=19&txt=200%C3%97290&w=200&h=290" alt="" />
  </div>
</div>

<div style="clear:both;"></div>   

CSS的部份:
沒什麼需注意的重點

body{
  max-width: 900px;
  float: none;
  margin: auto; 
}
#portfolio {  
    margin: 1rem 0;
    -webkit-column-count: 3; 
    -moz-column-count: 3;
    column-count: 3;
    -webkit-column-gap: 1rem;
    -moz-column-gap: 1rem;
    column-gap: 1rem;
    -webkit-column-width: 33.33333333333333%;
    -moz-column-width: 33.33333333333333%;
    column-width: 33.33333333333333%;
}
.tile { 
    -webkit-transform: scale(0);
    transform: scale(0);
    -webkit-transition: all 350ms ease;
    transition: all 350ms ease;
}
.tile:hover { 
}
.scale-anm {
  transform: scale(1);
}
p{ 
  padding:10px; 
  border-bottom: 1px #ccc dotted; 
  text-decoration: none; 
  font-family: lato; 
  text-transform:uppercase; 
  font-size: 12px; 
  color: #333; 
  display:block; 
  float:left;
}
p:hover { 
  cursor:pointer; 
  background: #333; 
  color:#eee; 
}
.tile img {
    max-width: 100%;
    width: 100%;
    height: auto;
    margin-bottom: 1rem;
  
}
.btn {
    font-family: Lato;
    font-size: 1rem;
    font-weight: normal;
    text-decoration: none;
    cursor: pointer;
    display: inline-block;
    line-height: normal;
    padding: .5rem 1rem;
    margin: 0;
    height: auto;
    border: 1px solid;
    vertical-align: middle;
    -webkit-appearance: none;
    color: #555;
    background-color: rgba(0, 0, 0, 0);
}
.btn:hover {
  text-decoration: none;
}
.btn:focus {
  outline: none;
  border-color: var(--darken-2);
  box-shadow: 0 0 0 3px var(--darken-3);
}
::-moz-focus-inner {
  border: 0;
  padding: 0;
}

JS部份:
要注意第6行的地方,這裡用了div作為選擇器,但如果我們的縮圖item包了好幾個div就可能會出錯,這邊要注意命名

$(function() {
        var selectedClass = "";
        $(".fil-cat").click(function(){ 
        selectedClass = $(this).attr("data-rel"); 
     $("#portfolio").fadeTo(100, 0.1);
        $("#portfolio div").not("."+selectedClass).fadeOut().removeClass('scale-anm');
    setTimeout(function() {
      $("."+selectedClass).fadeIn().addClass('scale-anm');
      $("#portfolio").fadeTo(300, 1);
    }, 300); 
        
    });
});

實作:﹙跟上述案例無關﹚

來源範例的篩選按鈕做的比較簡易,如果再加入下面這個功能,效果會更好:
簡易的JQuery應用,新增/移除class




 迴響列表

目前還沒有任何留言


 留言表單

如果想要使用回覆功能,或是刪除修改自己的留言,可以登入網站後再來留言。

🧨 Method Illuminate\View\View::__toString() must not throw an exception, caught ParseError: syntax error, unexpected 'array' (T_ARRAY), expecting function (T_FUNCTION) or const (T_CONST)