- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
下面的代码成功淡入一个感言 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>
最佳答案
这是一个非常简单的解决方案:
function fade($ele) {
$ele.fadeIn(6000).delay(3000).fadeOut(6000, function() {
var $next = $(this).next('p');
// if there is a following `p` element, it is faded in
// otherwise start from the beginning by taking
// the parent's first child
fade($next.length > 0 ? $next : $(this).parent().children().first());
});
};
fade($('#tml-container > p').first());
<小时/>
可重复使用的插件版本:
它不是迭代某个元素的子元素,而是迭代选定的元素。
(function($) {
var default_config = {
fadeIn: 3000,
stay: 3000,
fadeOut: 3000
};
function fade(index, $elements, config) {
$elements.eq(index)
.fadeIn(config.fadeIn)
.delay(config.stay)
.fadeOut(config.fadeOut, function() {
fade((index + 1) % $elements.length, $elements, config);
});
}
$.fn.fadeLoop = function(config) {
fade(0, this, $.extend({}, default_config, config));
return this;
};
}(jQuery));
可用作:
$('#tml-container > p').fadeLoop({fadeIn: 6000, stay: 3000, fadeOut: 6000});
关于jQuery 在 div 内一次循环 p 标签的 .fadeIn 和 .fadeOut,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8616395/
我正在努力编写我的第一个 jQuery 脚本。我的页面上有一个 DIV,该 DIV 设置为通过 CSS 隐藏。然后,我运行这个脚本以使其淡入、淡出,然后再次淡入: (function($) {
我刚刚学习了一些 jQUery,以便在我为酒店创建的网站上建立一个基本的图片库,但目前还没有计划。我明白了,所以箭头会在图像中循环(还没有动画),但我决定箭头应该在图像悬停时淡入,而不是淡出,但这不知
我有一个具有不同模块的交互式表单系统,其中每个模块的表单包含在不同的 div 元素内。当我从一个模块更改为另一个模块时,我经常这样做: $('#module_1').hide(); $('#modul
我想在滚动页面时淡入我的标题。这是我到目前为止所得到的,除了 .fadeIn() 之外一切正常。 $(document).scroll(function() { if( $(this).scr
我有一个 jquery 错误,我已经找了好几个小时了,我不知道出了什么问题......我有这段代码: $(document).ready(function(){ $('#ulPhotos a
我试图搜索并查看是否在此下方列出了任何内容,但我什么也没看到。我有一个使用 Isotope 布局的图库,它工作正常,但是在页面加载时,图库中的图像显示在页面中间,然后一旦加载就跳转到各自的位置。 我试
如果我执行更改表格尺寸的 fadeIn(),Firefox 会剪切新显示行的背景。 如果我在远离标签的地方单击焦点,然后单击返回,背景将恢复。 Chrome 中没有裁剪问题。我在 Firefox 26
现在,当我淡入叠加层 div 时,它会自动设置为 display: block;。但我希望它是 display: table。有没有像 fadein() 这样的 jQuery 函数可以做到这一点? j
我有这个函数,非常适合延迟加载。 panel.find('img[data-src]').each(function(){ element = $(this);
有人可以告诉我我做错了什么吗: 风格: .warning{border: 1px solid #F0AAAA; background:#FFBABA; color: #C90000;} 标记: A
当我单击切换按钮时,它会将元素的样式属性从“不显示”更改为“阻止”。现在,当发生这种情况时,我想为菜单的左侧位置设置动画。我有这样的代码,但工作有问题,而且,当它应该返回到右侧时,它出现在左侧.. v
我有这段代码,可以在一定的延迟后切换图像的不透明度: $(".pattern-overlay").css("background","black").delay(2000).queue(functio
我对 jQuery 很陌生,所以请耐心等待。 我想逐步淡入列表。但我的功能出了什么问题。请问有人可以帮助我吗? 非常感谢! 编辑:我检查了所有内容,没有发现任何错误。主体具有所需的类。 $(".sid
我希望有人能帮助我。我遇到以下问题: http://jsfiddle.net/zhPAF/ 标记: About Us
这是我的代码..我想要应用淡入淡出,我按照下面的方式做了,但淡入淡出没有应用 $(document).ready(function(){ var i = 1; $
我使用 jquery 加载和淡入目标 div 中的内容。这很好用,我喜欢它看起来很整洁。但是,如果您单击链接多次(快速)淡入内容,则内容似乎不会完全不透明。因此,如果我按链接两次(再次非常快),它只能
非常简单的 jQuery 元素作为实践。我已经设置了一个按钮(#btn),我想看看当我单击该按钮时是否可以淡入 Div。我设置了一个 Fiddle,代码本身有什么问题吗? 链接到JsFiddle .
我正在开发一个网站,我希望每次打开网页时都会淡入文本,所以我做了这样的事情: $(document).ready(function() { $('#title').fadeIn(100); }
请有人让我摆脱痛苦......我已经投入了一个又一个小时...... 我已经(这是缩写)创建了一个函数,该函数使用 append 将框添加到页面。问题是一旦添加了它们,淡入功能就不起作用了。 但是,如
与我在这里看到的其他帖子不同,我使用 jQuery fadeIn 来处理可变文本而不是图像。我看到它逐渐褪色,最后一个脉冲变成全彩。我希望它能逐渐淡出。我已将其发布在 jsFiddle 上:http:
我是一名优秀的程序员,十分优秀!