<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>浮动广告</title>
<style type="text/css">
body{
margin: 0; padding: 0;
}
.adv{
background: lightblue;
width: 100px;
line-height: 100px;
position: absolute;
color: white;
text-align: center;
border-radius: 100px;
}
</style>
</head>
<body>
<div id="adv" class="adv" style="left:0px;top:0px;">
浮动广告
</div>
<script type="text/javascript">
//设置两个变量
var lrFloat=true;
var tbFloat=true;
function changeMove(){
//获取屏幕的宽高
var widths=document.documentElement.clientWidth-100;
var heights=document.documentElement.clientHeight-100;
//获取控制的盒子
var obj=document.getElementById("adv");
//获取对象的left和top值
var xwidth=parseInt(obj.style.left);
var ywidth=parseInt(obj.style.top);
//判断广告left值是否大于屏幕width
if(xwidth>=widths) lrFloat=false;
//如果大于屏幕宽度,则left值逐步变小
if(lrFloat==false) obj.style.left=xwidth-1+'px';
//判断广告left值是否小于等于0
if(xwidth<=0) lrFloat=true;
//如果left值小于等于0,就实现left值的逐步增加
if(lrFloat==true) obj.style.left=xwidth+1+'px';
//上下判定方式一样
if(ywidth>=heights) tbFloat=false;
if(tbFloat==false) obj.style.top=ywidth-1+'px';
if(ywidth<=0) tbFloat=true;
if(tbFloat==true) obj.style.top=ywidth+1+'px';
}
setInterval("changeMove()",10);
</script>
</body>
</html>
<html>
<head>
<meta charset="utf-8">
<title>浮动广告</title>
<style type="text/css">
body{
margin: 0; padding: 0;
}
.adv{
background: lightblue;
width: 100px;
line-height: 100px;
position: absolute;
color: white;
text-align: center;
border-radius: 100px;
}
</style>
</head>
<body>
<div id="adv" class="adv" style="left:0px;top:0px;">
浮动广告
</div>
<script type="text/javascript">
//设置两个变量
var lrFloat=true;
var tbFloat=true;
function changeMove(){
//获取屏幕的宽高
var widths=document.documentElement.clientWidth-100;
var heights=document.documentElement.clientHeight-100;
//获取控制的盒子
var obj=document.getElementById("adv");
//获取对象的left和top值
var xwidth=parseInt(obj.style.left);
var ywidth=parseInt(obj.style.top);
//判断广告left值是否大于屏幕width
if(xwidth>=widths) lrFloat=false;
//如果大于屏幕宽度,则left值逐步变小
if(lrFloat==false) obj.style.left=xwidth-1+'px';
//判断广告left值是否小于等于0
if(xwidth<=0) lrFloat=true;
//如果left值小于等于0,就实现left值的逐步增加
if(lrFloat==true) obj.style.left=xwidth+1+'px';
//上下判定方式一样
if(ywidth>=heights) tbFloat=false;
if(tbFloat==false) obj.style.top=ywidth-1+'px';
if(ywidth<=0) tbFloat=true;
if(tbFloat==true) obj.style.top=ywidth+1+'px';
}
setInterval("changeMove()",10);
</script>
</body>
</html>