找了网上的js漂浮广告代码,但是浏览器可以无限地往下滚动,都滚动到网页外面去了。怎么解决这个问题
2个回答
展开全部
给你个不往外飞的,速度可以调整
<!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>
<style>
#div1{ width:100px; height:100px; background:#F00; position:absolute;}
#div1 h2{ width:10px; height:10px; float:right; margin-top:0px; border:#000 solid 2px; }
</style>
<script>
window.onload = function ()
{
var oDiv = document.getElementById('div1');
var oH2 = oDiv.getElementsByTagName('h2')[0];
oH2.onclick = function ()
{
oDiv.style.display='none';
}
oDiv.onmouseover = function ()
{
clearInterval(timer);
}
startMove ();
oDiv.onmouseout = function ()
{
startMove ();
}
}
var iSpeedX = 10;
var iSpeedY =10;
var timer = null;
function startMove ()
{
var oDiv = document.getElementById('div1');
timer=setInterval(function () {
var l = oDiv.offsetLeft+iSpeedX;
var t = oDiv.offsetTop+iSpeedY;
if(t>=document.documentElement.clientHeight-oDiv.offsetHeight)
{
iSpeedY*=-1;
t = document.documentElement.clientHeight-oDiv.offsetHeight;
}
else if (t<0)
{
iSpeedY*=-1;
t=0;
}
if(l>document.documentElement.clientWidth-oDiv.offsetWidth)
{
iSpeedX*=-1;
l = document.documentElement.clientWidth-oDiv.offsetWidth;
}
else if(l<=0)
{
iSpeedX *= -1;
l=0;
}
oDiv.style.left = l+'px';
oDiv.style.top = t+'px';
} ,30)
}
</script>
</head>
<body>
<input type="button" value="开始" onclick="startMove();" />
<div id="div1"><h2></h2></div>
</body>
</html>
<!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>
<style>
#div1{ width:100px; height:100px; background:#F00; position:absolute;}
#div1 h2{ width:10px; height:10px; float:right; margin-top:0px; border:#000 solid 2px; }
</style>
<script>
window.onload = function ()
{
var oDiv = document.getElementById('div1');
var oH2 = oDiv.getElementsByTagName('h2')[0];
oH2.onclick = function ()
{
oDiv.style.display='none';
}
oDiv.onmouseover = function ()
{
clearInterval(timer);
}
startMove ();
oDiv.onmouseout = function ()
{
startMove ();
}
}
var iSpeedX = 10;
var iSpeedY =10;
var timer = null;
function startMove ()
{
var oDiv = document.getElementById('div1');
timer=setInterval(function () {
var l = oDiv.offsetLeft+iSpeedX;
var t = oDiv.offsetTop+iSpeedY;
if(t>=document.documentElement.clientHeight-oDiv.offsetHeight)
{
iSpeedY*=-1;
t = document.documentElement.clientHeight-oDiv.offsetHeight;
}
else if (t<0)
{
iSpeedY*=-1;
t=0;
}
if(l>document.documentElement.clientWidth-oDiv.offsetWidth)
{
iSpeedX*=-1;
l = document.documentElement.clientWidth-oDiv.offsetWidth;
}
else if(l<=0)
{
iSpeedX *= -1;
l=0;
}
oDiv.style.left = l+'px';
oDiv.style.top = t+'px';
} ,30)
}
</script>
</head>
<body>
<input type="button" value="开始" onclick="startMove();" />
<div id="div1"><h2></h2></div>
</body>
</html>
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询