这是我在stackoverflow上的第一篇文章,大家好!
我对 jQuery 动画的第一次尝试是在两个 div 中设置一组图像,上半部分和下半部分,并通过向下翻转的过渡使它们旋转,就像古老的带有向下翻转数字的闹钟一样。
我已经把动画弄下来了,但问题是页面上同时有不止一个动画。我的问题来自于不了解 jQuery 选择器的范围。
$(document).ready(function(){
flippers($('.flipper'));
});
function flippers(divs){
divs.each(function(i){
$(".top_container img").each(function (j){
$(this).attr("id",i+"t"+j);
});
$(".bottom_container img").each( function(j){
$(this).attr("id",i+"b"+j);
});
});
var flip_wait = 3600;
setTimeout("flippers_start("+flip_wait+")",flip_wait);
}
function flippers_start(time,flipper){
var curr_top = '#' + $('.top_container img:visible').attr('id');
var curr_bottom = '#' + $('.bottom_container img:visible').attr('id');
if( ($('.top_container img').size() - 1) <= curr_top[3] ){
var next_top = curr_top.substring(0,3) + 0;
var next_bottom = curr_bottom.substring(0,3) + 0;
}
else{
var next_top = curr_top.substring(0,3) + (parseInt(curr_top[3]) + 1);
var next_bottom = curr_bottom.substring(0,3) + (parseInt(curr_bottom[3]) + 1);
}
var height = $(curr_top).height();
var width = $(curr_top).width();
flip(height,width,curr_top,curr_bottom,next_top,next_bottom);
setTimeout("flippers_start("+time+")",time);
}
function flip(height,width,fromtop,frombottom,totop,tobottom){
var fliptime = 250;
$(frombottom).css("z-index","1");
$(tobottom).css("z-index","2");
$(fromtop).css("z-index","2");
$(totop).css("z-index","1");
$(tobottom).css("height","1px").show();
$(totop).show();
$(fromtop).stop().animate({height:'0px',width:''+width+'px',marginTop:''+height+'px'},{duration:fliptime});
window.setTimeout(function() {
$(tobottom).stop().animate({height:''+height+'px',width:''+width+'px',marginBottom:'0px',opacity:'1'},{duration:fliptime});
},fliptime-40);
$(frombottom).slideUp();
window.setTimeout(function(){
$(fromtop).height(height).hide().css("margin-top","0px");
},fliptime*2);
}
当它在我描述的一组图像上运行时,
<div class="flipper">
<div class="top_container">
<img src="images/pink_floyd_img/dark1.jpg" class="top first" />
<img src="images/pink_floyd_img/wall1.jpg" class="top second hidden" />
<img src="images/pink_floyd_img/wish1.jpg" class="top third hidden" />
</div>
<div class="bottom_container">
<img src="images/pink_floyd_img/dark2.jpg" class="bottom first" />
<img src="images/pink_floyd_img/wall2.jpg" class="bottom second hidden" />
<img src="images/pink_floyd_img/wish2.jpg" class="bottom third hidden" />
</div>
</div>
它就像我期望的那样工作。但是,当我复制“flipper” div 时,动画首先在第一组上运行,然后在第二组上运行,依此类推。
我知道这与我选择每个图像进行动画处理的方式有关,但我真的不知道如何做我正在做的事情。
最终目标是能够将标有适当类的 div 和图像放在一起,并让每个集合动态 id 以运行动画 - 并且让它们都同时启动动画 - 即使它们有每个 div 的单独调用(这可能最终会发生,因此它们不会同时翻转)。
谢谢!