前端笔记83——自动书写代码效果

前言

不知道小伙伴有没有想过程序自动写代码是怎么样的呢?我觉得挺有趣的,因为我是试过让程序自己写代码。开发中可能会遇到自动写一段文字的需求,那么自动写代码的JS程序就可以实现这种需求啦!有兴趣的小伙伴们可以复制下面的代码到编译器当中,运行查看效果。

自动写代码效果
<!DOCTYPE html>
<html>

	<head>
		<meta charset="UTF-8">
		<title></title>
		<style id="style-app">
			
		</style>
	</head>

	<body>
		<div id="content">
			<div id="style-text"></div>
			<div id="tooltips" style="display: none;">
				<div id="info">
					<div class="title">信息</div>
					<div class="content" id="content">恭喜你,中奖啦!!!</div>
					<div class="btn">
						<button id="confirm">确定</button>
					</div>
				</div>
			</div>
		</div>
		<script type="text/javascript">
			// es6中定义一个很长的字符串 `
			var style_text = `#style-text{
				width: 300px;
				height: 460px;
				border: 5px solid white;
				background-color: green;
				color: white;
			}
			#tooltips{
				width: 100%;
				height: 100%;
				background-color: agba(0,0,0,0.5);
				position: absolute;
				top: 0;
				z-index: 999;
				display: block !important;
			}
			#info{
				width: 400px;
				height: 200px;
				background-color: white;
				margin: 150px auto;
			}
			#info .title{
				width: 100%;
				height: 40px;
				background-color: #009F95;
				line-height: 40px;
				color: white;
				padding-left: 20px;
				box-sizing: border-box;
			}
			#info .btn button{
				background-color: #009F95;
				color: white;
				outline: none;
				font-size: 10px;
				width: 60px;
				height: 30px;
				margin-left: 300px;
			}
			#info .content{
				height: 120px;
				padding: 20px;
				box-sizing: border-box;
			}` 
			var style_app = document.getElementById("style-app")
			var style_text_obj = document.getElementById("style-text")
			// 表示结束的位置
			var n = 1
			var timer =setInterval(function(){
				//substring字符串截取,第一个参数开始位置,第二个参数结束位置
				style_app.innerHTML = style_text.substring(0,n)
				style_text_obj.innerHTML = style_text.substring(0,n)
				n++
				if(n === style_text.length){
					clearInterval(timer)
					alert("代码书写完成!")
				}
			},100)
		</script>
	</body>
</html>
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值