本文實例講述了JS簡單實現浮動窗口效果。分享給大家供大家參考,具體如下:
HTML部分:
<!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"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>無標題文檔</title> <style type="text/css"> *{ margin:0; padding:0;} a img{ border:0;} </style> </head> <body> <div style="height:2000px; background:#ccc; display:none;"> </div> <!--div id="floatAd" style=" position:fixed !important; position:absolute; _top:expression(eval(document.documentElement.scrollTop+document.documentElement.clientHeight-this.offsetHeight)); z-index: 2147483647; left: 50%; margin-left: -240px !important; top: 50%; margin-top: -210px !important; background:url(images/onlineSay.jpg) no-repeat; width:487px; height:350px; cursor:pointer;"> <div onclick="closeFAd();" style=" width:26px; height:26px; float:right;"></div> <div style="width:487px; height:320px; float:right;" onclick="open_online();"></div> </div> <div id="floatSide" style=" width:141px; height:579px; position:fixed !important; position:absolute; _top:expression(eval(document.documentElement.scrollTop+document.documentElement.clientHeight-this.offsetHeight)); z-index: 99999; right:0; top:6%; background:url(images/onlineTel.gif);cursor:pointer;" onclick="open_online();></div--> </body> </html> <script type="text/javascript" src="online.js"></script>
JS部分:
// JavaScript Document //浮動廣告圖片 var floatAdImg = "images/onlineSay.jpg"; //浮動側欄圖片 var floatSideImg = "images/onlineTel.gif"; //打開在線溝通 function open_online() { window.open('http://p.qiao.baidu.com//im/index?siteid=7342332&ucid=7601325'); } //浮動廣告 document.writeln("<div id=\"floatAd\" style=\" position:fixed !important; position:absolute; _top:expression(eval(document.documentElement.scrollTop+document.documentElement.clientHeight-this.offsetHeight)); z-index: 2147483647; left: 50%; margin-left: -240px !important; top: 50%; margin-top: -210px !important; background:url("+floatAdImg+") no-repeat; width:487px; height:350px; cursor:pointer; display:none; \">"); document.writeln("<div onclick=\"closeFAd();\" style=\" width:26px; height:26px; float:right;\"></div>"); document.writeln("<div style=\"width:487px; height:320px; float:right;\" onclick=\"open_online();\"></div>"); document.writeln("</div>"); //浮動側欄 document.writeln("<div style=\" width:141px; height:579px; position:fixed !important; position:absolute; _top:expression(eval(document.documentElement.scrollTop+document.documentElement.clientHeight-this.offsetHeight)); z-index: 99999; right:0; top:6%; background:url("+floatSideImg+");cursor:pointer;\" onclick=\"open_online();\"></div>"); //關閉浮動廣告 function closeFAd() { document.getElementById('floatAd').style.display = 'none'; } //打開浮動廣告 function showFAd() { document.getElementById('floatAd').style.display = 'block'; } //打開浮動窗口 function showFloat() { document.getElementById('floatAd').style.display = 'block'; } //打開窗口 20 秒僅執行一次 setTimeout(showFAd,20000); //每個 30 秒執行一次 setInterval(showFloat,30000);
效果圖如下:
更多關於JavaScript相關內容感興趣的讀者可查看本站專題:《JavaScript遍歷算法與技巧總結》、《JavaScript切換特效與技巧總結》、《JavaScript查找算法技巧總結》、《JavaScript動畫特效與技巧匯總》、《JavaScript錯誤與調試技巧總結》、《JavaScript數據結構與算法技巧總結》及《JavaScript數學運算用法總結》
希望本文所述對大家JavaScript程序設計有所幫助。