jQuery 在 div 内一次循环 p 标签的 .fadeIn 和 .fadeOut
下面的代码成功淡入一个感言 6 秒,等待 3 秒,然后淡出并转到下一个感言。一旦到达第三个推荐,它就会跳回第一个。这正是我想要的,但在我的实际网站上,我有超过三个推荐,并且将来可能会添加更多。我不想每次添加新的推荐时都必须返回并添加新功能。我尝试了一段时间使用“this”和 .next() 让它工作但失败了。我希望有人可以通过循环它并使其移动到容器内的下一个 p 标签来提高效率,而不必每次都调用新函数。如有任何帮助,我们将不胜感激,谢谢。
注意:我知道有类似的问题,但没有一个是完全相同的,而且答案也低于标准。
<!DOCTYPE html>
<html>
<head>
<script src="http://code.jquery.com/jquery-latest.js"></script>
<style>
#tml-container p { display: none; }
</style>
</head>
<body>
<div id="tml-container">
<p id="one">Testimonial 1</p>
<p id="two">Testimonial 2</p>
<p id="three">Testimonial 3</p>
</div>
<script>
$(document).ready(function() {
function doFade() {
$("#one").fadeIn(6000,function() {
$("#one").fadeOut(6000).delay(3000);
setTimeout(fadeTwo,6000);
});
}
function fadeTwo() {
$("#two").fadeIn(6000,function() {
$("#two").fadeOut(6000).delay(3000);
setTimeout(fadeThree,6000);
});
}
function fadeThree() {
$("#three").fadeIn(4000,function() {
$("#three").fadeOut(6000).delay(3000);
setTimeout(doFade,6000);
});
}
doFade();
});
</script>
</body>
</html>
The code below successfully fades in one testimonial for 6 seconds, waits 3 seconds, and then fades it out and moves on to the next. Once it reaches the third testimonial it jumps back to the first. This is exactly what I want but on my actual site I have more than three testimonials and in the future may be adding more. I don't want to have to go back and add a new function every time I add a new testimonial. I tried for some time to get this to work using "this" and .next() but failed. I'm hoping someone could make this much more efficient by looping it and getting it to move to the next p tag within the container without calling a new function every time. Any help is appreciated, thank you.
Note: I know there are similar questions but none of them were quite the same and the answers are sub par.
<!DOCTYPE html>
<html>
<head>
<script src="http://code.jquery.com/jquery-latest.js"></script>
<style>
#tml-container p { display: none; }
</style>
</head>
<body>
<div id="tml-container">
<p id="one">Testimonial 1</p>
<p id="two">Testimonial 2</p>
<p id="three">Testimonial 3</p>
</div>
<script>
$(document).ready(function() {
function doFade() {
$("#one").fadeIn(6000,function() {
$("#one").fadeOut(6000).delay(3000);
setTimeout(fadeTwo,6000);
});
}
function fadeTwo() {
$("#two").fadeIn(6000,function() {
$("#two").fadeOut(6000).delay(3000);
setTimeout(fadeThree,6000);
});
}
function fadeThree() {
$("#three").fadeIn(4000,function() {
$("#three").fadeOut(6000).delay(3000);
setTimeout(doFade,6000);
});
}
doFade();
});
</script>
</body>
</html>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这是一个非常简单的解决方案:
DEMO
可重用插件版本:
迭代某个元素的子元素,而是迭代选定的元素。
可用作:
DEMO
This is a very simple solution:
DEMO
Reusable plugin version:
Instead of iterating over the children of a certain element, it iterates over the selected elements.
Usable as:
DEMO
改了一个自己开发的Plugin来满足你的要求,
但没有完全测试它。
至少应该向您展示如何正确设置此类内容。
希望这有帮助:)
Changed a self developed Plugin to meet your requirements,
didn't test it fully though.
Should at least show you how to set something like this up properly.
Hope this helps :)