简体   繁体   中英

JavaScript: Element positioning

How can we change a position of an element in JS from a function? object.style.left = "distance"; --> It won't work with returned value, but with right format (12%, 12px).

Code Example:

/*Get random value*/
function move() {
    var distance = 10*Math.random();
    return distance;
}
/*Assign to the style.left property of an element*/
function assign() {
    var distance = move();
    object.style.left = "distance";
}```

"distance" should not be a string. It's a variable. Try:

object.style.left = distance;

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