萬盛學電腦網

 萬盛學電腦網 >> 腳本專題 >> javascript >> js實現從右向左緩緩浮出網頁浮動層廣告的方法

js實現從右向左緩緩浮出網頁浮動層廣告的方法

   這篇文章主要介紹了js實現從右向左緩緩浮出網頁浮動層廣告的方法,可實現右側浮動廣告的定時彈出及點擊展開、折疊等功能,非常具有實用價值,需要的朋友可以參考下:

  ?

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 <!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> <script language="javascript"> var $ = function (d){return document.getElementById(d)}; var CLS={ create: function() { return function() { this.$ADD = function (fn){CLS.add(this,fn)}; this.init.apply(this, arguments); } }, add:function (obj,fn){ fn.apply(obj,arguments); }, enterFrame:function (){ this.onEnterFrame=function (){}; this.$PLAY = function (g){ this.enterFrameP = this.enterFrameP || 10; this.CLStimeIndex = CLS.ontimes.length; CLS.ontimes.push(this); window.clearTimeout(this.enterFrameTimeout); window.clearInterval(this.enterFrameInterval); if(g)this.enterFrameTimeout = window.setTimeout('CLS.ontimes['+this.CLStimeIndex+'].enterFrameInterval=window.setInterval("CLS.ontimes ['+this.CLStimeIndex+'].onEnterFrame()",'+this.enterFrameP+')',parseInt(g*1000)); else this.enterFrameInterval = window.setInterval("CLS.ontimes["+this.CLStimeIndex+"].onEnterFrame()",this.enterFrameP); } this.$STOP = function (){ window.clearInterval(this.enterFrameInterval); } this.$SET = function (p){ this.enterFrameP = p; } }, ontimes:new Array() }; CLS.Marquee = CLS.create(); CLS.Marquee.prototype = { init:function (button,box,speed){ this.box = $(box); this.tit = $(button) this.kong = $("kong"); this.onOpen = true; this.show = false; this.time = 0; this.speed = speed; this.kong.style.height = this.box.offsetHeight +"px"; this.Maxw = this.box.offsetWidth-this.tit.offsetWidth; this.box.style.right = -this.box.offsetWidth + "px"; this.boxTop = this.kong.offsetTop; var _t = this; this.tit.onclick = function (){ this.show = true; if(_t.onOpen){ _t.onEnterFrame = _t.close; _t.onOpen = false; }else{ _t.onEnterFrame = _t.open; _t.onOpen = true; } _t.$PLAY(); }; this.$ADD(CLS.enterFrame); this.onEnterFrame = this.open; this.$PLAY(); }, open:function(){ this.tit.innerHTML = "-"; var _r = parseInt(this.box.style.right); var _b = (0 - _r)/30; this.box.style.right = (_r + _b) +"px"; this.kong.style.top = (document.documentElement.scrollTop + this.boxTop) +"px"; if(_b==0 && !this.show){ this.time +=10; if(this.time>=this.speed*1000){ this.show = true; this.onOpen = false; this.$STOP(); this.onEnterFrame = this.close; this.$PLAY(); } } }, close:function (){ this.tit.innerHTML = "+"; var _r = parseInt(this.box.style.right); var _b = (-this.Maxw - _r)/5; this.box.style.right = Math.round(_r + _b) +"px"; this.kong.style.top = (document.documentElement.scrollTop + this.boxTop) +"px"; } }; window.onload = function (){ setTimeout(function(){new CLS.Marquee("tit","abc",10)},3000); //tit是點擊按鈕的Id ,abc是浮動塊的ID,10是顯示時長 }; </script> <style type="text/css"> <!-- #abc { border: 1px solid #003399; position: absolute; height: 150px; width: 220px; right: -100%; } #abc #tit { background-color: #0066FF; position: relative; height: 100%; width: 20px; color: #FFFFFF; text-align: center; float: left; } #kong { position: absolute; width: 100%; top: 100px; overflow: hidden; top: 100px; right: 0px; } .right { float: right; width: 190px; padding: 5px; } --> </style> </head> <body style="margin:0px;">
copyright © 萬盛學電腦網 all rights reserved