js“分享到”側邊框伸縮實現

來源:互聯網
上載者:User

標籤: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>
 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.