首頁 >web前端 >js教程 >JavaScript實作漂浮廣告程式碼的實例總結

JavaScript實作漂浮廣告程式碼的實例總結

黄舟
黄舟原創
2017-12-04 13:51:547991瀏覽

我們在打開網頁的時候,我們會發現不管是什麼網頁都會有很多漂浮的廣告,有很多人都會在想,這樣的效果是怎麼實現的呢?今天我們就帶大家詳細介紹下JavaScript實作漂浮廣告程式碼的實例總結!

第一種 漂浮廣告 不符合W3C
JavaScript漂浮廣告程式碼,很不錯,程式碼精簡,不​​過一次只有漂一個,複製就能用了。希望站長朋友喜歡。

 
<html> 
<head> 
<title>漂浮广告</title> 
<body> 
<div id="codefans_net" style="position:absolute"> 
<!--链接地址--><a href="#" target="_blank"> 
<!--图片地址--><img src="/images/logo.gif" border="0"> 
</a> 
</div> 
<script> 
var x = 50,y = 60 
var xin = true, yin = true 
var step = 1 
var delay = 10 
var obj=document.getElementById("codefans_net") 
function float() { 
var L=T=0 
var R= document.body.clientWidth-obj.offsetWidth 
var B = document.body.clientHeight-obj.offsetHeight 
obj.style.left = x + document.body.scrollLeft 
obj.style.top = y + document.body.scrollTop 
x = x + step*(xin?1:-1) 
if (x < L) { xin = true; x = L} 
if (x > R){ xin = false; x = R} 
y = y + step*(yin?1:-1) 
if (y < T) { yin = true; y = T } 
if (y > B) { yin = false; y = B } 
} 
var itl= setInterval("float()", delay) 
obj.onmouseover=function(){clearInterval(itl)} 
obj.onmouseout=function(){itl=setInterval("float()", delay)} 
</script> 
</body> 
</html>

第二種漂浮廣告不符合標準
Js漂浮廣告代碼,比較經典的浮動廣告,到現還很實用,如果你是一位站長的話,這種程式碼是必備的,希望對你有用。

 
<html> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" /> 
<title>漂浮广告</title> 
</head> 
<body> 
<DIV id=img1 style="Z-INDEX: 100; LEFT: 2px; WIDTH: 59px; POSITION: absolute; TOP: 43px; HEIGHT: 61px; 
visibility: visible;"><a href="http://sc.jb51.net" target="_blank"><img src="/images/logo.gif" width="80" height="80" border="0"></a></DIV> 
<SCRIPT> 
var xPos = 300; 
var yPos = 200; 
var step = 1; 
var delay = 30; 
var height = 0; 
var Hoffset = 0; 
var Woffset = 0; 
var yon = 0; 
var xon = 0; 
var pause = true; 
var interval; 
img1.style.top = yPos; 
function changePos() 
{ 
width = document.body.clientWidth; 
height = document.body.clientHeight; 
Hoffset = img1.offsetHeight; 
Woffset = img1.offsetWidth; 
img1.style.left = xPos + document.body.scrollLeft; 
img1.style.top = yPos + document.body.scrollTop; 
if (yon) 
{yPos = yPos + step;} 
else 
{yPos = yPos - step;} 
if (yPos < 0) 
{yon = 1;yPos = 0;} 
if (yPos >= (height - Hoffset)) 
{yon = 0;yPos = (height - Hoffset);} 
if (xon) 
{xPos = xPos + step;} 
else 
{xPos = xPos - step;} 
if (xPos < 0) 
{xon = 1;xPos = 0;} 
if (xPos >= (width - Woffset)) 
{xon = 0;xPos = (width - Woffset); } 
} 

function start() 
{ 
img1.visibility = "visible"; 
interval = setInterval(&#39;changePos()&#39;, delay); 
} 
function pause_resume() 
{ 
if(pause) 
{ 
clearInterval(interval); 
pause = false;} 
else 
{ 
interval = setInterval(&#39;changePos()&#39;,delay); 
pause = true; 
} 
} 
start(); 
</SCRIPT> 
</body> 
</html>

上面兩個都是不支援標準宣告的 
第三種符合W3C的漂浮廣告程式碼 

 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" /> 
<title>符合W3C的漂浮广告代码-脚本之家</title> 
</head> 
<body> 
<script> 
document.write ("<DIV id=img1 style=&#39;Z-INDEX: 100; LEFT: 2px; WIDTH: 252px; POSITION: absolute; TOP: 43px; HEIGHT: 172px; ") 
document.write (" visibility: visible;&#39;> ") 
document.write (" <a href=&#39;http://s.jb51.net&#39; target=&#39;_blank&#39;> ") 
document.write (" <img src=&#39;/upload/201204/20120411210123566.gif&#39;style=border:none;/> ") 
document.write (" </a></DIV> ") 
var xPos = 300; 
var yPos = 200; 
var step = 1; 
var delay = 8; 
var height = 0; 
var Hoffset = 0; 
var Woffset = 0; 
var yon = 0; 
var xon = 0; 
var pause = true; 
var interval; 
img1.style.top = yPos; 
function changePos() 
{ 
width = document.documentElement.clientWidth; 
height = document.documentElement.clientHeight; 
Hoffset = img1.offsetHeight; 
Woffset = img1.offsetWidth; 
img1.style.left = xPos + document.documentElement.scrollLeft; 
img1.style.top = yPos + document.documentElement.scrollTop; 
if (yon) 
{yPos = yPos + step;} 
else 
{yPos = yPos - step;} 
if (yPos < 0) 
{yon = 1;yPos = 0;} 
if (yPos >= (height - Hoffset)) 
{yon = 0;yPos = (height - Hoffset);} 
if (xon) 
{xPos = xPos + step;} 
else 
{xPos = xPos - step;} 
if (xPos < 0) 
{xon = 1;xPos = 0;} 
if (xPos >= (width - Woffset)) 
{xon = 0;xPos = (width - Woffset); } 
} 
function start() 
{ 
img1.visibility = "visible"; 
interval = setInterval(&#39;changePos()&#39;, delay); 
} 
function pause_resume() 
{ 
if(pause) 
{ 
clearInterval(interval); 
pause = false;} 
else 
{ 
interval = setInterval(&#39;changePos()&#39;,delay); 
pause = true; 
} 
} 
start(); 
</script> 
</body> 
</html>

總結:

本文介紹了JavaScript實作漂浮廣告的幾種方法,每個方法使用都不一樣,小夥伴們可以根據自己的需求在選擇適合自己的,希望對你的工作有所幫助!

相關推薦:

#js 居中漂浮廣告

js 對聯廣告、漂浮廣告封裝類別

#JS隨機漂浮廣告程式碼具體實例

#

以上是JavaScript實作漂浮廣告程式碼的實例總結的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn