js实现气球漂浮

开发工具与关键技术:

Adobe Dreamweaver CC 2019

Htmml部分:

<body>
	<div class="ballonBox" id="ballonBox">
	</div>
</body>

css部分:

<style>
		*{
			margin: 0px;
		}
		.ballonBox{
			height: 100vh;
			padding: 10px;
			box-sizing: border-box;
			overflow: hidden;
		}
		.ballon{
			width: 105px;
			height: 125px;
			border-radius: 75% 75% 70% 70%;
			position: relative;
			/*关键帧调用*/
			animation: a 16s ease infinite;
			margin: 20px 0px 0px 40px;
			float: left;
		}
		.ballon:before{
			content: "";/*插入内容为空*/
			display: block;/*块级元素展示出来*/
			width: 1px;
			height: 75px;
			background: rgba(100,65,23,1);
			position: absolute;
			left: 52px;
			top: 125px;
		}
		.ballon:after{
			content:"▲";
			display: block;
			position: absolute;
			top: 120px;
			left: 44px;
		}
		@keyframes a{
			0%{
				transform: translateY(100vh);
				opacity: 1;
			}
			100%{
				transform: translateY(-300vh);
				opacity: 0;
			}
		}
	</style>

 js部分:

<script>
	window.onload = function(){
		//获取元素
		var oballonBox = document.getElementById("ballonBox");
		//在页面上放一百个气球
		for(var i=0; i<100; i++){
			//创建元素
			var odiv = document.createElement("div");
			//为元素添加属性class
			odiv.className = "ballon";
			//设置元素样式
			odiv.style.cssText = setStyle();
			//将odiv添加到父元素oballonBox中
			oballonBox.appendChild(odiv);
		}
	}
	//设置元素样式的函数
	function setStyle(){
		
		var r = Math.floor(Math.random()*256);
		var g = Math.floor(Math.random()*256);
		var b = Math.floor(Math.random()*256);
		var t = Math.floor(Math.random()*6)+5;
		var mt = Math.floor(Math.random()*21+20);
		var ml = Math.floor(Math.random()*21+20);
		
		var rtStyle = 'background: rgba('+r+','+g+','+b+',1);'+
					  'box-shadow: -10px -10px 7px hsla('+(r-20)+','+(g-20)+','+(b-20)+',1)inset;'+
					  'color:rgba('+r+','+g+','+b+',1);'+
					  'animation: a '+t+'s ease infinite;'
					  'margin: '+mt+'px 0px 0px '+ml+'px;';
		return rtStyle;
	}
</script>

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值