[英]how can i go back to the beginning when there is no next()
how can i go back to the beginning when there is no next()没有next()怎么能回到开头
$(function () {
(function hideHeading() {
setInterval(function () {
$('.active').fadeOut(2000, function () {
$(this).removeClass('active').next().addClass('active').fadeIn(2000);
});
}, 2000);
})();
});
example here例子在这里
http://codepen.io/OsamaElzero/pen/LVeLeY http://codepen.io/OsamaElzero/pen/LVeLeY
Check if next is an element, if not, select the first sibling.检查 next 是否为元素,如果不是,则选择第一个兄弟元素。
var elem = $(this).removeClass('active')
var next = elem.next();
if(next.length===0) {
next = elem.siblings().first();
}
next.addClass('active').fadeIn(2000);
You actually don't need setInterval since you could simply do a recursion as below.您实际上不需要 setInterval ,因为您可以简单地进行如下递归。 If the
.next()
isn't there, set the pointer to first()
.如果
.next()
不存在,请将指针设置为first()
。
$(function () {
(function hideHeading() {
$(".active").fadeOut(2000, function () {
var $this = $(this).removeClass('active');
var $next = ($this.next().length && $this.next()) || ($this.siblings().first());
$next.addClass('active').fadeIn(2000, hideHeading);
});
}());
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.