Compatible with JS floating advertisements of multiple browsers [recommendation] _ advertisement code

Source: Internet
Author: User
It is very smooth and automatically adapted to the floating advertisement Effect of screen resolution. It is displayed on the Flash and is not blocked by any page elements. It also supports floating of multiple images, and the speed and floating random position are good. Floating advertisements are the forms of advertisements that we often use. The necessary code of webmasters is something you can use.

<Title> JS floating ad </title> <style type = "text/css"> img {border: 0 ;} </style> <SCRIPT type = text/javascript> function addEvent (obj, evtType, func, cap) {cap = cap | false; if (obj. addEventListener) {obj. addEventListener (evtType, func, cap); return true;} else if (obj. attachEvent) {if (cap) {obj. setCapture (); return true;} else {return obj. attachEvent ("on" + evtType, func) ;}} else {return false ;}} function getPageScr Oll () {var xScroll, yScroll; if (self. pageXOffset) {xScroll = self. pageXOffset;} else if (document.doc umentElement & document.doc umentElement. scrollLeft) {xScroll = document.doc umentElement. scrollLeft;} else if (document. body) {xScroll = document. body. scrollLeft;} if (self. pageYOffset) {yScroll = self. pageYOffset;} else if (document.doc umentElement & document.doc umentElement. scrollTop) {yS Croll = document.doc umentElement. scrollTop;} else if (document. body) {yScroll = document. body. scrollTop;} arrayPageScroll = new Array (xScroll, yScroll); return arrayPageScroll;} function GetPageSize () {var xScroll, yScroll; if (window. innerHeight & window. scrollMaxY) {xScroll = document. body. scrollWidth; yScroll = window. innerHeight + window. scrollMaxY;} else if (document. body. scrollHeight> Document. body. offsetHeight) {xScroll = document. body. scrollWidth; yScroll = document. body. scrollHeight;} else {xScroll = document. body. offsetWidth; yScroll = document. body. offsetHeight;} var implements wwidth, then wheight; if (self. innerHeight) {bytes wwidth = self. innerWidth; required wheight = self. innerHeight;} else if (document.doc umentElement & document.doc umentElement. clientHeight) {mongowwidt H = document.doc umentElement. clientWidth; required wheight = document.doc umentElement. clientHeight;} else if (document. body) {required wwidth = document. body. clientWidth; required wheight = document. body. clientHeight;} if (yScroll <windowHeight) {pageHeight = windowHeight;} else {pageHeight = yScroll;} if (xScroll <windowWidth) {pageWidth = windowWidth ;} else {pageWidth = xScroll;} arrayPageSize = ne W Array (pageWidth, pageHeight, expires wwidth, expires wheight) return arrayPageSize;} var AdMoveConfig = new Object (); AdMoveConfig. isInitialized = false; AdMoveConfig. scrollX = 0; AdMoveConfig. scrollY = 0; AdMoveConfig. moveWidth = 0; AdMoveConfig. moveHeight = 0; AdMoveConfig. resize = function () {var winsize = GetPageSize (); AdMoveConfig. moveWidth = winsize [2]; AdMoveConfig. moveHeight = winsize [3]; AdMoveConfig. scroll ();} AdMoveConfig. scroll = function () {var winscroll = getPageScroll (); AdMoveConfig. scrollX = winscroll [0]; AdMoveConfig. scrollY = winscroll [1];} addEvent (window, "resize", AdMoveConfig. resize); addEvent (window, "scroll", AdMoveConfig. scroll); function AdMove (id) {if (! AdMoveConfig. isInitialized) {AdMoveConfig. resize (); AdMoveConfig. isInitialized = true;} var obj = document. getElementById (id); obj. style. position = "absolute"; var W = AdMoveConfig. moveWidth-obj.offsetWidth; var H = AdMoveConfig. moveHeight-obj.offsetHeight; var x = W * Math. random (), y = H * Math. random (); var rad = (Math. random () + 1) * Math. PI/6; var kx = Math. sin (rad), ky = Math. cos (rad); var dirx = (Math. random () <0.5? 1:-1), diry = (Math. random () <0.5? 1:-1); var step = 1; var interval; this. setLocation = function (vx, vy) {x = vx; y = vy;} this. setDirection = function (vx, vy) {dirx = vx; diry = vy;} obj. customMethod = function () {obj. style. left = (x + AdMoveConfig. scrollX) + "px"; obj. style. top = (y + AdMoveConfig. scrollY) + "px"; rad = (Math. random () + 1) * Math. PI/6; W = AdMoveConfig. moveWidth-obj.offsetWidth; H = AdMoveConfig. moveHeight-obj.offsetHeight; x = x + step * kx * di Rx; if (x <0) {dirx = 1; x = 0; kx = Math. sin (rad); ky = Math. cos (rad) ;}if (x> W) {dirx =-1; x = W; kx = Math. sin (rad); ky = Math. cos (rad);} y = y + step * ky * diry; if (y <0) {diry = 1; y = 0; kx = Math. sin (rad); ky = Math. cos (rad) ;}if (y> H) {diry =-1; y = H; kx = Math. sin (rad); ky = Math. cos (rad) ;}} this. run = function () {var delay = 10; interval = setInterval (obj. customMethod, delay); obj. onmouseover = function () {clearInterva L (interval);} obj. onmouseout = function () {interval = setInterval (obj. CustomMethod, delay) ;}} SCRIPT <! -- Floating start --> <! -- Floating End --> <SCRIPT type = text/javascript> <! -- Var ad1 = new AdMove ("ad1"); ad1.Run (); // multiple floating groups // var ad1 = new AdMove ("ad2"); // ad2.Run (); // --> SCRIPT
[Ctrl + A select all Note: If you need to introduce external Js, You need to refresh it to execute]

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.