Home > Web Front-end > JS Tutorial > body text

Specific examples of JS random floating advertising code_javascript skills

WBOY
Release: 2016-05-16 17:14:19
Original
1667 people have browsed it

Copy code The code is as follows:


var xPos=0,yPos=0;//x, y-axis coordinates
var xon= 0;//The picture moves in the x-axis direction
var yon=0;//The picture moves in the y-axis direction
var step=1; //Moving distance
var img=document.getElementByIdx_x("float ");//Picture layer

function floatP()
{

var width=document.body.clientWidth;//Browser width
var height=document.body .clientHeight;//Browser height
var Hoffset=img.offsetHeight;//Image height
var Woffset=img.offsetWidth;//Image width

img.style.left=xPos document .body.scrollLeft;//The distance between the image and the left side of the browser
img.style.top=yPos document.body.scrollTop;//The distance between the image and the top of the browser

if(yon==0 ){
yPos=yPos step;//The picture moves up and down in the y-axis direction
}else{
yPos=yPos-step;
}
if(yPos<0){// Float to the top and move down along the y-axis
yon=0;
yPos=0;
}
if(yPos>=(height-Hoffset)){//Float to the low end, Move upward along the y-axis
yon=1;
yPos=(height-Hoffset);
}

if(xon==0){//x-axis moves right
xPos=xPos step;
}else{
xPos=xPos-step;//The x-axis moves left
}

if(xPos<0){//Float to When moving to the left, move right along the x-axis
xon=0;
Move left along the x-axis
xon=1;
xPos=(width-Woffset);
}

setTimeout("floatP()",30); // Timing call.

}
window.onload=floatP();







Copy code The code is as follows:

<script><br>var x = 50, y = 60 //The initial position of the floating layer corresponds to the initial X coordinate and Y coordinate of the layer respectively <br>var xin = true, yin = true //Determine whether the X coordinate and Y coordinate of the layer are within the control range. If xin is true, the layer moves to the right, otherwise to the left; if yin is true, the layer moves downward, otherwise upward <br>var step = 1 //Layer The step length of movement. The larger the value, the faster the movement speed. <br>var delay = 10 //The time interval for layer movement, in milliseconds. The smaller the value, the faster the movement speed. <br>var obj=document.getElementByIdx_x("float ") //Capture the layer with id ad as a floating target<br>function floatAD() {<br>var L=T=0 //The left boundary (L) and upper boundary (T) coordinates of the layer movement range<br>var R= document.body.clientWidth-obj.offsetWidth //The right boundary of the layer movement<br>var B = document.body.clientHeight-obj.offsetHeight //The lower boundary of the layer movement<br>obj.style.left = x document.body.scrollLeft //Update the X coordinate of the layer to realize movement in the Within the visible range <br>obj.style.top = y document.body.scrollTop //Update the Y coordinate of the layer to achieve movement in the Y-axis direction; document.body.scrollTop is the downward distance of the scroll bar in the document area , to ensure that the layer is still within the visible range when the scroll bar is pulled down<br>x = x step*(xin?1:-1) //Determine the movement direction of the layer on the X axis by judging the range of the layer<br>if (x < L) { xin = true; x = L} //Handling when the layer exceeds the left boundary<BR>if (x > R){ xin = false; x = R} //When the layer exceeds the right boundary Processing<br>y = y step*(yin?1:-1) //Determine the movement direction of the layer on the Y axis by judging the range of the layer<br>if (y < T) { yin = true; y = T } //Handling when the layer exceeds the upper boundary<BR>if (y > B) { yin = false; y = B } //Handling when the layer exceeds the lower boundary<br>}<br>var itl= setInterval("floatAD()", delay) //Execute the floatAD function every delay seconds<br>obj.onmouseover=function(){clearInterval(itl)} //The layer clears the above interval event when the mouse moves up, to achieve The effect of stopping the movement of the layer when the mouse moves up<br>obj.onmouseout=function(){itl=setInterval("floatAD()", delay)} //The layer starts interval events when the mouse moves away to realize the layer's movement when the mouse moves up The effect of continuing to move when the mouse is removed <br></script>

Note:
Insert Flash and floating ads into the web page, you will find that the floating ads float to the Flash The time will be below,
How do we solve this problem?
The rest is very simple
Just add the following statement to the flash code position

Example:
If the following is the code where the flash is located:

Copy code The code is as follows:






< /embed>

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!