2012-09-26 188 views
0

我有以下标记。控制淡入和淡出的动画

<ul id="ticker"> 
<li><q> Education, then, beyond all other devices of human origin, is the great equalizer of the conditions of men, the balance-wheel of the social machinery </q><cite>Horace Mann</cite></li> 
<li><q> The roots of education are bitter, but the fruit is sweet </q><cite>Aristotle</cite></li> 
<li><q> Education is what remains after one has forgotten everything he learned in school </q><cite>Albert Einstein</cite></li> 
<li><q> Education is the most powerful weapon which you can use to change the world </q><cite>Nelson Mandela</cite></li> 
<li><q> Formal education will make you a living; self-education will make you a fortune </q><cite>Jim Rohn</cite></li> 
</ul> 

和下面的脚本

<script> 
function tick() 
{ 
$('#ticker li:first').animate({'opacity':0}, 2000, function() { $(this).appendTo($('#ticker')).css('opacity', 1); }); 
} 
setInterval(function(){ tick() }, 8000); 
</script> 

的问题是文字淡出,但很好用闪光灯重新出现。任何方式我都可以使淡入淡出。

回答

1

我想你想要的是更多的东西是这样的:

var $ticker = $('#ticker'); // save the static element 
$ticker.children(':not(:first-child)').hide(); 

function tick(){ 
    $ticker.children(':first-child').fadeOut(1000, function() { 
     $(this).appendTo($ticker); 
     $ticker.children().first().fadeIn(1000); 
    }); 
} 
setInterval(tick, 8000); 

http://jsfiddle.net/ffe6q/3/

一个项目显示的时间,并显示项目淡出然后下一项消失。

+0

我相信你已经有了一些样式,所以这是我个人的享受比任何东西更多,但看看:http://jsfiddle.net/ffe6q/5/ – Shmiddty

+0

这是非常好的。谢谢 – Jawad

4

相反的:

$(this).appendTo($('#ticker')).css('opacity', 1); 

做这样的事情:

$(this).appendTo($('#ticker')).animate({'opacity' : 1}, 2000); 
2

检查FIDDLE

function tick() { 
    $('#ticker li:first').animate({ 
     'opacity': 0 
    },2000, function() { 
     $(this).appendTo($('#ticker')).animate({'opacity' : 1}, 2000); 
    }); 
} 
setInterval(function() { 
    tick() 
}, 8000);​ 
1

这个怎么样?

var $ticker = $('#ticker'); // save the static element 
function tick(){ 
    $ticker.children().first().fadeOut(2000, function() { 
     $(this).appendTo($ticker).fadeIn(500); 
    }); 
} 
setInterval(tick, 8000); 

jsfiddle基于susanth Reddy的

+0

注:如果孩子( ':第一胎')不舒尔实际上要快比孩子()第一。 (),前一段时间有一些测试,但没有找到它们atm。 – Simon

1

如果您正在尝试制作幻灯片,这就是我想出的。 的CSS:

ul{position: relative;} 
li{position: absolute;} 
li:not(:first-child){display: none;} 

和JS:

function tick(){ 
    $('#ticker > li:first').fadeOut(1000).next().fadeIn(1000).end().appendTo('#ticker'); 
} 
setInterval(function(){ tick() }, 8000); 
0

变量$股票= $( '#股票');

$ ticker.children(':not(:first-child)')。hide();

功能打勾(){

$ticker.children(':first-child').fadeOut(1000, function() { 

    $(this).appendTo($ticker); 

    $ticker.children().first().fadeIn(1000); 

}); 

} 的setInterval(打勾,8000)