JavaScript代碼實現左右上下自動晃動自動移動,javascript代碼
最近幾天做了一個項目,原來是用css3動畫做的,由於不相容IE,改成用js做了,特此分享給大家,供大家參考,代碼有bug歡迎提出,寫的不好還請見諒!
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en"><head><meta http-equiv="Content-Type" content="text/html;charset=UTF-8" /><title>float left and top</title><style type="text/css">.w1000{position:relative;width:1000px;margin:0 auto;}.positionpub{position:absolute;}.positionpub p{width:50px;height:50px;background:#333;color:#fff;line-height:50px;text-align:center;position:absolute;}.ad_Float{top:50px;left:50px;}.ad_Float1{top:150px;left:250px;}.ad_Float2{top:250px;left:450px;}</style></head><body><div class="w1000"><div class="ad_Float positionpub"><p id="ad_Float">left</p></div><div class="ad_Float1 positionpub"><p id="ad_Float1">right</p></div><div class="ad_Float2 positionpub"><p id="ad_Float2">down</p></div></div><script type="text/javascript">var x = 0, y = 0 , x1 = 0;var xin = true, yin = true;var step = 1,step2 = 2;var delay = 10;var obj = document.getElementById("ad_Float");var obj1 = document.getElementById("ad_Float1");var obj2 = document.getElementById("ad_Float2");function ad_Float() {var L = 0;var R = 100;obj.style.left = x + document.documentElement.scrollLeft + "px";x = x + step * (xin ? 1 : -1);if (x < L) {xin = true;x = L;}if (x > R) {xin = false;x = R;}};function ad_Float1() {var L1 = 0;var R1 = 100;obj1.style.left = x1 + document.documentElement.scrollLeft + "px";x1 = x1 + step * (xin ? 1 : -1);if (x1 < L1) {xin = true;x1 = L1;}if (x1 > R1) {xin = false;x1 = R1;}};function ad_Float2() {var T = 0;var B = 150;obj2.style.top = y + document.documentElement.scrollTop + "px";y = y + step2 * (yin ? 1 : -1);if (y < T) {yin = true;y = T;}if (y > B) {yin = false;y = B;}};var itl = setInterval("ad_Float()", delay);var itl1 = setInterval("ad_Float1()", delay);var itl2 = setInterval("ad_Float2()", delay);</script></body></html>
代碼到此結束了,希望對大家有所協助!
您可能感興趣的文章:
- js判斷用戶端是iOS還是Android等移動終端的方法
- JS onmousemove滑鼠移動座標接龍DIV效果執行個體
- js 滑鼠移動顯示圖片的簡單一實例
- js如何判斷使用者是在PC端和還是移動端訪問
- js動態移動捲軸至底部範例程式碼
- javascript行動裝置Web開發中對touch事件的封裝執行個體
- js實現鍵盤控制DIV移動的方法
- 原生js實現移動開發輪播圖、相簿滑動特效
- js實現touch移動觸屏滑動事件