javascript - 使用jquery循环div元素

标签 javascript jquery html css

我正在尝试循环 div 元素,我成功地以连续的方式循环了两个 div 元素。但是当我将代码扩展到五个 div 时,循环并没有扩展到所有 div 并且动画中存在延迟。我使用 animate.css 来制作翻译动画

jsFiddle- http://jsfiddle.net/e62m6hfn/7/

jQuery

function animate($el) {
    $el.addClass('animated  lightSpeedOut');
}

var elCounter = 0;

$(document).ready(function () {
    elCounter = $('.s-animate').length;
    animate($('.s-animate').eq(elCounter - 1));

    $('.s-animate').on('webkitAnimationEnd mozAnimationEnd MSAnimationEnd   oanimationend animationend', function () {
        elCounter = (elCounter > 1) ? elCounter - 1 : $('.s-animate').length;
        $('.s-animate').removeClass('bottom');
        $(this).addClass('bottom');
        $(this).removeClass('animated  lightSpeedOut');
        animate($('.s-animate').eq(elCounter - 1));
    });
});

最佳答案

在您的类(class) .bottom 中应用的 z-index 有问题。

事实上,您不应该在每个循环中删除该类,而应该只在所有循环结束时删除该类。

这是我在您的代码中所做的更改:

elCounter = elCounter - 1;
$(this).addClass('bottom');
if (elCounter <= 0) {
    $('.bottom').removeClass('bottom');
    elCounter = $('.s-animate').length;
}

代替

elCounter = (elCounter > 1) ? elCounter - 1 : $('.s-animate').length;
$('.s-animate').removeClass('bottom');
$(this).addClass('bottom');

这是更新的 jsfiddle .

关于javascript - 使用jquery循环div元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33880197/

相关文章:

javascript - 如何通过自定义事件传递浏览器事件?

javascript - 使用 JQuery 动态隐藏/显示 <td>

javascript - jQuery:从 DOM 元素的所有实例中删除 html 标签

html - 为什么我的导航栏覆盖了 Fancybox,我该如何解决?

javascript - ES6 箭头函数中关于 `arguments` 的官方信息?

javascript - 等待的 promise 仍在返回 <pending>

javascript - 从字符串数组生成星期几对象

javascript - 私有(private)属性(property)/公共(public) setter/getter - javascript

jquery - 如何使用 Backbone.js 和 Rails 更新一组记录?

html - css flexbox 网格砌体风格