我有一个绝对定位的 div 网格,每个包含 3 个图像。这些图像通过类(.z1、.z2 和 .z3)设置的 z-index 堆叠在一起。
我将我的 div 选择到一个数组中并将 div 随机化。
我使用 .animate 来“翻转”第一个图像并显示第二个图像。(.z3 被隐藏)然后我遍历这些 div 并切换类,z1 变为 z3(在底部),z3 移动到 z2(现在在中间),z2 变为 z1。
这在大多数情况下都有效,但偶尔会出现一个看似随机的问题,即 div 中没有显示任何图像。
除了 jQuery slideToggles 之外,我对其他任何东西都毫无用处,所以希望能得到一些帮助。
HTML
<div class="grid">
<div class="r1 c1">
<img src="<?=$grid->image_1_1->getPath()?>" width="149" height="104" class="z1" alt="">
<img src="<?=$grid->image_1_2->getPath()?>" width="149" height="104" class="z2" alt="">
<img src="<?=$grid->image_1_3->getPath()?>" width="149" height="104" class="z3" alt="">
</div>
<div class="r1 c2">
<img src="<?=$grid->image_2_1->getPath()?>" width="137" height="104" class="z1" alt="">
<img src="<?=$grid->image_2_2->getPath()?>" width="137" height="104" class="z2" alt="">
<img src="<?=$grid->image_2_3->getPath()?>" width="137" height="104" class="z3" alt="">
</div>
网格中有更多的 div,但它们在布局上都是相同的。r1/c1 类只是 top: 和 left: 的位置:
jQuery
//Find all sub divs in the grid and trigger the flip on them (in order currently)
function begin() {
var index = 0;
window.setInterval(function() {
var divs = $('div.grid div');
divs.sort(function() { return 0.5 - Math.random();});
flip(divs[index]);
if(++index == divs.length)
index = 0;
}, 1000);
}
//homepage grid animation
function flip(targetDiv) {
//Function begins gather variables
// All images inside target div are same size
var currWidth= $(targetDiv).children('img:eq(0)').width();
var currHeight= $(targetDiv).children('img:eq(0)').height();
var currMargin = currWidth/2;
//Remove .z3 - the "bottom" image so that it is not seen during flip
$('img.z3').width('auto').hide();
$('img.z2, img.z1').css('margin-left', 0).show();
// The Animation
$(targetDiv).children('img.z2').stop().css({width:0,height:''+currHeight+'px',marginLeft:''+currMargin+'px',opacity:'1'});
$(targetDiv).children('img.z1').stop().animate({width:0,height:''+currHeight+'px',marginLeft:''+currMargin+'px',opacity:'1'},
{duration:1000});
$(targetDiv).children('img.z2').stop().animate({width:+currWidth,height:''+currHeight+'px',marginLeft:0,opacity:'1'},
{duration:1000});
//Swap classes
$(targetDiv).children('img').each(function () {
var $self = $(this);
if ($self.hasClass('z1')) {
$self.removeClass('z1').addClass('z3');
$self.width(currWidth);
} else if ($self.hasClass('z2')) {
$self.removeClass('z2').addClass('z1');
} else if ($self.hasClass('z3')) {
$self.removeClass('z3').addClass('z2');
}
});
//Trying to combat items with 0px width on second run through
//$(targetDiv).children('img').width(currWidth);
}
//Trigger Rotation
begin()
不幸的是,我无法发布指向我的页面的链接,因为它是一个客户端,并且在一个私人服务器上,但我希望你能有任何见解。
谢谢