本文實例為大家分享了jQuery實現文字打印動態效果的具體代碼,供大家參考,具體內容如下
主體html
<!DOCTYPE html><html><head> <meta charset="utf-8"> <title>打印文字效果</title> <script type="text/javascript" src="js/jquery-3.2.1.min.js"></script> <script type="text/javascript"> <script/><head><body> <p id="typing">The furthest distance in the world.Is not between life and death.But when I stand in front of you.Yet you don't know that I love you</p></body>
對于JQuery的引用,可以先到JQuery官網下載相應的版本,引用的時候加入相應的目錄就可以了
接下來就是在script標簽中添加代碼實現文字的動態效果了,先上代碼
<script> $(dcument).ready(function(){ typing(); }) var text;//p標簽里對應的字符串 var index = 0;//text字符串的下標 var id;//setTimeout()的返回值,用于關閉clearTimeout(id) function typing() { text = $("#typing").text(); $("#typing").text(""); $("#typing").show(); typed(); } result = ""; function typed(){ result += text.charAt(index); $("#typing").text(result + (index & 1 ? "_" : " ")); if(index < text.length - 1) { index++; id = setTimeout("typed()", 100); } else clearTimeout(id); }</script>為什么顯示文字的時候是result+ (index & 1 ? "_" : " ")呢,當然是為了打印的動態效果了,當下標index為奇數的時候最后一個字符顯示為"_",當為偶數的時候顯示" ",這樣就能形成打印文字的那種動態效果。
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持武林網。
新聞熱點
疑難解答