code
<!DOCTYPE html>
<head>
<title>css3滑动</title>
<link rel="stylesheet" type="text/css" href="../css/reset.css">
<script type="text/javascript" src="../js/jquery-1.7.2.min.js"></script>
<style>
* {
margin: 0;
padding: 0
}
.main {
font-size: 24px;
width: 900px;
margin: 0 auto;
}
.main p {
margin-top: 10px;
}
.main input,
.main textarea {
border: 1px solid #ccc;
width: 300px;
padding: 5px;
}
.main input {
height: 30px;
line-height: 30px;
}
.main textarea {
width: 300px;
height: 100px;
}
.main button {
color: #fff;
background: blue;
width: 100px;
height: 30px;
line-height: 30px;
text-align: center;
border: none;
cursor: pointer;
}
.main button:hover {
opacity: .6
}
</style>
</head>
<body>
<div class="main">
<p>
<input id="colorInput" placeholder="请输入颜色值比如#ccc,red" />
</p>
<p>
<textarea id="textInput" placeholder="请输入文本内容"></textarea>
</p>
<p>
<input id="speedInput" placeholder="请输入打印速度:单位ms" />
</p>
<p>
<button onclick="doThis()">确定</button>
</p>
<div id="word-wrapper-js" class="word-wrapper-js"></div>
</div>
<script type="text/javascript">
var textPrint = {
init: function(config) {
this.id = $(config.id);
this.text = config.text || '你什么都没输入';
this.color = config.color || '#666';
this.speed = config.speed || 100;
//this.render();
//可链式调用
return this;
},
//渲染元素
render: function() {
var typeWord = '',
i = 0,
speed = this.speed,
_this = this,
cur = "_"
var typing = setInterval(function() {
if (_this.text[i] == "\n") {
typeWord = typeWord + "<br />"
}
typeWord += _this.text[i];
_this.id.css('color', _this.color).html(typeWord + cur);
i++;
if (i == _this.text.length) {
clearInterval(typing);
}
}, _this.speed)
}
}
function doThis() {
var color = $("#colorInput").val();
var text = $("#textInput").val();
var speed = $("#speedInput").val();
textPrint.init({
id: '#word-wrapper-js',
text: text,
color: color,
speed: speed
}).render();
}
</script>
</body>
</html>