简体   繁体   English

Javascript翻转动画错误

[英]Javascript Flip Animation Bug

I have a grid of absolutely positioned divs, each containing 3 images. 我有一个绝对定位的div网格,每个div包含3张图像。 These images are stacked on top of each other with z-index that is set via a class (.z1, .z2 and .z3). 这些图像通过z-index彼此堆叠,z-index是通过类(.z1,.z2和.z3)设置的。

I select my divs into an array and shuffle the div to randomise. 我将div选择为数组,然后将div随机排列以随机化。

I use .animate to "flip" the first image and show the second one. 我使用.animate来“翻转”第一个图像并显示第二个图像。 (.z3 is hidden) then I loop over those divs and switch the classes, z1 becomes z3 (on the bottom), z3 moves to z2 (now in the middle) and z2 becomes z1. (.z3被隐藏),然后循环遍历这些div并切换类,z1变为z3(在底部),z3移至z2(现在在中间),z2变为z1。

This works for the most part, but occasionally there is a problem seemingly at random where none of the images in a div are displayed. 这在大多数情况下都是有效的,但偶尔会出现一个问题,其中似乎不显示div中的任何图像。

I'm pretty useless with anything more than jQuery slideToggles so would appreciate some help. 除了jQuery slideToggles之外,我几乎没有其他用处,因此不胜感激。

HTML 的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>

There are more divs in the grid that this but they are all identical in layout. 网格中有更多的div,但是布局都相同。 The r1/c1 classes are just the positions for the top: and left: r1 / c1类只是顶部:和左侧的位置:

jQuery 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()

Unfortunately I cannot post a link to my page as it is a client piece and on a private server but I would appreciate any insight that you may have. 不幸的是,我无法将链接发布到我的页面上,因为它是客户端,并且是在私人服务器上,但是我希望您能提供任何见解。

Thanks 谢谢

Typically I solved it as soon as I hit submit, 通常,我一点击提交就解决了

The problem was in my begin function. 问题出在我的开始功能上。

I was getting the array of divs everytime the interval ran rather than once and looping through. 每次运行间隔时,我都会获取div数组,而不是一次遍历。

Moving the div selection out side of the window.interval fixed the issue. 将div选择移出window.interval可以解决此问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM