实现效果,文字逐个输出。

实例代码:

  1. <script>
  2. $(function(){
  3. $("#head-title").typed({
  4. strings: ["为艺术家而生^1000", "为艺术家服务^1000" ,"为艺术家坚持与创新^1000","帝萃 艺术家^1000"],
  5. typeSpeed: 100,
  6. loop: true,
  7. startDelay: 100
  8. });
  9. });
  10. </script>

  

typed.js

  1. // The MIT License (MIT)
  2.  
  3. // Typed.js | Copyright (c) 2014 Matt Boldt | www.mattboldt.com
  4.  
  5. // Permission is hereby granted, free of charge, to any person obtaining a copy
  6. // of this software and associated documentation files (the "Software"), to deal
  7. // in the Software without restriction, including without limitation the rights
  8. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. // copies of the Software, and to permit persons to whom the Software is
  10. // furnished to do so, subject to the following conditions:
  11.  
  12. // The above copyright notice and this permission notice shall be included in
  13. // all copies or substantial portions of the Software.
  14.  
  15. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. // THE SOFTWARE.
  22.  
  23. !function($){
  24.  
  25. "use strict";
  26.  
  27. var Typed = function(el, options){
  28.  
  29. // chosen element to manipulate text
  30. this.el = $(el);
  31.  
  32. // options
  33. this.options = $.extend({}, $.fn.typed.defaults, options);
  34.  
  35. // text content of element
  36. this.baseText = this.el.text() || this.el.attr('placeholder') || '';
  37.  
  38. // typing speed
  39. this.typeSpeed = this.options.typeSpeed;
  40.  
  41. // add a delay before typing starts
  42. this.startDelay = this.options.startDelay;
  43.  
  44. // backspacing speed
  45. this.backSpeed = this.options.backSpeed;
  46.  
  47. // amount of time to wait before backspacing
  48. this.backDelay = this.options.backDelay;
  49.  
  50. // input strings of text
  51. this.strings = this.options.strings;
  52.  
  53. // character number position of current string
  54. this.strPos = 0;
  55.  
  56. // current array position
  57. this.arrayPos = 0;
  58.  
  59. // number to stop backspacing on.
  60. // default 0, can change depending on how many chars
  61. // you want to remove at the time
  62. this.stopNum = 0;
  63.  
  64. // Looping logic
  65. this.loop = this.options.loop;
  66. this.loopCount = this.options.loopCount;
  67. this.curLoop = 0;
  68.  
  69. // for stopping
  70. this.stop = false;
  71.  
  72. // show cursor
  73. this.showCursor = this.isInput ? false : this.options.showCursor;
  74.  
  75. // custom cursor
  76. this.cursorChar = this.options.cursorChar;
  77.  
  78. // attribute to type
  79. this.isInput = this.el.is('input');
  80. this.attr = this.options.attr || (this.isInput ? 'placeholder' : null);
  81.  
  82. // All systems go!
  83. this.build();
  84. };
  85.  
  86. Typed.prototype = {
  87.  
  88. constructor: Typed
  89.  
  90. , init: function(){
  91. // begin the loop w/ first current string (global self.string)
  92. // current string will be passed as an argument each time after this
  93. var self = this;
  94. self.timeout = setTimeout(function() {
  95. // Start typing
  96. self.typewrite(self.strings[self.arrayPos], self.strPos);
  97. }, self.startDelay);
  98. }
  99.  
  100. , build: function(){
  101. // Insert cursor
  102. if (this.showCursor === true){
  103. this.cursor = $("<span class=\"typed-cursor\">" + + "</span>");
  104. this.el.after(this.cursor);
  105. }
  106. this.init();
  107. }
  108.  
  109. // pass current string state to each function, types 1 char per call
  110. , typewrite: function(curString, curStrPos){
  111. // exit when stopped
  112. if(this.stop === true)
  113. return;
  114.  
  115. // varying values for setTimeout during typing
  116. // can't be global since number changes each time loop is executed
  117. var humanize = Math.round(Math.random() * (100 - 30)) + this.typeSpeed;
  118. var self = this;
  119.  
  120. // ------------- optional ------------- //
  121. // backpaces a certain string faster
  122. // ------------------------------------ //
  123. // if (self.arrayPos == 1){
  124. // self.backDelay = 50;
  125. // }
  126. // else{ self.backDelay = 500; }
  127.  
  128. // contain typing function in a timeout humanize'd delay
  129. self.timeout = setTimeout(function() {
  130. // check for an escape character before a pause value
  131. // format: \^\d+ .. eg: ^1000 .. should be able to print the ^ too using ^^
  132. // single ^ are removed from string
  133. var charPause = 0;
  134. var substr = curString.substr(curStrPos);
  135. if (substr.charAt(0) === '^') {
  136. var skip = 1; // skip atleast 1
  137. if(/^\^\d+/.test(substr)) {
  138. substr = /\d+/.exec(substr)[0];
  139. skip += substr.length;
  140. charPause = parseInt(substr);
  141. }
  142.  
  143. // strip out the escape character and pause value so they're not printed
  144. curString = curString.substring(0,curStrPos)+curString.substring(curStrPos+skip);
  145. }
  146.  
  147. // timeout for any pause after a character
  148. self.timeout = setTimeout(function() {
  149. if(curStrPos === curString.length) {
  150. // fires callback function
  151. self.options.onStringTyped(self.arrayPos);
  152.  
  153. // is this the final string
  154. if(self.arrayPos === self.strings.length-1) {
  155. // animation that occurs on the last typed string
  156. self.options.callback();
  157.  
  158. self.curLoop++;
  159.  
  160. // quit if we wont loop back
  161. if(self.loop === false || self.curLoop === self.loopCount)
  162. return;
  163. }
  164.  
  165. self.timeout = setTimeout(function(){
  166. self.backspace(curString, curStrPos);
  167. }, self.backDelay);
  168. } else {
  169.  
  170. /* call before functions if applicable */
  171. if(curStrPos === 0)
  172. self.options.preStringTyped(self.arrayPos);
  173.  
  174. // start typing each new char into existing string
  175. // curString: arg, self.baseText: original text inside element
  176. var nextString = self.baseText + curString.substr(0, curStrPos+1);
  177. if (self.attr) {
  178. self.el.attr(self.attr, nextString);
  179. } else {
  180. self.el.text(nextString);
  181. }
  182.  
  183. // add characters one by one
  184. curStrPos++;
  185. // loop the function
  186. self.typewrite(curString, curStrPos);
  187. }
  188. // end of character pause
  189. }, charPause);
  190.  
  191. // humanized value for typing
  192. }, humanize);
  193.  
  194. }
  195.  
  196. , backspace: function(curString, curStrPos){
  197. // exit when stopped
  198. if (this.stop === true) {
  199. return;
  200. }
  201.  
  202. // varying values for setTimeout during typing
  203. // can't be global since number changes each time loop is executed
  204. var humanize = Math.round(Math.random() * (100 - 30)) + this.backSpeed;
  205. var self = this;
  206.  
  207. self.timeout = setTimeout(function() {
  208.  
  209. // ----- this part is optional ----- //
  210. // check string array position
  211. // on the first string, only delete one word
  212. // the stopNum actually represents the amount of chars to
  213. // keep in the current string. In my case it's 14.
  214. // if (self.arrayPos == 1){
  215. // self.stopNum = 14;
  216. // }
  217. //every other time, delete the whole typed string
  218. // else{
  219. // self.stopNum = 0;
  220. // }
  221.  
  222. // ----- continue important stuff ----- //
  223. // replace text with base text + typed characters
  224. var nextString = self.baseText + curString.substr(0, curStrPos);
  225. if (self.attr) {
  226. self.el.attr(self.attr, nextString);
  227. } else {
  228. self.el.text(nextString);
  229. }
  230.  
  231. // if the number (id of character in current string) is
  232. // less than the stop number, keep going
  233. if (curStrPos > self.stopNum){
  234. // subtract characters one by one
  235. curStrPos--;
  236. // loop the function
  237. self.backspace(curString, curStrPos);
  238. }
  239. // if the stop number has been reached, increase
  240. // array position to next string
  241. else if (curStrPos <= self.stopNum) {
  242. self.arrayPos++;
  243.  
  244. if(self.arrayPos === self.strings.length) {
  245. self.arrayPos = 0;
  246. self.init();
  247. } else
  248. self.typewrite(self.strings[self.arrayPos], curStrPos);
  249. }
  250.  
  251. // humanized value for typing
  252. }, humanize);
  253.  
  254. }
  255.  
  256. // Start & Stop currently not working
  257.  
  258. // , stop: function() {
  259. // var self = this;
  260.  
  261. // self.stop = true;
  262. // clearInterval(self.timeout);
  263. // }
  264.  
  265. // , start: function() {
  266. // var self = this;
  267. // if(self.stop === false)
  268. // return;
  269.  
  270. // this.stop = false;
  271. // this.init();
  272. // }
  273.  
  274. // Reset and rebuild the element
  275. , reset: function(){
  276. var self = this;
  277. clearInterval(self.timeout);
  278. var id = this.el.attr('id');
  279. this.el.after('<span id="' + id + '"/>')
  280. this.el.remove();
  281. this.cursor.remove();
  282. // Send the callback
  283. self.options.resetCallback();
  284. }
  285.  
  286. };
  287.  
  288. $.fn.typed = function (option) {
  289. return this.each(function () {
  290. var $this = $(this)
  291. , data = $this.data('typed')
  292. , options = typeof option == 'object' && option;
  293. if (!data) $this.data('typed', (data = new Typed(this, options)));
  294. if (typeof option == 'string') data[option]();
  295. });
  296. };
  297.  
  298. $.fn.typed.defaults = {
  299. strings: ["These are the default values...", "You know what you should do?", "Use your own!", "Have a great day!"],
  300. // typing speed
  301. typeSpeed: 0,
  302. // time before typing starts
  303. startDelay: 0,
  304. // backspacing speed
  305. backSpeed: 0,
  306. // time before backspacing
  307. backDelay: 500,
  308. // loop
  309. loop: false,
  310. // false = infinite
  311. loopCount: false,
  312. // show cursor
  313. showCursor: true,
  314. // character for cursor
  315. cursorChar: "|",
  316. // attribute to type (null == text)
  317. attr: null,
  318. // call when done callback function
  319. callback: function() {},
  320. // starting callback function before each string
  321. preStringTyped: function() {},
  322. //callback for every typed string
  323. onStringTyped: function() {},
  324. // callback for reset
  325. resetCallback: function() {}
  326. };
  327.  
  328. }(window.jQuery);

