Friends who are learning javascript can use this as a small exercise to do it. Strengthen your hands-on coding skills. Reference code: Copy code The code is as follows: ggggg <br><br> // Define global variables <br> // Ball coordinates <br> ballX=0; > directX=1;<br> directY=1;<br> //The ball moves<br> function ballMove(){<br> //The ball moves<br> ballX =2*directX;<br> ballY = 2*directY;<br> //Modify the top and width of the ball at the same time<br> div2.style.top=ballY 'px';<br> div2.style.left=ballX 'px';<br> // window.alert(div1.offsetWidth);//offsetwidth is the width of the element in JS, and the corresponding offsetHeight<br> .offsetWidth>=div1.offsetWidth || ball 🎜> directY=-directY; </head><br><body><br> <div id="div1" style="width:400px;height:300px;border:1px solid silver;POSITION: absolute; TOP: 100px"> <br> <div id="div2" style="position:absolute;left:0px;top:0px;"><img src="ball.png"></div><br> < ;/div><br></body><br></html><br><br><br><br>Picture:<br><br> <br><br><br> </div>