溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

jQuery如何實(shí)現(xiàn)打字機(jī)效果

發(fā)布時(shí)間:2021-06-24 14:54:24 來(lái)源:億速云 閱讀:103 作者:小新 欄目:web開(kāi)發(fā)

這篇文章主要介紹了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í)!

向AI問(wèn)一下細(xì)節(jié)

免責(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)容。

AI