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博客如需转载请紧急联系作者

相关文章
|
9月前
|
移动开发 前端开发 HTML5
彩色大风车泡泡Canvas特效
彩色大风车泡泡Canvas特效
68 7
彩色大风车泡泡Canvas特效
|
9月前
|
移动开发 JavaScript 前端开发
分享86个鼠标特效,总有一款适合您
分享86个鼠标特效,总有一款适合您
108 1
分享86个鼠标特效,总有一款适合您
|
3月前
纯css3发光霓虹灯文字闪烁特效代码
纯css3发光霓虹灯文字闪烁特效代码是一款彩色的店铺名称文字动画特效。
46 6
|
5月前
宇宙星星转动特效带背景音乐引导页源码
宇宙星星转动特效带背景音乐引导页源码,源码由HTML+CSS+JS组成,记事本打开源码文件可以进行内容文字之类的修改,双击html文件可以本地运行效果,也可以上传到服务器里面,重定向这个界面
69 7
宇宙星星转动特效带背景音乐引导页源码
|
6月前
|
前端开发
CSS动画霓虹灯闪烁效果
CSS动画霓虹灯闪烁效果
|
6月前
|
前端开发 JavaScript
HTML+CSS实现超酷炫的返回顶部特效,你一定会爱上!
HTML+CSS实现超酷炫的返回顶部特效,你一定会爱上!
|
6月前
PPT 动画-文字渐入
PPT 动画-文字渐入
27 0
|
7月前
|
前端开发
css特效——Photoshop选区(动感的虚线选框,支持不规则的选框)
css特效——Photoshop选区(动感的虚线选框,支持不规则的选框)
43 1
|
前端开发 容器
暗夜发光,独自闪耀,盘点网页暗黑模式(DarkMode)下的特效和动效,CSS3实现
众所周知,网页的暗黑模式可以减少屏幕反射和蓝光辐射,减少眼睛的疲劳感,特别是在夜间使用时更为明显。其实暗黑模式也给霓虹灯效应(Neon Effect)提供了发挥的环境。 霓虹灯效应是一种视觉效果,其特点是在深色背景上使用鲜艳的颜色来产生强烈的视觉冲击。这种效应通常用于设计海报、广告、标志和网页等。霓虹灯效应的作用在于吸引人们的注意力和增强品牌形象的辨识度,因为这种效果让人印象深刻且易于记忆,本次我们盘点适合暗色模式的网页特效,还是喜欢,暗夜的你。
暗夜发光,独自闪耀,盘点网页暗黑模式(DarkMode)下的特效和动效,CSS3实现
|
前端开发 开发工具
教你用HTML+CSS实现百叶窗动画效果
我们浏览网页的时候总能看见一些炫酷的特效,比如百叶窗效果,本文我们就用HTML+CSS制作一个百叶窗小项目,适合刚学前端的小伙伴,使用代码简单易懂,很容易上手,学习完本文后,相信你也能敲出来属于自己的百叶窗效果,改变图片及相应盒子的大小,有女朋友的可以试着放几张在一起的照片,也是一件非常浪漫的事
601 0
教你用HTML+CSS实现百叶窗动画效果