Article Detail

JS打字机效果

打字机 body{ margin: 0; padding: 0 0 50px; background: #CDDC39; font-family: 'STXin...

HTML 阅读 26.8万 8 分钟阅读 2017-10-24 10:17
GDScript3
208 行
<!DOCTYPE html>
<html lang="en">
<head>
	<meta name='viewport' content='width=device-width'>
	<meta charset="UTF-8">
	<title>打字机</title>
	<style>
		body{
			margin: 0;
			padding: 0 0 50px;
			background: #CDDC39;
			font-family: 'STXinwei';
			color: #333;
			font-size: 16px;
		}
		div:first-child{
			padding: 35px 0 15px;
			text-align: center;
		}
		button{
			margin: 0 50px;
			width: 100px;
			height: 35px;
			border: 0;
			outline: 0;
			background: #fff;
			font-weight: 800;
			border-radius: 5px;
			font-size: inherit;
			font-family: inherit;
		}
		#wrap{
			margin: 0 auto;
			padding: 20px 15px;
			width: 800px;
			max-width: 85%;
			background: #fff;
			border-radius: 10px;
			text-align: justify;
			line-height: 1.3em;
			font-family: inherit;
		}
	</style>
</head>
<body>
	<div>
		<button id="start">开始打字</button>
		<button id="change">切换光标</button>
	</div>
<pre id="wrap">
//打字机
function Typewriter(arg){

	//options
	var el = arg.el;
	var cursorFlash = arg.cursorFlash;
	var wordFlash = arg.wordFlash instanceof Array? arg.wordFlash : [0,400];
	var m = wordFlash[0];
	var n = wordFlash[1];

	//创建过就不要再创建了
	if(!el.typewriter){
		el.typewriter = true;
	}else{
		return false;
	}

	//初始化
	var text = el.innerHTML;
	var len = 0;
	var text_box = document.createElement('span');
	var cursor_box = document.createElement('span');
	cursor_box.innerHTML = '|';
	el.innerHTML = '';
	el.appendChild(text_box);
	el.appendChild(cursor_box);

	//光标闪闪
	setInterval(function (){
		if(cursor_box.show){
			cursor_box.style.opacity = 1;
			cursor_box.show = false;
		}else{
			cursor_box.style.opacity = 0;
			cursor_box.show = true;
		}
	},cursorFlash);

	//添加字符
	function addWords(){
		if(len<=text.length){
			text_box.innerHTML = text.slice(0,len);
			len++;
			setTimeout(addWords,Math.random()*(n-m)+m);
		}
	}
	//API
	this.changeCursor = function (){
		cursor_box.innerHTML = cursor_box.innerHTML == '|'? '_' : '|';
	}
	this.startWrite = function(){
		if(!text_box.canadd){
			text_box.canadd = true;
			addWords();
		}
	}
}

var wrap = document.querySelector('#wrap');
var start = document.querySelector('#start');
var change = document.querySelector('#change');

//创建打字机
var tw = new Typewriter({
	el: wrap,
	cursorFlash: 400,
	wordFlash: [0,400]
});

//开始
start.onclick = tw.startWrite;
//切换光标
change.onclick = tw.changeCursor;
</pre>

	<script>
		//打字机
		function Typewriter(arg){

			//options
			var el = arg.el;
			var cursorFlash = arg.cursorFlash;
			var wordFlash = arg.wordFlash instanceof Array? arg.wordFlash : [0,400];
			var m = wordFlash[0];
			var n = wordFlash[1];

			//创建过就不要再创建了
			if(!el.typewriter){
				el.typewriter = true;
			}else{
				return false;
			}

			//初始化
			var text = el.innerHTML;
			var len = 0;

			var text_box = document.createElement('span');
			text_box.id = 'typewriter-text';

			var cursor_box = document.createElement('span');
			cursor_box.id = 'typewriter-cursor';
			cursor_box.innerHTML = '|';

			el.innerHTML = '';
			el.appendChild(text_box);
			el.appendChild(cursor_box);

			//光标闪闪
			setInterval(function (){
				if(cursor_box.show){
					cursor_box.style.opacity = 1;
					cursor_box.show = false;
				}else{
					cursor_box.style.opacity = 0;
					cursor_box.show = true;
				}
			},cursorFlash);

			//添加字符
			function addWords(){
				if(len<=text.length){
					text_box.innerHTML = text.slice(0,len);
					len++;
					setTimeout(addWords,Math.random()*(n-m)+m);
				}
			}
			//API
			this.changeCursor = function (){
				cursor_box.innerHTML = cursor_box.innerHTML == '|'? '_' : '|';
			}
			this.startWrite = function(){
				if(!text_box.canadd){
					text_box.canadd = true;
					addWords();
				}
			}
		}


		var wrap = document.querySelector('#wrap');
		var start = document.querySelector('#start');
		var change = document.querySelector('#change');

		//创建打字机
		var tw = new Typewriter({
			el: wrap,
			cursorFlash: 400,
			wordFlash: [0,400]
		});

		//开始
		start.onclick = tw.startWrite;
		//切换光标
		change.onclick = tw.changeCursor;
	</script>
</body>
</html>
Comments 评论区
广西南宁市 2017-10-28 11:40

简单自动打字机效果,打完清空从头开始

GDScript3
28 行
<!doctype html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>Document</title>
</head>
<body>
	<div></div>
	<script type="text/javascript">
		var count=0;
		var newstr="";
		var other = "|"
		var odiv = document.getElementsByTagName('div')[0]
		var timer = setInterval(function(){
			var str = "哈哈哈哈哈哈哈哈哈哈哈哈嗝~哈哈哈哈哈哈哈哈哈哈哈哈哈嗝~";
			newstr += str[count];
			odiv.innerHTML = newstr + other;
			count++;
			if(count == str.length+1){
				odiv.innerHTML = "";
				newstr = "";
				count=0;
				timer;
			}
		},200)
	</script>
</body>
</html>
| | #0
Comment Form 留下评论
正在回复 #0
粘贴图片、拖拽文件,或点上面的按钮上传 图片会自动插入 [img] 标签,其他附件会自动插入 [attach] 标签。
正在上传...
提交前会先拉起旧项目同款第三方人机验证。

不再要求填写昵称;reply 会生成一条带楼层回链的新评论,quote 会附带完整引用块,并保持评论锚点跳转。

lizhenqiu blog is powered by lizhenqiu.com Version 6.9

Processed in 0.0214 second(s) W3C

本博客的所有原创作品采用 知识共享 署名-非商业性使用-相同方式共享 2.5 协议 进行许可

本站由 七七牛 云存储 阿阿里云 计算与安全服务 拍又拍云 CDN 加速 百百度智能 AAMH 布布集网 AI指南针AI

桂公网安备 45010302000998号 桂ICP备15007619号-1 中国互联网举报中心 建议使用谷歌浏览器浏览
Navigation 文章导航
⌂ ↓ ↑
100%
图片预览
Gallery 图集
0 张图片

正在整理正文和评论里的图片。

当前页还没有可展示的图片。
Quick Comment 快速评论

直接输入内容,提交时仍会走当前页面的人机验证。

正在上传...
操作提示