JS漂浮广告问题,鼠标移动到广告的位置会停下。但是就第一次这样做是可以的,第二次第三次都是不会停。。
代码如下(求高手解答):<html><head></head><body><divid="img"style="position:absolute;visibility:...
代码如下(求高手解答):
<html>
<head>
</head>
<body>
<div id="img" style="position:absolute;visibility:visible;left:2px; width:70px;top:43px;height:70px">
<a href="about:blank" target="_blank" style="display:inline"><img style="width:50px;height:50px;border:1px solid black" onMouseOver="clearInterval(interval)" onMouseOut="setInterval('ChangePos()',delay)"/></a>
<span>X</span>
</div>
<script>
var Xpos=100;
var Ypos=50;
var pause=true;
var xon=1;
var yon=1;
var step=1;
var delay=30;
var interval;
function ChangePos()
{
var areaWidth=document.body.clientWidth;
var areaHeight=document.body.clientHeight;
img.style.left=Xpos+document.body.scrollLeft;
img.style.top=Ypos+document.body.scrollTop;
var ImgWidth=img.offsetWidth;
var ImgHeight=img.offsetHeight;
//go step
if(xon)
{
Xpos=Xpos+step;
}
else
{
Xpos=Xpos-step;
}
if(yon)
{
Ypos=Ypos+step;
}
else
{
Ypos=Ypos-step;
}
//direction
if(Xpos<0)
{
xon=1;
Xpos=0;
}
if(Xpos>(areaWidth-ImgWidth))
{
xon=0;
Xpos=areaWidth-ImgWidth;
}
if(Ypos<0)
{
yon=1;
Ypos=0;
}
if(Ypos>(areaHeight-ImgHeight))
{
yon=0;
Ypos=areaHeight-ImgHeight;
}
}
function Start()
{
img.style.visibility="visible";
interval=setInterval('ChangePos()',delay);
}
Start();
</script>
</body>
</html> 展开
<html>
<head>
</head>
<body>
<div id="img" style="position:absolute;visibility:visible;left:2px; width:70px;top:43px;height:70px">
<a href="about:blank" target="_blank" style="display:inline"><img style="width:50px;height:50px;border:1px solid black" onMouseOver="clearInterval(interval)" onMouseOut="setInterval('ChangePos()',delay)"/></a>
<span>X</span>
</div>
<script>
var Xpos=100;
var Ypos=50;
var pause=true;
var xon=1;
var yon=1;
var step=1;
var delay=30;
var interval;
function ChangePos()
{
var areaWidth=document.body.clientWidth;
var areaHeight=document.body.clientHeight;
img.style.left=Xpos+document.body.scrollLeft;
img.style.top=Ypos+document.body.scrollTop;
var ImgWidth=img.offsetWidth;
var ImgHeight=img.offsetHeight;
//go step
if(xon)
{
Xpos=Xpos+step;
}
else
{
Xpos=Xpos-step;
}
if(yon)
{
Ypos=Ypos+step;
}
else
{
Ypos=Ypos-step;
}
//direction
if(Xpos<0)
{
xon=1;
Xpos=0;
}
if(Xpos>(areaWidth-ImgWidth))
{
xon=0;
Xpos=areaWidth-ImgWidth;
}
if(Ypos<0)
{
yon=1;
Ypos=0;
}
if(Ypos>(areaHeight-ImgHeight))
{
yon=0;
Ypos=areaHeight-ImgHeight;
}
}
function Start()
{
img.style.visibility="visible";
interval=setInterval('ChangePos()',delay);
}
Start();
</script>
</body>
</html> 展开
1个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询