正しく動作しない非常に単純な JavaScript/jquery コードがあります。問題は、ID 'circle' を持つ div の配置が、ループの実行時に計算されないように見えることです。問題が発生している理由と、利用可能な修正があるかどうかを知る必要があるだけです。
ここにリンクhttp://jsfiddle.net/TDRyS/ があります。
コード:
var maxa = document.width;
var maxb = document.height;
$(document).ready(function() {
dothis();
});
function dothis() {
var left = parseInt(document.getElementById('circle').style.left);
var top = parseInt(document.getElementById('circle').style.top);
if (top >= 500) {
$("#circle").animate({
top: "-=" + Math.floor(Math.random() * 100 + 1) + "px",
left: "-=" + Math.floor(Math.random() * 100 + 1) + "px"
}, 1000);
}
else {
$("#circle").animate({
top: "+=" + Math.floor(Math.random() * 100 + 1) + "px",
left: "+=" + Math.floor(Math.random() * 100 + 1) + "px"
}, 1000);
}
dothis();
}