typed.js的更多相关文章

  1. 使用typed.js实现页面上的写字功能

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  2. JS框架_(Typed.js)彩色霓虹灯发光文字动画

    百度云盘 传送门 密码:8oei 发光文字动画效果: <!doctype html> <html> <head> <meta charset="ut ...

  3. 第二章 Js语法

    来博客园已经有二年了,看了看自己发布的内容,少的可怜.完全背离了自己的初衷.how time fly,想着自己两年后,还是没写什么东西,岂不是白白浪费时间!有感于王宝强说的那句话,“好好活着,做有意义 ...

  4. JS 常用库汇总收集

    本文不定期更新, 用于汇总记录一些看着 ok 的 JS 库. 库名 简介 项目地址 macy.js 仅 4 kb的 原生 流布局插件 http://macyjs.com/ Driver.js 仅 4 ...

  5. 推荐 11 个好用的 JS 动画库

    为了保证的可读性,本文采用意译而非直译. 1.Three.js 超过46K的星星,这个流行的库提供了非常多的3D显示功能,以一种直观的方式使用 WebGL.这个库提供了<canvas>. ...

  6. CSS 实现打字效果

    JS实现 最近做项目的时候需要实现一个字符逐个出现的打字效果,在网上一搜有个不错的jQuery插件Typed.js,效果很赞 <div class="element"> ...

  7. javascript实现键盘自动打字效果

    最近在网上看到一个字符逐个出现的打字效果,觉得挺有趣的,想一想基本实现思路就是设置一个定时器逐然后逐个向容器中添加字符,于是就基于jQuery写了一个简单版的. <!DOCTYPE html&g ...

  8. jquery plugins

    jQuery官网插件 jQuery自定义滚动条样式插件 jQuery custom content scroller examples Twitter typeahead typeahead.js t ...

  9. 67 个JavaScript和CSS实用工具、库与资源

    在这篇文章中,我不会与大家谈论大型的前端框架,如 React.Angular.Vue 等,也没有涉及那些流行的代码编辑器,如 Atom.VS Code.Sublime,我只想与大家分享一个有助于提升开 ...

