<!doctype html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>test</title> <style type="text/css"> body,div,span{margin:0;padding:0;} #div1{ width:200px; height:200px; position: relative; left:-200px; top:0; background:red;} #div1 span{ width:20px; height:50px; position:absolute; left:200px; top:75px; background:blue;} </style> <script type="text/javascript"> window.onload = function(){ var oDiv = document.getElementById('div1'); oDiv.onmouseover = function(){ stratMove(10,0); } oDiv.onmouseout = function(){ stratMove(-10,-200); } } var timer = null; function stratMove(speed,iTarget){ clearInterval(timer); var oDiv = document.getElementById('div1'); timer = setInterval(function(){ if(oDiv.offsetLeft == iTarget){ clearInterval(timer); }else{ oDiv.style.left = oDiv.offsetLeft+speed+'px'; } },30) } </script> </head> <body> <div id="div1"> <span id="share">分享</span> </div> </body> </html>
比較喜歡二個參數的方式~~