Animate absolute div left: 0 then right: 0 and loop

Well, I can animate divs and loop animations and stuff, but it’s really nervous, I want to animate divs to left:0;and then do right:0;and loop animations, and IT DOES NOT WORK like that ... Why?

How should I do it?..

PS: Obviously, jquery cannot animate an absolute container from the left: 0 to the right: o ... but how can I get the desired animation?

Example:

Something like this will not work ...:

$(document).ready(function() {   
function animateMydiv() {
$('#mydiv').animate({'left':'0px'},6000).animate({'right':'0px'},6000, animateMydiv); 
}

animateMydiv();
}); 

Here is the jsfiddle .

+4
source share
1 answer

As far as I understand positioning in CSS, you cannot set rightand values ​​at the same time left.

. :

$(document).ready(function() {

  var width = $(document).width() - $('#mydiv').width();

  function animateMydiv() {
    $('#mydiv').animate({'left': width + 'px'}, 6000).animate({'left': '0px'}, 6000);

  }

  animateMydiv();

}); 

: http://jsfiddle.net/bc927afp/1/

, , , : $('#mydiv').animate({'left': width + 'px'}, 6000).animate({'left': '0px'}, 6000, animateMydiv);

+2

All Articles