#out { width: 630px; height:400px; } #in { width: 60px; height: 60px; } div{ border: 3px solid; } with(document) { var timer = null; timer = setInterval(fn, 0.001); var boolwid = true var boolhei = true var i = 1 var j = 1 var ii = 0 var oDivIn = getElementById('in'); function fn() { if (i < 1) { boolwid = true ii++ } if (i >= 580) { boolwid = false ii++ } if (j < 1) { boolhei = true ii++ } if (j >= 330) { boolhei = false ii++ } boolhei ? j++ : j-- boolwid ? i++ : i-- oDivIn.style.marginLeft = i + 'px'; oDivIn.style.marginTop = j + 'px'; var arryanse = ["red", "yellow", "blue", "green"] oDivIn.style.background = arryanse[ii % 4] } }
#out { width: 630px; height:400px; } #in { width: 60px; height: 60px; } div{ border: 3px solid; }
bug挺多的,楼主需要改改哦!右边会突出,下边没有真正接触到