基于jQuery实现文字打印动态效果

<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>打印文字效果</title> <script type="text/javascript" src="https://www.jb51.net/js/jquery-3.2.1.min.js"></script> <script type="text/javascript"> <script/> <head> <body> <p>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为奇数的时候最后一个字符显示为"_",当为偶数的时候显示" ",这样就能形成打印文字的那种动态效果。

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wypjps.html