J.Aurélien J.Aurélien - 1 month ago 12
jQuery Question

If margin left == percent

I want my div to go left 5 times and after that coming back.
To do that I have that script :

Javascrpit :

$(document).ready(function() {
if(document.getElementById('twitter').style.marginLeft == "-278%")
{
(function($){
setInterval(function(){

$('#twitter').animate({
marginLeft: '+=278%',
},3000);

}, 5000);

})(jQuery);
}else{
(function($){
setInterval(function(){
$('#twitter').animate({
marginLeft: '-=55.6%',
},2000);

}, 5000);
})(jQuery);
}
});


I have a working script with px using :

if($('#twitter').css("marginLeft")==('-5300px'))


but I need percent to be responsive, can someone help me please ?

EDIT :

The animation is working, just the condition for the if isn't working.

Answer

Use position: absolute and left attribute instead of margin-left

<div id='twitter'>
  hello
</div>

#twitter{
 position: absolute;
 left: -50%;
}