简体   繁体   English

没有next()怎么能回到开头

[英]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);
        });
    }());
});

Updated Pen更新笔

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在页面加载时转到history.back()? - How can I go to history.back() when page loads? 单击后退按钮时,如何返回上一个状态 - How can I go back to the previous state when back button is clicked 如何使用 api 转到下一页? - How can I go to the next page with the api? 如何在不单击下一步按钮的情况下进入下一个问题? - How can I go for next question without click next button? 使用Puppeteer,如何打开页面,获取数据,然后返回上一页以获取列表中的下一页? - Using Puppeteer, how can I open a page, get the data, then go back to the previous page to get the next page on the list? 当我从目标链接返回时,如何保持列表组中的位置? - How can I mantain the position in a list group when I go back from a destination link? 如何获取数组中的下几个元素,但在传递最后一个元素时跳回到开头? - How can I get next few elements in an array but jump back to the start when passing the last element? 如何在Angularjs表达式中转到数组的下一个项目? - How can I go to next item of array in Angularjs expression? 如何转到json数组中的Next和Previous值? - How can I go to the Next and Previous value in a json array? 如何使它转到上一张图像而不是下一张图像? - How can I make this go to a previous image instead of the next one?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM