Last active
October 21, 2016 03:53
-
-
Save dofy/ddf6457d3186a9f257245936d67a8f36 to your computer and use it in GitHub Desktop.
Type Script Demo https://jsfiddle.net/dofy/eo5cu3fa/1/
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html> | |
<head> | |
<meta charset="utf-8"> | |
<title>Type Script Demo</title> | |
<script src="//code.jquery.com/jquery-3.1.1.min.js"> </script> | |
<script src="main.js"></script> | |
<script> | |
var textScript = [ | |
'This is an example...', '.3', | |
'-21', | |
'This ', '.2', | |
'+is ', '.2', | |
'+an ', '.2', | |
'+example...', '.5', | |
'-21', | |
'我们', '.3', | |
'+是', '.3', | |
'+清单', '.3', | |
'-2', | |
'+轻单', '.7', | |
'zhege', '.3', | |
'-5', | |
'这个', '.3', | |
'+视界', '.3', | |
'-2', | |
'+世界', '.3', | |
'+是由', '.3', | |
'+空气', '.3', | |
'+,', '.3', | |
'+水', '.3', | |
'+和', '.3', | |
'+轻单', '.3', | |
'+组成的。', '.7', | |
' ', '.7']; | |
$(function () { | |
runScript('#debug', textScript, 70); | |
}); | |
</script> | |
<style media="screen"> | |
body { | |
font-family: 'courier new'; | |
} | |
h1 { | |
text-align: center; | |
} | |
.copy { | |
text-align: center; | |
} | |
#debug { | |
font-size: 1.4em; | |
width: 75%; | |
margin: 30px auto; | |
padding: 3px 5px; | |
background-color: black; | |
color: green; | |
} | |
#debug:after { | |
font-size: 1.4em; | |
content:'|'; | |
} | |
</style> | |
</head> | |
<body> | |
<h1>Type Script Demo</h1> | |
<div id="debug"></div> | |
<p class="copy">Copyright © 2016 phpz.org</p> | |
</body> | |
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function runScript(selector, script, delay) { | |
var output = $(selector), | |
texts = parseScript(script), | |
loop = 0, | |
count = texts.length; | |
setInterval(function () { | |
if (loop < count) { | |
output.text(texts[loop++]); | |
} else { | |
loop = 0; | |
} | |
}, delay); | |
function parseScript(script) { | |
var result = [], | |
last = ''; | |
for (var text, opt, n, m, i = 0, l = script.length; i < l; i++) { | |
text = script[i]; | |
opt = /^[\.\-\+]/.test(text) ? text[0] : ''; | |
switch (opt) { | |
case '-': | |
for (n = 0, m = parseInt(text, 10); n > m; n--) { | |
last = last.slice(0, -1); | |
result.push(last); | |
} | |
break; | |
case '+': | |
text = text.slice(1); | |
for (n = 0, m = text.length; n < m; n++) { | |
last += text.slice(n, n + 1); | |
result.push(last); | |
} | |
break; | |
case '.': | |
for (n = 0, m = parseInt(text.slice(1) || 1, 10); n < m; n++) { | |
result.push(last); | |
} | |
break; | |
default: | |
for (n = 0, m = text.length; n < m; n++) { | |
last = text.slice(0, n + 1); | |
result.push(last); | |
} | |
break; | |
} | |
} | |
return result; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment