js 小实例 随机出现小飞机

<!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>

  

原文地址:https://www.cnblogs.com/mingjixiaohui/p/5262569.html