標籤:style width os art cti html
思路:1,臨界值是 -150 和 0如果大於臨界值,就要隱藏2,隱藏:速度為負顯示:速度為正3,如果與臨界值相等,就清空定時器否則,就運動
--------------------------------
html
<div id="div1"><span>分享到</span></div>
<style>#div1 { position:absolute; left:-150px; width:150px; height:200px; background:green;}#div1 span {position:absolute; right:-20px; top:70px; width:20px; height:60px; line-height:20px; background:blue;}</style>
----------------------------------------
js
<script>window.onload = function(){var oDiv = document.getElementById("div1");var timer = null;oDiv.onmouseover = function(){showHide(0);}oDiv.onmouseout = function(){showHide(-150);}//最佳化成一個方法function showHide(iCritical){clearInterval(timer);var speed;timer = setInterval(function(){if(oDiv.offsetLeft > iCritical){speed = -10;}else{speed = 10;}if(oDiv.offsetLeft == iCritical){clearInterval(timer);} else{oDiv.style.left = oDiv.offsetLeft + speed + "px";}},30);}//展開function show(){clearInterval(timer);timer = setInterval(function(){if(oDiv.offsetLeft>=0){clearInterval(timer);}else{oDiv.style.left = oDiv.offsetLeft + 10 + "px";}},30);}//隱藏function hide(){clearInterval(timer);timer = setInterval(function(){if(oDiv.offsetLeft==-150){//這裡是等於clearInterval(timer);}else{oDiv.style.left = oDiv.offsetLeft - 10 + "px";}},30);}}</script>