Common JavaScript advertisement code

Source: Internet
Author: User
Nowadays, many website advertisements are in full swing. Now I will introduce the common code usage methods for floating couplet advertisements. The results are displayed at a resolution of 1024*768, the image can be automatically hidden at a resolution of 800*600 to avoid page hiding affecting the visitor's browsing content. The following code is required to achieve the effect:

Code:
VaR Delta = 0.015.
VaR collection;
Function floaters (){
This. Items = [];
This. additem = function (ID, X, Y, content)
{
Document. write ('<Div id =' + ID + 'style = "Z-INDEX: 0; position: absolute; width: 80px; Height: 60px; left:' + (typeof (X) = 'string '? Eval (x): x) + '; top:' + (typeof (y) = 'string '? Eval (y): Y) + '">' + content + '</div> ');

VaR newitem = {};
Newitem. Object = Document. getelementbyid (ID );
Newitem. x = X;
Newitem. Y = y;

This. items [This. Items. Length] = newitem;
}
This. Play = function ()
{
Collection = This. Items
Setinterval ('play () ', 10 );
}
}
Function play ()
{
If (screen. width <= 800)
{
For (VAR I = 0; I <collection. length; I ++)
{
Collection [I]. Object. style. Display = 'none ';
}
Return;
}
For (VAR I = 0; I <collection. length; I ++)
{
VaR followobj = Collection [I]. object;
VaR followobj_x = (typeof (Collection [I]. X) = 'string '? Eval (Collection [I]. X): Collection [I]. X );
VaR followobj_y = (typeof (Collection [I]. y) = 'string '? Eval (Collection [I]. Y): Collection [I]. y );

If (followobj. offsetleft! = (Document. Body. scrollleft + followobj_x )){
VaR dx = (document. Body. scrollleft + followObj_x-followObj.offsetLeft) * delta;
DX = (dx> 0? 1:-1) * Math. Ceil (math. Abs (dx ));
Followobj. style. Left = followobj. offsetleft + dx;
}

If (followobj. offsettop! = (Document. Body. scrolltop + followobj_y )){
VaR DY = (document. Body. scrolltop + followObj_y-followObj.offsetTop) * delta;
DY = (dy> 0? 1:-1) * Math. Ceil (math. Abs (dy ));
Followobj. style. Top = followobj. offsettop + dy;
}
Followobj. style. Display = '';
}
}

VaR thefloaters = new floaters ();
Thefloaters. additem ('followdiv1 ', 'document. body. clientWidth-100 ', 0, '</a> <br> <a href = aD link address target = _ blank> </> ');
Thefloaters. additem ('followdiv2', 0, 0, '<br> <a href = aD link address target = _ blank> </a> ');
Thefloaters. Play ();

Save the above Code as a JS file and call it on the page to achieve this effect! Modify the ad image address and connection address!

[Mouse sensing]

Different from the previous Code, when you move the mouse over an advertisement image, you can display another set advertisement image. The following code is required to achieve the effect:

Code:
Function bigshow (){
Document. All. div_250.style.visibility = 'visable ';
Document. All. div_80.style.visibility = 'den den ';
}
Function bighide (){
Document. All. div_80.style.visibility = 'visable ';
Document. All. div_250.style.visibility = 'den den ';
}

VaR ad_80 = new array (1 );
VaR ad_250 = new array (1 );

Ad_80 [0] = "<a href = http://www.dfeng.net target = _ blank> </a> ";
Ad_250 [0] = "<a href = http://www.dfeng.net target = _ blank> </a> ";

VaR imgheight;
VaR fubioleft;
Window. Screen. width> 800? Fubioleft = 15: fubioleft = 15

Document. write ('< Div id = floater_left style = "Z-INDEX: 25; left: '+ fubioleft + 'px; width: 28px; position: absolute; top: 42px; height: 22px "> ');

Ad_now = new date ();
Ad_id = ad_no1_getseconds () % 1

VaR adhead1 = "<Div id = div_80 style = 'position: absolute; width: 95px; Height: 62px; Z-index: 12' onmouseover = bigshow ();> ";
VaR adhead2 = "</div> <Div id = div_250 style = 'position: absolute; visibility: hidden; Z-index: 13; width: 80; height: 60 'onmouseout = bighide ();> ";

// Var adhead1 = "<Div id = div_80 style = 'position: absolute; width: 95px; Height: 62px; Z-index: 20 'onmouseout = '// mm_showhidelayers (/"div_250/",/"/",/"hide/") 'onmouseover = 'Mm _ showhidelayers (/"div_250 /", /"/",/"show/") '> ";
// Var adhead2 = "</div> <Div id = div_250 style = 'position: absolute; Z-index: 21; visibility: hidden; width: 80; height: 60 'onmouseover = 'Mm _ showhidelayers (/"div_250/",/"/",/"show/") 'onmouseout = 'Mm _ showhidelayers (/"div_250 /", /"/",/"hide/") '> ";

Document. Write (adhead1 + ad_80 [ad_id] + adhead2 + ad_250 [ad_id] + "</div> ");

Document. Write ("</div> ");
Self. onerror = NULL;
Currentx = currenty = 0;
Whichit = NULL;
Lastscrollx = 0; lastscrolly = 0;
NS = (document. layers )? 1: 0;
Ie = (document. All )? 1: 0;
Function HEARTBEAT (){
If (IE ){
Diffy = Document. Body. scrolltop;
Diffx = Document. Body. scrollleft ;}
If (NS ){
Diffy = self. pageyoffset; diffx = self. pagexoffset ;}
If (diffy! = Lastscrolly ){
Percent =. 5 * (diffy-lastscrolly );
If (percent> 0) percent = math. Ceil (percent );
Else percent = math. Floor (percent );
If (IE) document. All. floater_left.style.pixeltop + = percent;
If (NS) document. floater_left.top + = percent;
Lastscrolly = lastscrolly + percent;
}
If (diffx! = Lastscrollx ){
Percent =. 5 * (diffx-lastscrollx );
If (percent> 0) percent = math. Ceil (percent );
Else percent = math. Floor (percent );
If (IE) document. All. floater_left.style.pixelleft + = percent;
If (NS) document. floater_left.left + = percent;
Lastscrollx = lastscrollx + percent;
}
}

If (NS | IE) Action = Window. setinterval ("Heartbeat ()", 1); var ad_80 = new array (1 );
VaR ad_250 = new array (1 );

Ad_80 [0] = "<a href = http://www.dfeng.net target = _ blank> </a> ";
Ad_250 [0] = "<a href = http://www.dfeng.net target = _ blank> </a> ";

VaR imgheight;
VaR fubioleft;
Window. Screen. width> 800? Fubioleft = 15: fubioleft = 15

Document. write ('< Div id = floater_right style = "Z-INDEX: 25; left: '+ fubioleft + 'px; width: 28px; position: absolute; top: 42px; height: 22px "> ');

Ad_now = new date ();
Ad_id = ad_no1_getseconds () % 1

Function myload ()
{
If (navigator. appname = "Netscape ")
{
Document. div_right_80.pagex = + window. innerWidth-130;
Document. div_right_250.pagex = + window. innerWidth-300;
Mymove ();
}
Else
{
Div_right_80.style.left = Document. Body. offsetWidth-130;
Div_right_250.style.left = Document. Body. offsetWidth-300;
Mymove ();
}
}

Function mymove ()
{
If (document. NS)
{
Document. div_right_80.left = pagexoffset + window. innerWidth-130;
Document. div_right_250.left = pagexoffset + window. innerWidth-300;
SetTimeout ("mymove ();", 20)
}
Else
{
Div_right_80.style.left = Document. Body. scrollleft + document. Body. offsetWidth-145;
Div_right_250.style.left = Document. Body. scrollleft + document. Body. offsetWidth-300;
SetTimeout ("mymove ();", 20)
}
}

VaR adhead1 = "<Div id = div_right_80 style = 'position: absolute; width: 95px; Height: 60px; Z-index: 12 'onmouseover = bigshow_right ();> ";
VaR adhead2 = "</div> <Div id = div_right_250 style = 'position: absolute; visibility: hidden; Z-index: 13; width: 250; height: 250 'onmouseout = bighide_right ();> <Div align = right> ";