随机推荐

  1. Sublime PlantUML环境配置

    参考[http://www.jianshu.com/p/e92a52770832]在安装中遇到不少问题,总结一次成功的步骤如下 一.安装步骤: 1)准备java  环境 jdk1.7 2)安装Subl ...

  2. RDMA卡的检测方法

    1. udaddy This script covers RDMA_CM UD connections. (It establishes a set of unreliable RDMA datagr ...

  3. 分布式搜索elasticsearch几个概念解析

    原文链接:http://blog.csdn.net/july_2/article/details/24367177 介绍下es的几个概念:cluster     代表一个集群,集群中有多个节点,其中有 ...

  4. 数据库操作相关(sql语句-命令行)

    创建数据库: create database books; 创建用户: mysql> grant select,insert,delete,uptate     -> on books.* ...

  5. web.xml之context-param,listener,filter,servlet加载顺序及其周边

    先以加载spring为例子看看加载顺序的作用: Spring加载可以利用ServletContextListener 实现,也可以采用load-on-startup Servlet 实现,但比如fil ...

  6. 关于Python的集合set

    网上那么多说创建集合的语句是: >>>a=set([1,2,3]) python 3.6.3,你们真的能运行吗? 我这里报: Traceback (most recent call ...

  7. php安装redis扩展'checking for igbinary includes... configure: error: Cannot find igbinary.h'解决方法

    今天准备给yii2安装redis扩展,先安装了redis服务,然后安装redis php官方扩展,在make的时候提示' checking for igbinary includes... confi ...

  8. Integral transform 积分变换

    总结: 1.为了更加便于求解,将方程从原域映射到另外一个域 Integral transform - Wikipedia https://en.wikipedia.org/wiki/Integral_ ...

  9. 网站编辑须知9个SEO技巧

    1. 文章当中最好需要出现一个网站核心关键词所谓的网站核心的关键词就是指与网站内容相关性比较高并且是比较受欢迎的关键词,当然还有相当高的转化率 2. 文章标题当中需要出现关键词.关键字在标题标签< ...

  10. flask之SQLAlchemy语法和原生mysql语法

    作为一个程序员,我想把有限的大脑空间留给有价值的知识,本人偏向于原生语法 特随笔于易查阅 # -*- encoding: utf-8 -*- from flask import Flask,rende ...