在你原来的代码上进行了修改,加粗倾斜的部分是添加或者修改的位置
<script>
var timer1 = null
var el = null
var left = 1
function moveItRight() {
if (parseInt(el.style.left) >(screen.width - 50)) //el.style.left = 0
{
left = -1
}
else if (parseInt(el.style.left) <= 0) {
left = 1
}
el.style.left = parseInt(el.style.left) + 2 * left + 'px'//本题目最关键的一句代码,让el对象的左边距每次循环都增加2像素,也就是向右移动了2像素
timer1 = setTimeout(moveItRight, 25)//隔25毫秒后运行一次moveItRight函数
}
window.onload = function () {
el = document.getElementById("div1")
el.style.left = '500px'
moveItRight()
}
</script>