I want to move my div position continuously left to right then top to bottom.
After first move my code stop.
please check https://jsfiddle.net/LLqmL33p/
function placeDiv(x_pos) {
var d = document.getElementById('boxed');
d.style.position = "absolute";
d.style.left = x_pos+'px';
setTimeout(function(){ placeDiv2(10); }, 1000);
}
function placeDiv2(y_pos) {
var d = document.getElementById('boxed');
d.style.position = "absolute";
d.style.top = y_pos+'px';
setTimeout(function(){ placeDiv(15); }, 1000);
}
placeDiv(10);
I cant understand what can I do now?
The function keeps running continuously, but because the x_pos=10 and y_pos=15 have same value always, the div will not move, try this:
function placeDiv(x_pos) {
var d = document.getElementById('boxed');
d.style.position = "absolute";
if(d.style.left=="")
{
var cur_left=0;
}
else
{
var cur_left=parseFloat(d.style.left);
}
d.style.left = (cur_left+x_pos)+'px';
setTimeout(function(){ placeDiv2(10); }, 1000);
}
function placeDiv2(y_pos) {
var d = document.getElementById('boxed');
//d.style.position = "absolute";
if(d.style.top=="")
{
var cur_top=0;
}
else
{
var cur_top=parseFloat(d.style.top);
}
d.style.top = (cur_top+y_pos)+'px';
setTimeout(function(){ placeDiv(15); }, 1000);
}
placeDiv(10);
What I do is I add the x_pos and y_pos value to current left and top value of the div.
here is the updated fiddle: https://jsfiddle.net/LLqmL33p/2/ and sorry for my bad English.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With