我正在尝试使用css动画一次显示3张图像(当前为12张).我想首先显示图像1-3,然后是4-6,然后是7-9,然后是10-12.
到目前为止我有这个,但我无法理解如何使用:第n个选择器显示3下一个而隐藏其余部分.
这或多或少是我到目前为止所拥有的:
#crossfade > img {
max-width: 30%;
height: auto;
opacity: 0;
z-index: 0;
-webkit-backface-visibility: hidden;
animation: imageAnimation 30s linear infinite 0s;
}
#crossfade > img:nth-child(n+3) {
animation-delay: 6s;
}
@keyframes imageAnimation {
0% {
opacity: 0;
animation-timing-function: ease-in;
}
8% {
opacity: 1;
animation-timing-function: ease-out;
}
17% {
opacity: 1
}
25% {
opacity: 0
}
100% {
opacity: 0
}
}
以下示例[or click here for jsfiddle]
#crossfade {
height: 185px;
width: 100%;
position: relative;
}
#crossfade > img {
max-width: 30%;
height: auto;
opacity: 0;
z-index: 0;
-webkit-backface-visibility: hidden;
-webkit-animation: imageAnimation 30s linear infinite 0s;
-moz-animation: imageAnimation 30s linear infinite 0s;
-o-animation: imageAnimation 30s linear infinite 0s;
-ms-animation: imageAnimation 30s linear infinite 0s;
animation: imageAnimation 30s linear infinite 0s;
}
#crossfade > img:nth-child(n+3) {
-webkit-animation-delay: 6s;
-moz-animation-delay: 6s;
-o-animation-delay: 6s;
-ms-animation-delay: 6s;
animation-delay: 6s;
}
#crossfade > img:nth-child(n+3) {
-webkit-animation-delay: 12s;
-moz-animation-delay: 12s;
-o-animation-delay: 12s;
-ms-animation-delay: 12s;
animation-delay: 12s;
}
#crossfade > img:nth-child(4) {
-webkit-animation-delay: 18s;
-moz-animation-delay: 18s;
-o-animation-delay: 18s;
-ms-animation-delay: 18s;
animation-delay: 18s;
}
#crossfade > img:nth-child(5) {
-webkit-animation-delay: 24s;
-moz-animation-delay: 24s;
-o-animation-delay: 24s;
-ms-animation-delay: 24s;
animation-delay: 24s;
}
@keyframes imageAnimation {
0% {
opacity: 0;
animation-timing-function: ease-in;
}
8% {
opacity: 1;
animation-timing-function: ease-out;
}
17% {
opacity: 1
}
25% {
opacity: 0
}
100% {
opacity: 0
}
}
最佳答案
我很感激这已经得到了回答,但是这是我的jQuery解决方案,如果它可以帮助将来的任何人.
我已经对JavaScript代码进行了大量评论,所以应该是自我解释的,但实际上你提供的函数包含显示的img /幻灯片的数量以及显示它们的速度,剩下的就完成了.它是一个动态的,将调整奇数幻灯片等.
希望这对任何人都有帮助:)
http://jsfiddle.net/BradChelly/qjtcojfc/3/
function slideShow(currentSlide){
// speed at which the slides change in miliseconds (1000 = 1sec)
// must not exceed total time for the fade in/out animations
var duration = 3000;
// how many slides to show at a time?
var slidesPerShow = 3;
// count the number of slides (imgs)
slideCount = $( "#slideshow_images" ).children().length;
// Array of next slides to be displayed
slidesToShow = [];
// add the defined number of slides "slidesPerShow" to the slidesToShow array
var times = slidesPerShow;
for(var i=0; i < times; i++){
slidesToShow.push(currentSlide+i+1);
}
// set slides array
slides = [];
// add each slide to the slides array
$.each( slidesToShow,function( i,val ) {
slide = $( "#slideshow_images img:nth-child(" + val + ")" ).clone();
slides.push(slide);
});
// provided the current slideshow is not empty
// fade out its contents,replace it with new slides and fade back in.
if ( $("#slideshow").children().length > 0 ) {
$("#slideshow").children().fadeOut("slow",function(){
$("#slideshow").empty().append(slides)
$("#slideshow").children().fadeIn(1000);
});
}
// otherwise,if the current slideshow is empty
// just load the slides straight away.
else {
$("#slideshow").append(slides);
$("#slideshow").children().fadeIn(1000);
}
// set the current slide to the last slide shown or 0 if max slide count reached
slidesToShow[(slidesPerShow-1)] >= slideCount ? currentSlide = 0 : currentSlide = slidesToShow[(slidesPerShow-1)];
// repeat after set time (set the duration at the top of function)
setTimeout( function(){
slideShow(currentSlide);
},duration );
}
// call the slideshow
slideShow(0);
#slideshow img {display: none;}
原文链接:https://www.f2er.com/css/426983.html