您的位置:首页 > Web前端 > JavaScript

js 小实例 随机出现小飞机

2016-03-10 16:57 591 查看
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
<script>
window.onload = function(){
document.body.bgColor = "black";
window.setInterval("str()",1000);
}

function str()
{
var imgObj = document.createElement("img");
imgObj.setAttribute("src","img/1.jpg");

//图片的随机大小
var width = getRandom(20,100);
imgObj.setAttribute("width",width);

//位置
var x = getRandom(0,window.innerWidth);
var y = getRandom(0,window.innerHeight);
imgObj.setAttribute("style","position: absolute;left:"+x+"px;top:"+y+"px;")

//点击的时候消失
imgObj.setAttribute("onclick","removeImg(this)");
document.body.appendChild(imgObj);

}

//随机数
function getRandom(min,max){
var random = Math.random()*(max - min)+min;
random = Math.floor(random);
return random;
}
//移除img
function removeImg(obj)
{
document.body.removeChild(obj);
}
</script>
</head>
<body>
</body>
</html>


  
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: