繁体   English   中英

使用for循环,而不是为每个元素创建新的var

[英]Using for-loop instead of creating new var for every element

我创建了在元素进入视口时触发的动画。 问题是我必须为每个元素重复相同的代码。 我正在使用anime.js和scrollMonitor.js进行动画制作,因此很难进行for循环。

这是我的代码:

        $(window).on("load", function() {
            'use strict';

            var elementWatcher1 = scrollMonitor.create('#about', -200);
            elementWatcher1.enterViewport(function() {
                var startAnimation = anime.timeline();
                startAnimation
                    .add({
                        targets: '#about .toAnimate',
                        translateY: [50, 0],
                        opacity: 1,
                        duration: 600,
                        delay: function(el, index) {
                            return index * 80;
                        },
                        easing: 'easeOutQuad'
                    });
                this.destroy();
            });
            var elementWatcher2 = scrollMonitor.create('#portfolio', -200);
            elementWatcher2.enterViewport(function() {
                var startAnimation = anime.timeline();
                startAnimation
                    .add({
                        targets: '#portfolio .toAnimate',
                        translateY: [50, 0],
                        opacity: 1,
                        duration: 600,
                        delay: function(el, index) {
                            return index * 80;
                        },
                        easing: 'easeOutQuad'
                    });
                this.destroy();
            });
            var elementWatcher3 = scrollMonitor.create('#gallery', -200);
            elementWatcher3.enterViewport(function() {
                var startAnimation = anime.timeline();
                startAnimation
                    .add({
                        targets: '#gallery .toAnimate',
                        translateY: [50, 0],
                        opacity: 1,
                        duration: 600,
                        delay: function(el, index) {
                            return index * 80;
                        },
                        easing: 'easeOutQuad'
                    })
                    .add({
                        targets: '#gallery .toAnimateToo',
                        opacity: 1,
                        duration: 600,
                        delay: function(el, index) {
                            return index * 80;
                        },
                        easing: 'easeOutQuad',
                        offset: 0
                    });
                this.destroy();
            });
        });

有谁知道如何将其放入for循环中?

在ES5中,只需对这些选择器的数组使用forEach循环:

$(window).on("load", function() {
    'use strict';

    ["#about", "#portfolio", "#gallery"].forEach(function(selector) {
        var elementWatcher = scrollMonitor.create(selector, -200);
        elementWatcher.enterViewport(function() {
            var startAnimation = anime.timeline();
            startAnimation
                .add({
                    targets: selector + ' .toAnimate',
                    translateY: [50, 0],
                    opacity: 1,
                    duration: 600,
                    delay: function(el, index) {
                        return index * 80;
                    },
                    easing: 'easeOutQuad'
                });
            this.destroy();
        });
    });
});

在ES2015 +中,您可以改为使用for-of (对于const来说是elementWatcher )。

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM