2017-02-27 58 views
0

我在cycle2 carousel插件的幫助下創建了一個畫廊,我需要一個類別,這就是爲什麼我在我的旋轉木馬上寫ul.li元素,當我點擊一個類別時,相關圖片必須是開放的,而另一個圖片必須是隱形我該怎麼做?我寫了一個jQuery,但不起作用。Cycle2圖片分類

and click to see on codepen - correctly

這裏,你是我的代碼

$(document).ready(function() { 
 

 

 
    $(".filter li").on("click", function() { 
 
    var activeId = $(this).attr("id"); 
 

 
    $("img[data-id]").hide(); 
 
    $("img[data-id = '" + activeId + "']").show(); 
 
    }); 
 
});
.cycle-slideshow { 
 
    position: relative; 
 
    width: 800px; 
 
    height: 494px; 
 
    overflow: hidden; 
 
} 
 

 
#single-pager a img { 
 
    width: 49.3px; 
 
    border: 1px solid #fff; 
 
} 
 

 
#single-pager a.cycle-pager-active img { 
 
    opacity: 0.4; 
 
} 
 

 
#single-left, 
 
#single-right { 
 
    position: absolute; 
 
    top: 50%; 
 
    z-index: 1000; 
 
    background: rgba(255, 255, 255, .8); 
 
    padding: 12px; 
 
    cursor: pointer; 
 
} 
 

 
#single-left { 
 
    left: 0; 
 
} 
 

 
#single-right { 
 
    right: 0; 
 
} 
 

 
.filter { 
 
    position: absolute; 
 
    z-index: 1000; 
 
    right: 0; 
 
    list-style-type: none; 
 
    padding: 0; 
 
    color: #FFF; 
 
    background: rgba(255, 255, 255, 0.6); 
 
    padding: 10px 30px; 
 
} 
 

 
.filter li { 
 
    cursor: pointer; 
 
    display: inline-block; 
 
    background: rgba(0, 0, 0, .6); 
 
    padding: 5px; 
 
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery.cycle2/2.1.6/jquery.cycle2.min.js"></script> 
 

 
<div class="cycle-slideshow" data-cycle-pager="#single-pager" data-cycle-pager-template="<a href='#'><img src='{{src}}' width=48 height=48></a>" data-cycle-prev="#single-left" data-cycle-next="#single-right" data-cycle-pause-on-hover="true" data-cycle-auto-height="true"> 
 
    <img src="https://amazingcarousel.com/wp-content/uploads/amazingcarousel/7/images/lightbox/golden-wheat-field-lightbox.jpg" data-id="sports"> 
 
    <img src="https://amazingcarousel.com/wp-content/uploads/amazingcarousel/7/images/lightbox/sunny-day-lightbox.jpg" data-id="naturel"> 
 
    <img src="https://amazingcarousel.com/wp-content/uploads/amazingcarousel/7/images/lightbox/night-in-the-city-lightbox.jpg" data-id="animals"> 
 
    <img src="https://amazingcarousel.com/wp-content/uploads/amazingcarousel/7/images/lightbox/sakura%20trees-lightbox.jpg" data-id="animals" /> 
 
    <img src="https://amazingcarousel.com/wp-content/uploads/amazingcarousel/7/images/lightbox/daffodil-flowers-lightbox.jpg" data-id="animals" /> 
 
    <img src="https://amazingcarousel.com/wp-content/uploads/amazingcarousel/7/images/lightbox/dandelion-lightbox.jpg" data-id="animals" /> 
 
    <img src="https://amazingcarousel.com/wp-content/uploads/amazingcarousel/7/images/lightbox/tulips-lightbox.jpg" data-id="sports" /> 
 
    <ul class="filter"> 
 
    <li id="sports">Sports</li> 
 
    <li id="naturel">Naturel</li> 
 
    <li id="animals">Animals</li> 
 
    </ul> 
 
    <div id="single-next-prev"> 
 
    <span id="single-left">Prev</span> 
 
    <span id="single-right">Next</span> 
 
    </div> 
 

 
</div> 
 

 
<div id="single-pager" class="center external"></div>

+2

商店中的所有陣列圖像和重建HTML和初始化插件程序再次每次 – charlietfl

+0

我會怎麼做任何的例子嗎? –

回答

1

我知道這可能是有點晚了,但你需要的時候使用「過濾器På重新初始化滑塊「-功能。

我也把你所有的圖像放在一個數組中,所以你可以控制代碼。

看看下面的代碼的解決方案

var images = [ 
 
    { 
 
     cat: "sports", 
 
     src: "https://amazingcarousel.com/wp-content/uploads/amazingcarousel/7/images/lightbox/golden-wheat-field-lightbox.jpg" 
 
    }, 
 
    { 
 
     cat: "naturel", 
 
     src: "https://amazingcarousel.com/wp-content/uploads/amazingcarousel/7/images/lightbox/sunny-day-lightbox.jpg" 
 
    }, 
 
    { 
 
     cat: "animals", 
 
     src: "https://amazingcarousel.com/wp-content/uploads/amazingcarousel/7/images/lightbox/night-in-the-city-lightbox.jpg" 
 
    }, 
 
    { 
 
     cat: "animals", 
 
     src: "https://amazingcarousel.com/wp-content/uploads/amazingcarousel/7/images/lightbox/sakura%20trees-lightbox.jpg" 
 
    }, 
 
    { 
 
     cat: "animals", 
 
     src: "https://amazingcarousel.com/wp-content/uploads/amazingcarousel/7/images/lightbox/daffodil-flowers-lightbox.jpg" 
 
    }, 
 
    { 
 
     cat: "animals", 
 
     src: "https://amazingcarousel.com/wp-content/uploads/amazingcarousel/7/images/lightbox/dandelion-lightbox.jpg" 
 
    }, 
 
    { 
 
     cat: "sports", 
 
     src: "https://amazingcarousel.com/wp-content/uploads/amazingcarousel/7/images/lightbox/tulips-lightbox.jpg" 
 
    } 
 
] 
 

 
$(document).ready(function() { 
 
    for (var i in images) { 
 
     $("#mySlideshow").append("<img src='" + images[i].src + "'/>"); 
 
    } 
 

 
    init() 
 

 
    $(".filter li").on("click", 
 
     function() { 
 
      $("#mySlideshow").cycle('destroy'); 
 
      $("#mySlideshow img").remove(); 
 

 
      var activeId = $(this).attr("id"); 
 

 
      buildCycleSlideshow(activeId); 
 
     }) 
 
}) 
 

 
function init() { 
 
    $("#mySlideshow").cycle({ 
 
     log: false, 
 
     pauseOnHover: true, 
 
     next: "#single-right", 
 
     prev: "#single-left", 
 
     autoHeight: true, 
 
     pager: "#single-pager", 
 
     pagerTemplate: "<a href='#'><img src='{{src}}' width=48 height=48></a>" 
 
    }); 
 
} 
 

 
function buildCycleSlideshow(id) { 
 
    var showAllImages = false; 
 

 
    if (id == "allImages") { 
 
     showAllImages = true; 
 
    } 
 
    
 

 
    for (var i in images) { 
 
     if (images[i].cat == id || showAllImages) { 
 
      $("#mySlideshow").append("<img src='" + images[i].src + "'/>"); 
 
     } 
 
    } 
 
    init(); 
 
}
#mySlideshow { 
 
    position: relative; 
 
    width: 800px; 
 
    height: 494px; 
 
    overflow: hidden; 
 
} 
 

 
#single-pager a img { 
 
    width: 49.3px; 
 
    border: 1px solid #fff; 
 
} 
 

 
#single-pager a.cycle-pager-active img { 
 
    opacity: 0.4; 
 
} 
 

 
#single-left, 
 
