Coffeescript 迭代页面上的单词数组?

发布于 2024-12-26 16:00:46 字数 1339 浏览 1 评论 0 原文

我想要在页面上迭代一系列单词,使现有单词轮换出来,并轮换新单词。

我正在使用 animate.css 通过添加和删除类来处理动画。

初始 div 上有一个入口类 .rotateIn,因此在页面渲染时会产生动画。它还具有一个 .animated 类,该类必须存在才能触发动画。

延迟两秒后,我添加退出动画类 .rotateOut,然后删除入口类,然后触发退出动画。此时div就被隐藏了。

我更改文本,然后添加入口类,然后删除出口类,触发入口动画。

一旦我击中数组中的最后一个单词,该函数就会停止。

在咖啡脚本中编写此内容以便我可以调整入口和出口之间的时间的最佳方法是什么?

编辑:这是一些基本的 JavaScript 代码,显示了我正在尝试执行的操作。我想重写这个,以便我可以设置一个单词数组 ['foo', 'bar'] 而不是下面脆弱的 PITA 方式。

var $adj = $('#adjectives');

setTimeout(function() {
$adj.addClass('rotateOut');
setTimeout(function() {
    $adj.removeClass('rotateIn');
    setTimeout(function() {
        $adj.text('foo');
        $adj.addClass('rotateIn');
        setTimeout(function() {
            $adj.removeClass('rotateOut');
            setTimeout(function() {
                $adj.addClass('rotateOut');
                setTimeout(function() {
                    $adj.removeClass('rotateIn');
                    setTimeout(function() {
                        $adj.text('bar');
                        $adj.addClass('rotateIn');
                    }, 1000);
                }, 1000);
            }, 1000);
        }, 1000);
    }, 1000);
}, 1000);
}, 1000);

I've got an array of words I want to iterate through on a page, making the existing word rotate out, and a new word rotate in.

I'm using animate.css to handle the animations via adding and removing a class.

The initial div has an entrance class .rotateIn on it, and so animates on page render. It also has a .animated class on it, which must be present in order for the animation to be triggered.

After a two second delay, I add the exit animation class .rotateOut, then remove the entrance class, which then triggers the exit animation. At this point, the div is hidden.

I change the text, then add the entrance class, then remove the exit class, triggering the entrance animation.

Once I hit the last word in the array, the function stops.

What's the best way to write this in coffeescript so that I can tweak the timings between entrance and exit?

EDIT: here's some basic javascript code showing what I'm trying to do. I'd like to rewrite this so that I can set an array of words ['foo', 'bar'] rather than the fragile PITA way below.

var $adj = $('#adjectives');

setTimeout(function() {
$adj.addClass('rotateOut');
setTimeout(function() {
    $adj.removeClass('rotateIn');
    setTimeout(function() {
        $adj.text('foo');
        $adj.addClass('rotateIn');
        setTimeout(function() {
            $adj.removeClass('rotateOut');
            setTimeout(function() {
                $adj.addClass('rotateOut');
                setTimeout(function() {
                    $adj.removeClass('rotateIn');
                    setTimeout(function() {
                        $adj.text('bar');
                        $adj.addClass('rotateIn');
                    }, 1000);
                }, 1000);
            }, 1000);
        }, 1000);
    }, 1000);
}, 1000);
}, 1000);

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

皇甫轩 2025-01-02 16:00:46

您应该尝试使用触发相同回调的时间间隔,直到到达末尾,而不是每次都设置超时。看一下这个示例,然后添加额外的动画位。

words = ["hey", "just", "testing", "this", "out"]
i = 0
len = words.length

callback = ->
    clearInterval timer if i is len - 1
    console.log words[i]
    i++
    return

timer = setInterval callback, 1000

为了清楚起见,这里是编译后的 JS

(function() {
  var callback, i, len, timer, words;
  words = ["hey", "just", "testing", "this", "out"];
  i = 0;
  len = words.length;

  callback = function() {
    if (i === len - 1) clearInterval(timer);
    console.log(words[i]);
    i++;
  };

  timer = setInterval(callback, 1000);
}).call(this);

Rather than setting a timeout each time, you should try using an interval that fires the same callback until you've reached the end. Take a look at this example, then add in your extra bits for animating.

words = ["hey", "just", "testing", "this", "out"]
i = 0
len = words.length

callback = ->
    clearInterval timer if i is len - 1
    console.log words[i]
    i++
    return

timer = setInterval callback, 1000

Just for clarity's sake, here is the compiled JS

(function() {
  var callback, i, len, timer, words;
  words = ["hey", "just", "testing", "this", "out"];
  i = 0;
  len = words.length;

  callback = function() {
    if (i === len - 1) clearInterval(timer);
    console.log(words[i]);
    i++;
  };

  timer = setInterval(callback, 1000);
}).call(this);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文