javascript - Javascript/jQuery fadeIn fadeOut 在 N 长度后不显示
问题描述
我正在使用 Javascript/jQueryfadeIn
delay
和fadeOut
我的数组文本。
function animate(text){
var index = 0;
$(".text").delay(5000).fadeOut(1000, function(){
index = (index + 1) % text.length;
this.textContent = text[index];
}).fadeIn(1000, animate);
}
var text = ['1', '2', '3'];
$(".text").text(text[0]); //First Show
animate(text);
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="text"></div>
上面的代码只在2
. 3 从不显示这样的错误Uncaught TypeError: Cannot read property 'length' of undefined
上面的代码有没有遗漏的部分?
解决方案
animate
接受一个参数,您最初将其作为数组传入:
animate(text);
但是递归回调没有得到参数:
}).fadeIn(1000, animate);
你index
也没有递归地坚持。
我将摆脱参数并使用外部变量来代替text
和index
。
var index = 0;
function animate() {
$(".text").delay(1000).fadeOut(1000, function() {
index = (index + 1) % text.length;
this.textContent = text[index];
}).fadeIn(1000, animate);
}
var text = ['1', '2', '3'];
$(".text").text(text[0]); //First Show
animate(text);
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="text"></div>
推荐阅读
- amazon-web-services - AWS 管理员账户,但看不到账单
- php - 如何将 Url 存储在变量中并检查它是否与另一个 url 匹配
- javascript - 如何使用 querySelectorAll 获取 Youtube 频道名称?
- flutter - 溢出的注释文本不对齐
- javascript - 如何访问对象内部的地址并赋予它另一个值?
- c++ - 在 C 和 C++ 中比较不同符号的表达有什么危害?
- python - 想使用 python 从数据库中选择一个特定的字段,其中所需的字段名称由用户输入
- automata - L = {aa, bb, ba, baa, bba} 的花自动机
- vba - 如何跟踪在 Microsoft Access 中打开表的用户
- python - 严格递增子序列的最小数量