使用 Javascript 代码,实现一个可移动的图标在网页上飘动,鼠标放在上面的时候,图标不动,移开后,图标又开始移动.
javascript 代码如下:
<script language='javascript'> 
var xPos = 0;
var yPos = 0;
var step = 1;
var delay = 10;
var height = 0;
var Hoffset = 0;
var Woffset = 0;
var yon = 0;
var xon = 0;
var xon = 0;
var interval; 
var img = document.getElementById('img');
img.style.top = 0; 
function changePos()
...{
width = document.body.clientWidth;
height = document.body.clientHeight;
Hoffset = img.offsetHeight;
Woffset = img.offsetWidth;
img.style.left = xPos + document.body.scrollLeft;
img.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() ...{
img.visibility = 'visible';
interval = setInterval('changePos()', delay);
} 
function mystop() 
...{
clearInterval(interval)
} 
start()
</script>页面中,代码如下:
<!-- FLASH浮动图标广代码-->
<span id='img' style='position: absolute;' onmouseover='mystop()' onmouseout='start()'>
<object classid='clsid:D27CDB6E-AE6D-11cf-96B8-444553540000' codebase='http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,29,0'
width='140' height='160'>
<param name='movie' value='http://www.myhome.com.cn/swf/sample.swf'>
<param name='quality' value='high'>
<param name="wmode" value="transparent">
<!--这里代码可使Flash背景透明 -->
<embed src='http://www.myhome.com.cn/swf/sample.swf' quality='high' pluginspage='http://www.macromedia.com/go/getflashplayer'
type='application/x-shockwave-flash' width='306' height='141'> </embed>
</object>
</span>

本文介绍了一种使用JavaScript实现网页上图标飘动效果的方法。当鼠标悬停在图标上时,图标停止移动;鼠标移开后,图标继续移动。通过调整变量控制图标的移动方向和速度。
1471

被折叠的 条评论
为什么被折叠?



