jQuery在div之间淡入/淡出

时间:2017-09-08 03:04:19

标签: javascript jquery fade

我有这段代码:

var next = null;
    var outer = jQuery('.banner .container');
    var current = outer.find('.banner-word:first');
    current.fadeIn();
    function fade() {
        if (current.next('div.banner-word').length > 0) {
            next = current.next('div.banner-word');
        } else {
            next = outer.find('.banner-word:first');
        }
        current.fadeOut();
        next.fadeIn();
        current = next;
        setTimeout(fade, 11000);
    }
    // start the process
    fade();

它的一些问题 - 1)它似乎忽略了第一个横幅字div 2)在加载时它显示快速显示前2个横幅字div然后从第二个横幅字div开始

我错过了一些明显的东西吗?

1 个答案:

答案 0 :(得分:0)

尝试更改:

if (current.next('div.banner-word').length > 0) {
    next = current.next('div.banner-word');
} else {
    next = outer.find('.banner-word:first');
}

为:

if (current.next().is('div.banner-word')) {
    next = current.next();
} else {
    next = outer.find('.banner-word:first');
}

编辑: 尝试在初始fade()调用中添加延迟。 变化

fade();

setTimeout(fade, 5000);