jQuery实现文字打印动态效果实例分享

js教程评论348 views阅读模式

本文主要介绍了基于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为奇数的时候最后一个字符显示为"_",当为偶数的时候显示" ",这样就能形成打印文字的那种动态效果。

相关推荐:

介绍一个javascript 实现文字打字效果的特效实例

js实现打印超市小票功能代码详解

js 客户端打印html 如何去掉页眉、页脚

以上就是jQuery实现文字打印动态效果实例分享的详细内容,更多请关注php教程其它相关文章!

企鹅博客
  • 本文由 发表于 2020年8月26日 17:23:18
  • 转载请务必保留本文链接:https://www.qieseo.com/395148.html

发表评论