跑马灯、霓虹灯效果

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title>霓虹灯效果</title>
		<style type="text/css">
			* {
				margin: 0;
				padding: 0;
			}
			#wp {
				padding:50px 0;
				display: flex;
				justify-content: center;
				align-items: center;
				border: 1px #f00 solid;
			}
			#wp div {
				margin:50px;
				border: 1px #f00 solid;
				background-color: #fff;
			}
			.nei {
				width:100px;
				height:100px;
			}
			.wai {
				width:508px;
				height:508px;
			}
			.btn-box {
				display: flex;
				justify-content: center;
				align-items: center;
				height:40px;
			}
			.btn-box button {
				height:40px;
				width:100px;
				text-align: center;
				line-height: 40px;
				cursor: pointer;
				margin:0 40px;
				font-size:20px;
			}
		</style>
	</head>
	<body>
		<div id="wp">
			<div class="wai">
				<div>
					<div>
						<div>
							<div class="nei"></div>
						</div>
					</div>
				</div>
			</div>
		</div>
		
		<div class="btn-box">
			<button type="button" id="outIn">从外到内</button>
			<button type="button" id="inOut">从内到外</button>
		</div>
	</body>
</html>
<script type="text/javascript">
	var outIn = document.getElementById("outIn");
	var inOut = document.getElementById("inOut");
	var wp = document.getElementById("wp");
	var divs = wp.getElementsByTagName('div');
	
	//声明一个存放颜色的数组
	var colors = ['#fff','#fff','#fff','#fff','#fff'];
	
	//点击从外到内
	outIn.onclick = function(){
		//随机颜色
		var r = Math.floor(Math.random()*256);
		var g = Math.floor(Math.random()*256);
		var b = Math.floor(Math.random()*256);
		var rgb = 'rgb('+r+','+g+','+b+')';
		//把颜色放入数组头部
		colors.unshift(rgb);
		
		// 从尾部删除一个颜色
		colors.pop();
		//for 循环divs数组,把对应位置上的colors存放的颜色,添加给对应的div
		//最外层的div是数组的头,最内层的div是数组的尾
		for(var i=0;i<divs.length;i++){
			divs[i].style.background = colors[i];
		}
	};
	
	//点击从内到外
	inOut.onclick = function(){
		//随机颜色
		var r = Math.floor(Math.random()*256);
		var g = Math.floor(Math.random()*256);
		var b = Math.floor(Math.random()*256);
		var rgb = 'rgb('+r+','+g+','+b+')';
		
		colors.push(rgb);
		colors.shift();
		
		for(var i=0;i<divs.length;i++){
			divs[i].style.background = colors[i];
		}
		
	};
	
	
	
</script>
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值