引入jquery(jquery-10.1.3.js)
自己写一个扩展方法
(function($) {
$.fn.typewriter = function() {
this.each(function() {
var $ele = $(this), str = $ele.html(), progress = 0;
$ele.html('');
var timer = setInterval(function() {
var current = str.substr(progress, 1);
if (current == '<') {
progress = str.indexOf('>', progress) + 1;
} else {
progress++;
}
$ele.html(str.substring(0, progress) + (progress & 1 ? '_' : ''));
if (progress >= str.length) {
clearInterval(timer);
}
}, 75);
});
return this;
};
})(jQuery);
3.html代码
I want to say:
Baby, I love you forever;
4.加载完页面调用方法
<script type="text/javascript">
$(function(){
$("#code").typewriter();
})
</script>