如何使用fadeIn()和fadeOut() jQuery效果更改2个文本?
下面是一个例子:苹果网站。在iPhone图片下面是一个框,“热门新闻标题”。看到新闻有什么变化了吗?一段文字逐渐消失,然后逐渐消失,而另一段文字即将到来。
我是jQuery新手,所以请给出详细的代码。谢谢!
发布于 2011-05-17 16:23:04
假设您的html是这样的:
<div>
<div class='fadey'>1</div>
<div class='fadey'>2</div>
<div class='fadey'>3</div>
</div>
你可以这样做:
var faderIndex = 0, //to keep track
faders = $('.fadey'); //cache, so we won't have to walk the dom every time
function nextFade() {
//fade out element over 1000 milliseconds, after which call a function
$(faders[faderIndex]).fadeOut(1000, function() {
//increase the index and do a check, so we won't overflow
faderIndex++;
if (faderIndex >= faders.length)
faderIndex = 0;
//fade in the next element, after which call yourself infinitely
$(faders[faderIndex]).fadeIn(1000, nextFade);
});
}
//get the ball rolling
nextFade();
实例:http://jsfiddle.net/gpQYW/
发布于 2011-05-17 16:38:13
考虑以下HTML:
<span class="rotateText">Test</span>
<div class="rotateArray">
<a href="http://www.google.com">Google.com</a>
<a href="http://www.microsoft.com">Microsoft.com</a>
<a href="http://stackoverflow.com">Stackoverflow.com</a>
<a href="http://www.wired.com">Wired.com</a>
</div>
剧本:
$(function () {
// set first
$('.rotateText').html($('.rotateArray a').eq(0));
// enter loop
rotateText();
});
function rotateText() {
var count = $('.rotateArray a').length;
var i = 0;
var loopArr = $('.rotateArray a');
var rotationElm = $('.rotateText');
window.setInterval(function () {
rotationElm
.fadeOut(400)
.queue(function () {
$(this).html('<a href="' + loopArr.eq(i).attr('href') + '">' + loopArr.eq(i).text() + '</a>');
if (++i == count) i = 0;
$(this).dequeue();
})
.fadeIn(400)
;
}, 4000);
}
发布于 2011-05-17 15:40:45
您可以使用.fadeOut()
函数的回调来更改HTML,然后将其淡入,因为中间会闪烁:
$('#foo').fadeOut(500, function() {
$(this).html('<span>Bar</span>');
$(this).fadeIn(500);
});
https://stackoverflow.com/questions/6033329
复制相似问题