<!DOCTYPE html><html><head><script type="text/javascript">var g_fDiv; //浮動DIVvar l2r = 1; //左右移動方向var t2b = 1; //上下移動方向/* * 注意: element.style.xxx ,必須先設定才能讀取。例如元素都有寬度,但是style.width不設定是沒有的*///移動函數function movePic(pic) { var xpos = parseInt(pic.style.left); var ypos = parseInt(pic.style.top); var picWidth = parseInt(pic.style.width); //浮動DIV大小 var picHeight = parseInt(pic.style.height); var bodyWidth = document.body.clientWidth;//主視窗大小,有沒有更好的擷取方式? var bodyHeight = document.body.clientHeight; var step = 1; if(l2r==1) { //當前是左->右 xpos += step; if(xpos+picWidth>=bodyWidth) { l2r = 0; xpos -= step; } }else{ xpos -= step; if(xpos<=0) { l2r = 1; xpos += step; } } if(t2b==1) { //當前是上->下 ypos += step; if(ypos+picHeight>=bodyHeight) { t2b = 0; ypos -= step; } }else{ ypos -= step; if(ypos<=0) { t2b = 1; ypos += step; } } pic.style.left = xpos + "px"; pic.style.top = ypos + "px"; setTimeout('movePic(g_fDiv)', 18); // 第一個參數為執行的語句,不是函數名}//浮動啟動函數, picFile-圖片檔案路徑function startFloat(picPath) { //插入一張圖片 var fDiv = document.createElement("div");fDiv.setAttribute("style","z-index:99;background-color:red;"); g_fDiv = fDiv; fDiv.innerHTML = '<img src="' + picPath + '" />'; fDiv.style.display = "block"; fDiv.style.position = "absolute";//fDiv.style.z-index = 999; //此語句報錯!不知為什嗎? fDiv.style.left = 0; fDiv.style.top = 0; fDiv.style.width = "400px"; fDiv.style.height = "300px"; document.body.appendChild(fDiv); //開始浮動 movePic(fDiv);}//相容瀏覽器的載入完成事件處理, func--自動執行的函數的名字function addLoadEvent(func) { var oldonload = window.onload; if(typeof window.onload != 'function' ) { window.onload = func; } else { window.onload = function() { oldonload(); func(); } }}addLoadEvent(function(){ startFloat('http://www.baidu.com/img/shouye_b5486898c692066bd2cbaeda86d74448.gif'); });</script></head><body style="height:800px;"></body></html>
另外,往往flash會顯示在最上面而把浮動圖片擋住,解決辦法是:
簡述:<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=7,0,19,0" width="963" height="233"> <param name="movie" value="images/top.swf" /> <param name="quality" value="high" /> <param name="wmode" value="opaque" /> <embed src="images/top.swf" quality="high" pluginspage="http://www.macromedia.com/go/getflashplayer" type="application/x-shockwave-flash" width="963" height="233"></embed> </object> 加入紅色字元代碼即可。 ------------------------------ 讓Flash不檔住浮動物件或層的關鍵參數:wmode=opaque 方法: 針對IE 在<object></object>內加上參數<param name="wmode" value="opaque" /> 針對FF 在<embed />內加上參數wmode="opaque" |