Document. Write (adhead1 + ad_80 [ad_id] + adhead2 + ad_250 [ad_id] + "</div> ");

Myload ()

Document. Write ("</div> ");
Self. onerror = NULL;
Currentx_right = currenty_right = 0;
Whichit_right = NULL;
Lastscrollx_right = 0; lastscrolly_right = 0;
NS = (document. layers )? 1: 0;
Ie = (document. All )? 1: 0;
Function heartbeat_right (){
If (IE ){
Diffy_right = Document. Body. scrolltop;
Diffx_right = Document. Body. scrollleft ;}
If (NS ){
Diffy_right = self. pageyoffset; diffx_right = self. pagexoffset ;}
If (diffy_right! = Lastscrolly_right ){
Percent_right =. 5 * (diffy_right-lastscrolly_right );
If (percent_right> 0) percent_right = math. Ceil (percent_right );
Else percent_right = math. Floor (percent_right );
If (IE) document. All. floater_right.style.pixeltop + = percent_right;
If (NS) document. floater_right.top + = percent_right;
Lastscrolly_right = lastscrolly_right + percent_right;
}
If (diffx_right! = Lastscrollx_right ){
Percent_right =. 5 * (diffx_right-lastscrollx_right );
If (percent_right> 0) percent_right = math. Ceil (percent_right );
Else percent_right = math. Floor (percent_right );
If (IE) document. All. floater_right.style.pixelleft + = percent_right;
If (NS) document. floater_right.left + = percent_right;
Lastscrollx_right = lastscrollx_right + percent_right;
}
}

If (NS | IE) Action = Window. setinterval ("heartbeat_right ()", 1 );
Function bigshow_right (){
Document. All. div_right_250.style.visibility = 'visable ';
Document. All. div_right_80.style.visibility = 'den den ';
}
Function bighide_right (){
Document. All. div_right_80.style.visibility = 'visable ';
Document. All. div_right_250.style.visibility = 'den den ';
} Document. Write ('');
Document. Write ('');
Document. Write ('');

Save the above Code as a JS file, and then use

Code:
<SCRIPT type = "text/JavaScript" Language = "javascript1.2" src = "*. js"> </SCRIPT>

You can call it. * indicates the name of your saved file! Modify the ad image address and connection address!

[Disable allowed]

Different from the previous two codes, an image button is added below the ad diagram to allow visitors to click to close the ad image. The following text box contains the Code required to achieve the effect:

Code:
VaR Delta = 0.015;
VaR collection;
VaR closeb = false;
Function floaters (){
This. Items = [];
This. additem = function (ID, X, Y, content)
{
Document. write ('<Div id =' + ID + 'style = "Z-INDEX: 10; position: absolute; width: 80px; Height: 60px; left:' + (typeof (X) = 'string '? Eval (x): x) + '; top:' + (typeof (y) = 'string '? Eval (y): Y) + '">' + content + '</div> ');

VaR newitem = {};
Newitem. Object = Document. getelementbyid (ID );
Newitem. x = X;
Newitem. Y = y;

This. items [This. Items. Length] = newitem;
}
This. Play = function ()
{
Collection = This. Items
Setinterval ('play () ', 30 );
}
}
Function play ()
{
If (screen. Width <= 800 | closeb)
{
For (VAR I = 0; I <collection. length; I ++)
{
Collection [I]. Object. style. Display = 'none ';
}
Return;
}
For (VAR I = 0; I <collection. length; I ++)
{
VaR followobj = Collection [I]. object;
VaR followobj_x = (typeof (Collection [I]. X) = 'string '? Eval (Collection [I]. X): Collection [I]. X );
VaR followobj_y = (typeof (Collection [I]. y) = 'string '? Eval (Collection [I]. Y): Collection [I]. y );

If (followobj. offsetleft! = (Document. Body. scrollleft + followobj_x )){
VaR dx = (document. Body. scrollleft + followObj_x-followObj.offsetLeft) * delta;
DX = (dx> 0? 1:-1) * Math. Ceil (math. Abs (dx ));
Followobj. style. Left = followobj. offsetleft + dx;
}

If (followobj. offsettop! = (Document. Body. scrolltop + followobj_y )){
VaR DY = (document. Body. scrolltop + followObj_y-followObj.offsetTop) * delta;
DY = (dy> 0? 1:-1) * Math. Ceil (math. Abs (dy ));
Followobj. style. Top = followobj. offsettop + dy;
}
Followobj. style. Display = '';
}
}
Function closebanner ()
{
Closeb = true;
Return;
}

