Home:ALL Converter>Animate element to random left position

Animate element to random left position

Ask Time:2013-08-05T22:08:19         Author:user1207524

Json Formatter

Im using this code to generate random left position of an element and animate it to that position.

function moveBox() {

    var randomSpeed = Math.round(Math.random()*200);
    var bounceSpeed = defaultSpeed + randomSpeed;   

    var randomX = Math.round(Math.random()*200);

    $("#box").animate({left:'"+='+randomX+'"', top:"+=20"},bounceSpeed, "linear", function(){moveBox();});
};

However its not working for some reason. The top animation works flawlessly because its just normal number. But the left property doesnt do anything, I think its something with the method i added the += and "" but cant figure out how to fix it.

Author:user1207524,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/18060283/animate-element-to-random-left-position
yy