【问题标题】:Using for-loop instead of creating new var for every element使用 for 循环而不是为每个元素创建新 var
【发布时间】:2017-11-10 15:44:29
【问题描述】:

我创建了当元素进入视口时触发的动画。问题是我必须为每个元素重复相同的代码。我正在使用anime.js 和scrollMonitor.js 来制作动画,而且很难让for-loop 工作。

这是我的代码:

        $(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 循环中?

【问题讨论】:

  • 先尝试一个可以多次调用的函数。

标签: javascript for-loop repeat anime.js scrollmonitorjs


【解决方案1】:

在 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)。

【讨论】:

  • (如果您在上面看到'#about' + ' .toAnimate',请点击刷新。编辑错误。)
  • 我使用了 .each() 函数,但 ThemeForest 建议使用 for-loop 代替,因为它稍微快一些。但我无法弄清楚。 For-loop 仅对最后一个元素进行“元素数”次的动画处理。
  • @RobiZzT:您无需担心在窗口load 事件上对三元素数组进行一次forEach 调用时的性能。 :-) (事实上,除了极少数的性能非常密集的代码,你don't have to worry about it at all。)至于for为什么不起作用,那是因为closures-in-loops issue
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-12-27
  • 1970-01-01
  • 2021-08-01
  • 1970-01-01
  • 2017-10-25
相关资源
最近更新 更多