找回密码
 免费注册

[jQuery] 利用jQuery实现打字机字幕效果实例代码

[复制链接]
admin 发表于 2023-5-24 01:39:07 | 显示全部楼层 |阅读模式
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="UTF-8">
  5. <title></title>
  6. <meta name="renderer" content="webkit">
  7. <meta http-equiv="X-UA-Compatible" content="IE=Edge">
  8. <meta name="keyword" content="">
  9. <meta name="description" content="">
  10. </head>
  11. <body>
  12. <div class="autotype" id="autotype">
  13.         <p>一场雨把我困在这里</p>
  14.         <br/>
  15.         <p>你温柔的表情</p>
  16.         <p>会让我伤心</p>
  17.         <br/>
  18.         <p>六月的雨,只是无情的你~</p>
  19. </div>
  20. <script src="http://file2.ci123.com/ast/js/jquery_172.js"></script>
  21. <script>

  22.         $.fn.autotype = function(){
  23.                 var $text = $(this);
  24.                 console.log('this',this);
  25.                
  26.                 var str = $text.html();//返回被选 元素的内容
  27.                
  28.                 var index = 0;
  29.                 var x = $text.html('');
  30.                 //$text.html()和$(this).html('')有区别
  31.                
  32.                 var timer = setInterval(function(){
  33.                         //substr(index, 1) 方法在字符串中抽取从index下标开始的一个的字符
  34.                         var current = str.substr(index, 1);
  35.                        
  36.                         if(current == '<'){
  37.                         //indexOf() 方法返回">"在字符串中首次出现的位置。
  38.                                 index = str.indexOf('>', index) + 1;
  39.                         }else{
  40.                                 index ++ ;
  41.                         }
  42.                        
  43.                         //console.log(["0到index下标下的字符",str.substring(0, index)],["符号",index & 1 ? '_': '']);
  44.                         //substring() 方法用于提取字符串中介于两个指定下标之间的字符
  45.                         $text.html(str.substring(0, index) + (index & 1 ? '_': ''));
  46.                         if(index >= str.length){
  47.                                 clearInterval(timer);
  48.                         }
  49.                 },100);
  50.         };
  51.        
  52.         $("#autotype").autotype();
  53. </script>
  54. </body>
  55. </html>
复制代码


回复

使用道具 举报

您需要登录后才可以回帖 登录 | 免费注册

本版积分规则

QQ|Archiver|手机版|小黑屋|信息共享网

GMT+8, 2024-5-15 01:50 , Processed in 0.085937 second(s), 18 queries .

Powered by Discuz! X3.5

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表