#single-right { 
 
    position: absolute; 
 
    top: 50%; 
 
    z-index: 1000; 
 
    background: rgba(255, 255, 255, .8); 
 
    padding: 12px; 
 
    cursor: pointer; 
 
} 
 

 
#single-left { 
 
    left: 0; 
 
} 
 

 
#single-right { 
 
    right: 0; 
 
} 
 

 
.filter { 
 
    position: absolute; 
 
    z-index: 1000; 
 
    right: 0; 
 
    list-style-type: none; 
 
    padding: 0; 
 
    color: #FFF; 
 
    background: rgba(255, 255, 255, 0.6); 
 
    padding: 10px 30px; 
 
} 
 

 
.filter li { 
 
    cursor: pointer; 
 
    display: inline-block; 
 
    background: rgba(0, 0, 0, .6); 
 
    padding: 5px; 
 
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery.cycle2/2.1.6/jquery.cycle2.min.js"></script> 
 

 
<div id="mySlideshow"> 
 
    <ul class="filter"> 
 
    <li id="sports">Sports</li> 
 
    <li id="naturel">Naturel</li> 
 
    <li id="animals">Animals</li> 
 
    <li id="allImages">Show all images</li> 
 
    </ul> 
 
    <div id="single-next-prev"> 
 
    <span id="single-left">Prev</span> 
 
    <span id="single-right">Next</span> 
 
    </div> 
 
</div> 
 

 
<div id="single-pager" class="center external"></div>

+0

oow thanks..this是另一個使用html的例子(http://codepen.io/cowardguy/pen/VpvLNp),但有些東西我不能顯示所有圖像,並且非常感謝您的幫助 –

+0

不用客氣 – Zorken17

+1

這是你用'顯示所有圖像'的解決方案:http://codepen.io/anon/pen/Vpvzrg – Zorken17