简体   繁体   中英

jQuery Animation - animating different divs at different speeds

I have 5 divs layered and a object in the foreground I want to move across them. This is using the paralax effect. I have been sucessfully able to move the object using basic .animate in jQuery.

The problem I'm having is getting the background divs to animate properly - or at all. What happens is when I click on my trigger div - the div.cloud1 and div.cloud2 move BEFORE my object does. They also change positions despite my playing with the timing values.

All objects in the divs are absolutely positioned - the divs are relative for being able to use z-index.

Specifically I'm trying to move div.cloud1, div.cloud2, div.ground, div.Mountain all at different speeds so it gives the illusion of 3d.

The object I'm sending across is a different div.

I'm not sure what the problem is.

Here is my JSfiddle: http://jsfiddle.net/U6Mu6/

 jQuery(document).ready(function () {
    jQuery('#cloud-01').css({
        backgroundPosition: '50 -180px'
    });
    jQuery('#cloud-02').css({
        backgroundPosition: '0 -100px'
    });
    jQuery('#mountains-03').css({
        backgroundPosition: '0 50px'
    });
    jQuery('#trees-04').css({
        backgroundPosition: '0 50px'
    });
    jQuery('#ground').css({
        backgroundPosition: 'left bottom'
    });
    jQuery('#branding').css({
        backgroundPosition: 'center 0'
    });
    jQuery('#content').css({
        backgroundPosition: 'center 0'
    });
    jQuery('#sec-content').css({
        backgroundPosition: 'center 0'
    });
    jQuery('#footer').css({
        backgroundPosition: 'center 0'
    });
    jQuery('#wrapper').css({
        overflow: "hidden"
    });

    jQuery('#klicker').click(function () {
        jQuery('#cloud-01').animate({
            backgroundPosition: '(-100px -10px)'
        }, 200000);
        jQuery('#cloud-02').animate({
            backgroundPosition: '(-400px 0px)'
        }, 20000);
        jQuery('#mountains-03').animate({
            backgroundPosition: '(-2500px 50px)'
        }, 20000);
        jQuery('#ground').animate({
            backgroundPosition: '(-5000px bottom)'
        }, 20000);

        startHim();

        jQuery("#full-robot").animate({
            left: "50%",
            marginLeft: "-150px"
        }, 2000);
        setTimeout("leaveScreen()", 15000);
    });

});

var num = 1;

function startHim() {
    num++;
    jQuery("#sec-content").animate({
        top: "-=5px"
    }, 150).animate({
        top: "+=5px"
    }, 150);
    jQuery("#content,#branding").animate({
        top: "-=" + num + "px"
    }, 150).animate({
        top: "+=" + num + "px"
    }, 150);
    if (num < 4) {
        setTimeout("startHim()", 300);
    } else {
        setTimeout("bounceHim()", 300);
    }
}

function bounceHim() {
    jQuery("#sec-content,#branding").animate({
        top: "-=4px"
    }, 150).animate({
        top: "+=4px"
    }, 150);
    jQuery("#content").animate({
        top: "-=8px"
    }, 150).animate({
        top: "+=8px"
    }, 150);
    setTimeout("bounceHim()", 300);
}

function leaveScreen() {
    jQuery("#full-robot").animate({
        left: "100%",
        marginLeft: "0px"
    }, 2000);
}

Just FYI - some of the objects in the fiddle are not included on purpose. I just want to get things working first.

I did see a error in JSFIDDLE dealing with implied eval on my setTime expression. But I'm not sure how to fix it. I suppose I could pass the div as function and use .hide instead.

All help is welcome thanks!

EDIT:::

I forgot this:

/**
* v. 1.02
*/
(function($) {
$.extend($.fx.step,{
'background-position': function(fx) {
if (fx.state === 0 && typeof fx.end == 'string') {
var start = $.curCSS(fx.elem,'background-position');
start = toArray(start);
fx.start = [start[0],start[2]];
var end = toArray(fx.end);
fx.end = [end[0],end[2]];
fx.unit = [end[1],end[3]];
}
var nowPosX = [];
nowPosX[0] = ((fx.end[0] - fx.start[0]) * fx.pos) + fx.start[0] + fx.unit[0];
nowPosX[1] = ((fx.end[1] - fx.start[1]) * fx.pos) + fx.start[1] + fx.unit[1];
fx.elem.style.backgroundPosition = nowPosX[0]+' '+nowPosX[1];
function toArray(strg){
strg = strg.replace(/left|top/g,'0px');
strg = strg.replace(/right|bottom/g,'100%');
strg = strg.replace(/([0-9\.]+)(\s|\)|$)/g,"$1px$2");
var res = strg.match(/(-?[0-9\.]+)(px|\%|em|pt)\s(-?[0-9\.]+)(px|\%|em|pt)/);
return [parseFloat(res[1],10),res[2],parseFloat(res[3],10),res[4]];
}
}
});

})(jQuery);// JavaScript Document

I don't know if this is too obvious, but your trying to set the "background-position" attribute of the clouds by using backgroundPosition

You might just change them to

$("#cloud-01").css({'background-position': '50px -180px'})

Notice the background-position instead of backgroundPosition

If you want to stagger the time each cloud takes to move, you need to offset your animation durations, like

    $('#cloud-01').animate({
        'background-position' : '(-100px -10px)'
    }, (1000) ); // 1 second duration
    $('#cloud-02').animate({
        'background-position' : '(-400px 0px)'
    }, (2000) ); // 2 seconds
    $('#mountains-03').animate({
        'background-position' : '(-2500px 50px)'
    }, (2000) ); // 3 seconds

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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