您好,登錄后才能下訂單哦!
這篇文章主要介紹了jQuery如何實(shí)現(xiàn)打字機(jī)效果,具有一定借鑒價(jià)值,感興趣的朋友可以參考下,希望大家閱讀完這篇文章之后大有收獲,下面讓小編帶著大家一起了解一下。
實(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)開(kāi)始的一個(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>
感謝你能夠認(rèn)真閱讀完這篇文章,希望小編分享的“jQuery如何實(shí)現(xiàn)打字機(jī)效果”這篇文章對(duì)大家有幫助,同時(shí)也希望大家多多支持億速云,關(guān)注億速云行業(yè)資訊頻道,更多相關(guān)知識(shí)等著你來(lái)學(xué)習(xí)!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。