飘浮盒子,随机色,随机宽高

来源:互联网 发布:大数据平台需求分析 编辑:程序博客网 时间:2024/05/21 17:33
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>漂浮框</title>
</head>
<style>
*{
margin: 0;
padding: 0;
list-style: none;
background:black;
}
.div{
width: 100px;
height: 100px;
background: red;
position: absolute;
}
#bigbox{
width: 1000px;
height: 500px;
margin: 60px auto;
position: relative;
border:1px solid #000;
overflow: hidden;
background: #ccc;
}
</style>
<body>
<div id="bigbox">
<div class="div"></div>
</div>
</body>
<script>
var div=document.getElementsByTagName('div')[1];
var x=1000;
var y=500;
var b=1;
var c=1;
var d=100;
var e=100;
var t=setInterval(move,5)
function move() {
if(div.offsetLeft+d>=x&&b>0||div.offsetLeft<=0&&b<0){
b=-b;div.style.background=bg();
div.style.width=Math.ceil(Math.random()*100)+"px";
div.style.height=Math.ceil(Math.random()*100)+"px";
 d=parseInt(div.style.width);
 e=parseInt(div.style.height);
}
if(div.offsetTop+e>=y&&c>0||div.offsetTop<=0&&c<0){
c=-c;div.style.background=bg();
div.style.width=Math.ceil(Math.random()*100)+"px";
div.style.height=Math.ceil(Math.random()*100)+"px";
d=parseInt(div.style.width);
e=parseInt(div.style.height);
}
div.style.marginLeft=div.offsetLeft+b+"px";
div.style.marginTop=div.offsetTop+c+"px";
}
function bg() {
            var r = Math.floor(Math.random() * 256);
            var g = Math.floor(Math.random() * 256);
            var b = Math.floor(Math.random() * 256);
            return "rgb(" + r + ',' + g + ',' + b + ")";
        }
</script>
</html>