Javascrpit特效之打字机效果

简介:

今天来看看怎么实现炫酷的打字机效果。即把一段话一个字一个字的显示出来。

效果图:


实现思路:

首先规定好显示字数的速度即settimeout执行间隔用来控制每个字之间输出速度。再把判断段落的总字数,循环段落总字数来实现一个字一个字的输出。

js代码:



  
  
  1. var theNewsNum;
  2. var theAddNum;
  3. var totalNum;
  4. var CurrentPosion= 0;
  5. var theCurrentNews;
  6. var theCurrentLength;
  7. var theNewsText;
  8. var theTargetLink;
  9. var theCharacterTimeout;
  10. var theNewsTimeout;
  11. var theBrowserVersion;
  12. var theWidgetOne;
  13. var theWidgetTwo;
  14. var theSpaceFiller;
  15. var theLeadString;
  16. var theNewsState;
  17. function startTicker(){
  18. // ------ 设置初始数值
  19. theCharacterTimeout = 50; //字符间隔时间
  20. theNewsTimeout = 2000; //新闻间隔时间
  21. theWidgetOne = "_"; //新闻前面下标符1
  22. theWidgetTwo = "-"; //新闻前面下标符
  23. theNewsState = 1;
  24. theNewsNum = document.getElementById( "incoming").children.AllNews.children.length; //新闻总条数
  25. theAddNum = document.getElementById( "incoming").children.AddNews.children.length; //补充条数
  26. totalNum =theNewsNum+theAddNum;
  27. theCurrentNews = 0;
  28. theCurrentLength = 0;
  29. theLeadString = " ";
  30. theSpaceFiller = " ";
  31. runTheTicker();
  32. }
  33. // --- 基础函数
  34. function runTheTicker(){
  35. if(theNewsState == 1){
  36. if(CurrentPosion<theNewsNum){
  37. setupNextNews();
  38. }
  39. else{
  40. setupAddNews();
  41. }
  42. CurrentPosion++;
  43. if(CurrentPosion>=totalNum||CurrentPosion>= 1){
  44. CurrentPosion= 0; //最多条数不超过num_gun条
  45. }
  46. }
  47. if(theCurrentLength != theNewsText.length){
  48. drawNews();
  49. }
  50. else{
  51. closeOutNews();
  52. }
  53. }
  54. // --- 跳转下一条新闻
  55. function setupNextNews(){
  56. theNewsState = 0;
  57. theCurrentNews = theCurrentNews % theNewsNum;
  58. theNewsText = document.getElementById( "AllNews").children[theCurrentNews].children.Summary.innerText;
  59. theTargetLink = document.getElementById( "AllNews").children[theCurrentNews].children.Summary.children[ 0].href;
  60. theCurrentLength = 0;
  61. document.all.hottext.href = theTargetLink;
  62. theCurrentNews++;
  63. }
  64. function setupAddNews() {
  65. theNewsState = 0;
  66. theCurrentNews = theCurrentNews % theAddNum;
  67. theNewsText = document.getElementById( "AllNews").children[theCurrentNews].children.Summary.innerText;
  68. theTargetLink = document.getElementById( "AllNews").children[theCurrentNews].children.Summary.children[ 0].href;
  69. theCurrentLength = 0;
  70. document.all.hottext.href = theTargetLink;
  71. theCurrentNews++;
  72. }
  73. // --- 滚动新闻
  74. function drawNews(){
  75. var myWidget;
  76. if((theCurrentLength % 2) == 1){
  77. myWidget = theWidgetOne;
  78. }
  79. else{
  80. myWidget = theWidgetTwo;
  81. }
  82. document.all.hottext.innerHTML = theLeadString + theNewsText.substring( 0,theCurrentLength) + myWidget + theSpaceFiller;
  83. theCurrentLength++;
  84. setTimeout( "runTheTicker()", theCharacterTimeout);
  85. }
  86. // --- 结束新闻循环
  87. function closeOutNews(){
  88. document.all.hottext.innerHTML = theLeadString + theNewsText + theSpaceFiller;
  89. theNewsState = 1;
  90. setTimeout( "runTheTicker()", theNewsTimeout);
  91. }
  92. window.onload=startTicker;


原文发布时间:2018年06月30日

本文来源CSDN博客如需转载请紧急联系作者

相关文章
|
7月前
|
人工智能 安全 物联网
《鸿蒙系统中人工智能驱动的智能助手:应用模式与未来航向》
在数字化时代,人工智能与操作系统的融合成为科技变革的核心力量。鸿蒙系统作为华为自主研发的分布式操作系统,为智能助手提供了广阔舞台。通过语音交互、多模态融合、场景感知与跨设备协同,智能助手实现了便捷操控、深度交互和主动服务。未来,借助大模型赋能、物联网深度融合及强化隐私保护,智能助手将推动全场景服务创新,助力开发者生态繁荣,开启万物互联的智能交互新时代。
439 12
|
程序员 开发者
IDEA插件-Rainbow Brackets彩虹括号
DEA插件-Rainbow Brackets是一款用于增强IDEA开发环境的工具,它可以帮助开发者更好地管理和展示代码中的括号。
3402 0
IDEA插件-Rainbow Brackets彩虹括号
|
人工智能 中间件 物联网
移动应用与系统:探索移动应用开发和操作系统的奥秘
本文将深入探讨移动应用开发和操作系统的基本概念、关键技术及其发展趋势。我们将从移动应用的生命周期、开发工具和技术栈的选择,到移动操作系统的架构设计、性能优化,再到未来的发展方向,如人工智能、物联网和5G技术的融合,为您全面解析这个充满创新与机遇的领域。无论您是开发者、设计师还是对移动技术感兴趣的读者,本文都将为您提供丰富的信息和启发。
233 0
|
12月前
|
负载均衡 应用服务中间件 Apache
Tomcat负载均衡原理详解及配置Apache2.2.22+Tomcat7
Tomcat负载均衡原理详解及配置Apache2.2.22+Tomcat7
182 3
|
11月前
|
存储 人工智能 自然语言处理
人工智能与未来工作:机遇与挑战
【10月更文挑战第27天】 本文探讨了人工智能(AI)对未来工作的影响,重点分析了AI技术带来的机遇与挑战。通过具体案例和数据,展示了AI如何改变传统行业,创造新的就业机会,同时也提出了应对策略,帮助读者理解并准备迎接这一变革。
|
人工智能 搜索推荐
AI助理小课堂03期
创建组织使用的AI助理 让AI助力企业的每一位员工
|
算法 Python
深度剖析!Python中图的DFS与BFS遍历,让你的数据搜索快到飞起
【7月更文挑战第10天】在数据结构和算法中,图遍历是核心概念,Python支持DFS和BFS来探索图。DFS递归深入节点,利用栈,先访问深处;BFS使用队列,层次遍历,先访问最近节点。
287 1
|
存储 JSON 物联网
设备管理组件功能介绍
设备管理组件功能介绍
473 0
|
前端开发 UED
设计新潮流:CSS动画毛玻璃按钮,展开效果引人入胜!
设计新潮流:CSS动画毛玻璃按钮,展开效果引人入胜!
|
数据处理 Python
Python并发编程:实现高效的多线程与多进程
Python作为一种高级编程语言,提供了强大的并发编程能力,通过多线程和多进程技术,可以实现程序的并发执行,提升系统的性能和响应速度。本文将介绍Python中多线程和多进程的基本概念,以及如何利用它们实现高效的并发编程,解决实际开发中的并发性问题。