js視窗邊緣滑出初級代碼

來源:互聯網
上載者:User

標籤:class   java   javascrip   style   lang   var   margin   interval   oct   

 1 <!DOCTYPE html> 2 <html lang="en"> 3 <head> 4     <meta charset="UTF-8"> 5     <title>practice</title> 6     <style type="text/css"> 7         *{margin:0px;padding:0px;} 8         #div1 {position:relative;left:-200px;width:200px;height:200px;margin-top:50px;background-color: pink;} 9         #share{background-color:lightblue;width:20px;height:50px;position: absolute;left:200px;top:75px;}10     </style>11     <script type="text/javascript">12         window.onload=function(){13             var div1=document.getElementById("div1");14             div1.onmouseover=startMove;15             /*不加括弧,就是正常的,滑鼠滑過,開始移動,表示滑鼠滑過,調用這個函數*/        16             /*div1.onmouseover=startMove();*/17             /*加了括弧之後,一開啟就開始移動,表示,把函數執行的結果給div1的滑鼠滑過事件,所以會去先執行函數,18             因為函數執行的結果就是offsetLeft==0,然後把這個結果給div1的滑鼠滑過事件,所以會出現一開啟就跑,19             這是為了得到函數執行的結果*/20             div1.onmouseout=startMove1;            21         };22         var timer=[‘1‘];23         /*必須要在外面定義,放在函數裡面定義就失敗了;24         含義:timer函數外定義,在兩個函數內同時調用,意為,當startMove1調用時,清楚startMove的timer,反之亦如此;25         這樣就不會同時運行兩個timer,一個函數執行timer前,先清楚另一個timer,這樣往前停止,再調用timer往後,26         否則同時運行會出錯;*/27         function startMove(){28             /*alert(timer.length);*/29             clearInterval(timer);30             timer=setInterval(31                             function(){32                                 if(div1.offsetLeft==0){clearInterval(timer)}//判斷要計算,所以不能帶‘px‘,用offsetLeft33                                     else {/*alert(div1.style.left);*/34                                     //offsetLeft顯示無‘px‘,style.left顯示有‘px‘35                 /*疑問:alert裡面這樣用,第一次顯示不出left值,後面才可以,為什嗎?36                 換成offsetLeft第一次就可以顯示出來*/    37                                         div1.style.left=div1.offsetLeft+10+‘px‘;38                                         //這個地方要+-,最好不帶‘px‘39                                         console.log(div1.offsetLeft);40                                     }}    41                                ,30);42         }43         function startMove1(){44             clearInterval(timer);45             timer=setInterval(46                             function(){47                                 if(div1.offsetLeft==-200){clearInterval(timer);}48                                     else{div1.style.left=div1.offsetLeft-10+‘px‘;49                                     console.log(div1.offsetLeft);}50                             }51                             ,30);52         }53     </script>54 </head>55 <body>56 57     <div id="div1">58         <span id="share">分享</span>59     </div>60 </body>61 </html>

 

js視窗邊緣滑出初級代碼

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.