基于jQuery實(shí)現(xiàn)的打字機(jī)效果
話不多說,請(qǐng)看實(shí)例代碼:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
<meta name="renderer" content="webkit">
<meta http-equiv="X-UA-Compatible" content="IE=Edge">
<meta name="keyword" content="">
<meta name="description" content="">
</head>
<body>
<div class="autotype" id="autotype">
<p>一場(chǎng)雨把我困在這里</p>
<br/>
<p>你溫柔的表情</p>
<p>會(huì)讓我傷心</p>
<br/>
<p>六月的雨,只是無(wú)情的你~</p>
</div>
<script src="http://file2.ci123.com/ast/js/jquery_172.js"></script>
<script>
$.fn.autotype = function(){
var $text = $(this);
console.log('this',this);
var str = $text.html();//返回被選 元素的內(nèi)容
var index = 0;
var x = $text.html('');
//$text.html()和$(this).html('')有區(qū)別
var timer = setInterval(function(){
//substr(index, 1) 方法在字符串中抽取從index下標(biāo)開始的一個(gè)的字符
var current = str.substr(index, 1);
if(current == '<'){
//indexOf() 方法返回">"在字符串中首次出現(xiàn)的位置。
index = str.indexOf('>', index) + 1;
}else{
index ++ ;
}
//console.log(["0到index下標(biāo)下的字符",str.substring(0, index)],["符號(hào)",index & 1 ? '_': '']);
//substring() 方法用于提取字符串中介于兩個(gè)指定下標(biāo)之間的字符
$text.html(str.substring(0, index) + (index & 1 ? '_': ''));
if(index >= str.length){
clearInterval(timer);
}
},100);
};
$("#autotype").autotype();
</script>
</body>
</html>
以上就是本文的全部?jī)?nèi)容,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作能帶來一定的幫助,同時(shí)也希望多多支持腳本之家!
相關(guān)文章
AMD異步模塊定義介紹和Require.js中使用jQuery及jQuery插件的方法
這篇文章主要介紹了AMD異步模塊定義介紹和Require.js中使用jQuery及jQuery插件的方法,需要的朋友可以參考下2014-06-06
jQuery中隊(duì)列queue()函數(shù)的實(shí)例教程
這篇文章主要介紹了jQuery中隊(duì)列queue()函數(shù)的實(shí)例教程,queue()函數(shù)為JavaScript函數(shù)的執(zhí)行順序控制操作提供了便利,需要的朋友可以參考下2016-05-05
jQuery :nth-child前有無(wú)空格的區(qū)別分析
基于jQuery實(shí)現(xiàn)Ajax驗(yàn)證用戶名是否可用實(shí)例
Jqyery中同等與js中windows.onload的應(yīng)用
jQuery實(shí)現(xiàn)獲取元素索引值index的方法
jQuery動(dòng)態(tài)移除和添加背景圖片的方法詳解

