最近幾天做了一個項目,原來是用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>
代碼到此結束了,希望對大家有所幫助!