VaR thefloaters = new floaters ();
//
Thefloaters. additem ('followdiv1 ', 'document. body. clientWidth-100 ', 0,' <a onclick = "closebanner (); "href = http://www.dfeng.net target = _ blank> </a> <br> ');
Thefloaters. additem ('followdiv2', 0, 0, '<a onclick = "closebanner (); "href = http://www.dfeng.net target = _ blank> </a> <br> ');
Thefloaters. Play ();

Save the above Code as a JS file, and then call it on the page to achieve this effect. * indicates the file name you saved! Modify the ad image address and connection address!

Floating couplet advertisement-code on the left

If you want to display only one side, the following code is required to achieve the effect on the left side:

Code:
VaR ad_float_left_src = "image address ";
VaR ad_float_left_url = "Address ";
VaR ad_float_left_type = "";
Document. NS = navigator. appname = "Microsoft Internet Explorer"

VaR imgheight_close
VaR imgleft
Window. Screen. width> 800? Imgheight_close = 120: imgheight_close = 120
Window. Screen. width> 800? Imgleft = 8: imgleft = 122
Function myload ()
{
Myleft. style. Top = Document. Body. scrolltop + document. Body. offsetheigh
T-imgheight_close;
Myleft. style. Left = imgleft;
Leftmove ();
}
Function leftmove ()
{
Myleft. style. Top = Document. Body. scrolltop + document. Body. offsetheigh
T-imgheight_close;
Myleft. style. Left = imgleft;
SetTimeout ("leftmove ();", 50)
}

Function mm_reloadpage (init) {// reloads the window if nav4
Resized
If (init = true) with (Navigator) {If (appname = "Netscape ")&&
(Parseint (appversion) = 4 )){
Document. mm_pgw = innerwidth; document. mm_pgh = innerheight;
Onresize = mm_reloadpage ;}}
Else if (innerwidth! = Document. mm_pgw | innerheight!
= Document. mm_pgh) location. Reload ();
}
Mm_reloadpage (true)

Function close_float_left (){
Myleft. style. Visibility = 'hiden ';
}

Document. Write ("<Div id = myleft style = 'position:
Absolute; width: 80; top: 300; left: 5; visibility: visible; Z-index: 1'>"
+ "<Style>"
+ "A. closefloat: Link, A. refloat: visited {text-
Decoration: none; color: #000000; font-size: 12px }"
+ "A. closefloat: active, A. refloat: hover {text-Decoration: underline; color: # 0000ff; font-size: 12px }"
+ "</Style>"
+ "<Table border = 0 cellpadding = 0 cellspacing = 0> <tr> <TD> ");

If (document. NS ){
If (ad_float_left_type! = "SWF ")
Document. Write ("<a href = '" + ad_float_left_url + "'target
= '_ Blank'> Height = 31 border = 0> </a> ");
Else
Document. Write ("<embed src = '" + ad_float_left_src + "'
Quality = high width = 80 Height = 80 type = 'application/X-Shockwave-
Flash 'id = changhongout> </embed> ");

Document. Write ("</TD> </tr> <TD width = 80 Height = 20
Align = right> <a href = 'javascript: close_float_left (); void (0 );'
Class = closefloat> <B> <font color = # ff0000> close </font> </B> </a> </TD> </tr>"
+ "</Table>"
+ "</Div> ");

Myload ()}

Save the above Code as a *. js file, and then use

Code:
<SCRIPT src = "*. js"> </SCRIPT>

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.