JavaScript實現的漂浮廣告效果代碼

fdpg 9年前發布 | 2K 次閱讀 JavaScript

當鼠標在圖片上方時,圖片停止漂浮,點擊關閉按鈕可隱藏圖片,代碼簡單,直接插入網頁更換成自己的漂浮圖片就可以使用。

<body>
<div id="float" style=" position:absolute; z-index:3">
<img id="img1" src="http://www.sharejs.com/images/logo.png&quot; style=" border:1px #ccc solid" /><img id="img2" onClick="float.style.display='none'" src="關閉圖片" style=" position:absolute; margin-left:-13px;" />
</div>
<script type="text/javascript">
var xPos=0;
var yPos=0;
var directionX=true;
var directionY=true;
var step=1;
var speed=1;
var float=document.getElementById("float");
function move(){
    var height=document.documentElement.clientHeight;
    var width=document.documentElement.clientWidth;
    var imgHeight=float.offsetHeight;
    var imgWidth=float.offsetWidth;
    float.style.left=xPos+document.documentElement.scrollLeft+"px";
    float.style.top=yPos+document.documentElement.scrollTop+"px";
    if(directionX){xPos=xPos+step;}else{xPos=xPos-step;}
    if(xPos<=0){xPos=0;directionX=true;}
    if(xPos>=width-imgWidth){xPos=width-imgWidth;directionX=false;}

if(directionY){yPos=yPos+step;}else{yPos=yPos-step;}
if(yPos<=0){yPos=0;directionY=true;}
if(yPos>=height-imgHeight){yPos=height-imgHeight;directionY=false;}
t=setTimeout(move,speed);
}
float.onmouseover=function(){clearTimeout(t);}
float.onmouseout=function(){t=setTimeout("move()",speed);}

window.onload=move(); </script> </body> </pre>

 本文由用戶 fdpg 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!