js實(shí)現(xiàn)鍵盤(pán)自動(dòng)打字效果
最近在網(wǎng)上看到一個(gè)字符逐個(gè)出現(xiàn)的打字效果,覺(jué)得挺有趣的,想一想基本實(shí)現(xiàn)思路就是設(shè)置一個(gè)定時(shí)器逐然后逐個(gè)向容器中添加字符,于是就基于jQuery寫(xiě)了一個(gè)簡(jiǎn)單版的。
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>AutoType</title> </head> <body> <div id="autotype"></div> <script src="http://cdn.bootcss.com/jquery/1.9.1/jquery.min.js"></script> <script> $.fn.autotype = function (str, speed) { var self = this, defaultStr = '<p>我希望有個(gè)如你一般的人.</p><br>' +'<p>如山間清爽的風(fēng).</p><br>' +'<p>如古城溫暖的光.</p><br>' +'<p>從清晨到夜晚.</p><br>' +'<p>由山野到書(shū)房.</p><br>' +'<p>只要最后是你,就好.</p><br>',//將要添加的元素的默認(rèn)內(nèi)容 defaultSpeed = 100, str = str || defaultStr, speed = speed || defaultSpeed, index = 0, timer = setInterval(function () { var current = str.substr(index, 1); if (current == '<') { index = str.indexOf('>', index) + 1; } else { index++; } self.html(str.substring(0, index) + ( (index & 1) && (index != str.length) ? '_' : '')); if (index >= str.length) { clearInterval(timer); } }, speed); }; $("#autotype").autotype(); </script> </body> </html>
本人才疏學(xué)淺,總覺(jué)得自己寫(xiě)的方法比較簡(jiǎn)陋,于是搜索了一波資料,發(fā)現(xiàn)有個(gè)不錯(cuò)的jQuery插件Typed.js。
Type.js的基礎(chǔ)使用
<script src="jquery.js"></script> <script src="typed.js"></script> <script> $(function(){ $(".element").typed({ strings: ["First sentence.", "Second sentence."], typeSpeed: 0 }); }); </script> ... <span class="element"></span>
插件為用戶定制了許多默認(rèn)設(shè)置與效果
<script> $(function(){ $(".element").typed({ strings: ["First sentence.", "Second sentence."], // Optionally use an HTML element to grab strings from (must wrap each string in a <p>) stringsElement: null, // typing speed typeSpeed: 0, // time before typing starts startDelay: 0, // backspacing speed backSpeed: 0, // shuffle the strings shuffle: false, // time before backspacing backDelay: 500, // loop loop: false, // false = infinite loopCount: false, // show cursor showCursor: true, // character for cursor cursorChar: "|", // attribute to type (null == text) attr: null, // either html or text contentType: 'html', // call when done callback function callback: function() {}, // starting callback function before each string preStringTyped: function() {}, //callback for every typed string onStringTyped: function() {}, // callback for reset resetCallback: function() {} }); }); </script>
具體用法可以看看GitHub地址,帶注釋的源碼400多行,不算復(fù)雜。
以上就是本文的全部?jī)?nèi)容,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作能帶來(lái)一定的幫助,同時(shí)也希望多多支持腳本之家!
相關(guān)文章
淺談 Webpack 如何處理圖片(開(kāi)發(fā)、打包、優(yōu)化)
這篇文章主要介紹了淺談 Webpack 如何處理圖片(開(kāi)發(fā)、打包、優(yōu)化),小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2019-05-05手把手教你 CKEDITOR 4 實(shí)現(xiàn)Dialog 內(nèi)嵌 IFrame操作詳解
這篇文章主要介紹了手把手教你 CKEDITOR 4 實(shí)現(xiàn)Dialog 內(nèi)嵌 IFrame操作,結(jié)合實(shí)例形式分析了CKEDitor4 Dialog內(nèi)嵌IFrame具體操作步驟與相關(guān)注意事項(xiàng),需要的朋友可以參考下2019-06-06ASP中進(jìn)行HTML數(shù)據(jù)及JS數(shù)據(jù)編碼函數(shù)
在有些時(shí)候我們無(wú)法控制亂碼的出現(xiàn), 比如發(fā)送郵件的時(shí)候有些郵件顯示亂碼, 比如Ajax返回?cái)?shù)據(jù)總是亂碼. 怎么辦?2009-11-11JS實(shí)現(xiàn)根據(jù)數(shù)組對(duì)象的某一屬性排序操作示例
這篇文章主要介紹了JS實(shí)現(xiàn)根據(jù)數(shù)組對(duì)象的某一屬性排序操作,涉及javascript針對(duì)json數(shù)組的遍歷、比較、排序等相關(guān)操作技巧,需要的朋友可以參考下2019-01-01JS實(shí)現(xiàn)可點(diǎn)擊展開(kāi)與關(guān)閉的左側(cè)廣告代碼
這篇文章主要介紹了JS實(shí)現(xiàn)可點(diǎn)擊展開(kāi)與關(guān)閉的左側(cè)廣告代碼,通過(guò)鼠標(biāo)onClick事件調(diào)用自定義javascript函數(shù)實(shí)現(xiàn)頁(yè)面元素及樣式的顯示與隱藏效果,非常簡(jiǎn)單實(shí)用,需要的朋友可以參考下2015-09-09