为jquery幻灯片创建循环
早些时候,我在这个网站上找到了jquery幻灯片的解决方案,但它不是循环的。我试图解决这个问题,但我需要一些帮助 HTML: JQUERY(这里需要帮助,我想用if语句循环幻灯片):为jquery幻灯片创建循环,jquery,loops,slideshow,Jquery,Loops,Slideshow,早些时候,我在这个网站上找到了jquery幻灯片的解决方案,但它不是循环的。我试图解决这个问题,但我需要一些帮助 HTML: JQUERY(这里需要帮助,我想用if语句循环幻灯片): $(函数(){ $('.slides_header li:first child').addClass('active'); $('.active').css({'z-index':'1'}); }); 函数slideSwitch(){ var$active=$('.slides_header li.active'
$(函数(){
$('.slides_header li:first child').addClass('active');
$('.active').css({'z-index':'1'});
});
函数slideSwitch(){
var$active=$('.slides_header li.active');
var$next=$active.next();
var$zindex=$active.css('z-index')+1;
//我想在这里做一个if语句来循环幻灯片
console.log($next.length());//您尝试检查$next
的长度是正确的
尝试以下方法:
if(!$next.length){
$next = $('.slides_header li').first();
// or
$next = $active.siblings().first();
}
正确!非常感谢!也许将来其他人会喜欢使用此脚本,然后在z-index变量上生成parseInt:var$zindex=parseInt($active.css('z-index'))+1;这样就不会得到疯狂的z-index值。
div#slideshow_header {
width: 100%;
max-width: 1100px;
position: relative;
height: inherit;
}
ul.slides_header {
width: 100%;
height: inherit;
margin: 0;
position: relative;
padding: 0;
overflow: hidden;
}
ul.slides_header li {
width: 100%;
height: inherit;
position: absolute;
}
ul.slides_header img {
width: 100%;
position: absolute;
display: block;
margin: auto;
top: 0;
bottom: 0;
left: 0;
right: 0;
}
.active {
-webkit-animation: fadein 1s; /* Chrome, Safari, Opera */
animation: fadein 1s;
}
@-webkit-keyframes fadein {
from {opacity: 0;}
to {opacity: 1;}
}
$(function(){
$('.slides_header li:first-child').addClass('active');
$('.active').css({'z-index':'1'});
});
function slideSwitch() {
var $active = $('.slides_header li.active');
var $next = $active.next();
var $zindex = $active.css('z-index') + 1;
// I'd like to make an if statement here to loop the slideshow
console.log($next.length()); // <<< Tried to check if next not exists
$next.addClass('active');
$next.css({'z-index':$zindex});
$active.removeClass('active');
}
$(function() {
setInterval( "slideSwitch()", 5000 );
});
if(!$next.length){
$next = $('.slides_header li').first();
// or
$next = $active.siblings().first();
}