typed.js
实现效果,文字逐个输出。
实例代码:
- <script>
- $(function(){
- $("#head-title").typed({
- strings: ["为艺术家而生^1000", "为艺术家服务^1000" ,"为艺术家坚持与创新^1000","帝萃 艺术家^1000"],
- typeSpeed: 100,
- loop: true,
- startDelay: 100
- });
- });
- </script>
typed.js
- // The MIT License (MIT)
- // Typed.js | Copyright (c) 2014 Matt Boldt | www.mattboldt.com
- // Permission is hereby granted, free of charge, to any person obtaining a copy
- // of this software and associated documentation files (the "Software"), to deal
- // in the Software without restriction, including without limitation the rights
- // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
- // copies of the Software, and to permit persons to whom the Software is
- // furnished to do so, subject to the following conditions:
- // The above copyright notice and this permission notice shall be included in
- // all copies or substantial portions of the Software.
- // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
- // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
- // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
- // THE SOFTWARE.
- !function($){
- "use strict";
- var Typed = function(el, options){
- // chosen element to manipulate text
- this.el = $(el);
- // options
- this.options = $.extend({}, $.fn.typed.defaults, options);
- // text content of element
- this.baseText = this.el.text() || this.el.attr('placeholder') || '';
- // typing speed
- this.typeSpeed = this.options.typeSpeed;
- // add a delay before typing starts
- this.startDelay = this.options.startDelay;
- // backspacing speed
- this.backSpeed = this.options.backSpeed;
- // amount of time to wait before backspacing
- this.backDelay = this.options.backDelay;
- // input strings of text
- this.strings = this.options.strings;
- // character number position of current string
- this.strPos = 0;
- // current array position
- this.arrayPos = 0;
- // number to stop backspacing on.
- // default 0, can change depending on how many chars
- // you want to remove at the time
- this.stopNum = 0;
- // Looping logic
- this.loop = this.options.loop;
- this.loopCount = this.options.loopCount;
- this.curLoop = 0;
- // for stopping
- this.stop = false;
- // show cursor
- this.showCursor = this.isInput ? false : this.options.showCursor;
- // custom cursor
- this.cursorChar = this.options.cursorChar;
- // attribute to type
- this.isInput = this.el.is('input');
- this.attr = this.options.attr || (this.isInput ? 'placeholder' : null);
- // All systems go!
- this.build();
- };
- Typed.prototype = {
- constructor: Typed
- , init: function(){
- // begin the loop w/ first current string (global self.string)
- // current string will be passed as an argument each time after this
- var self = this;
- self.timeout = setTimeout(function() {
- // Start typing
- self.typewrite(self.strings[self.arrayPos], self.strPos);
- }, self.startDelay);
- }
- , build: function(){
- // Insert cursor
- if (this.showCursor === true){
- this.cursor = $("<span class=\"typed-cursor\">" + + "</span>");
- this.el.after(this.cursor);
- }
- this.init();
- }
- // pass current string state to each function, types 1 char per call
- , typewrite: function(curString, curStrPos){
- // exit when stopped
- if(this.stop === true)
- return;
- // varying values for setTimeout during typing
- // can't be global since number changes each time loop is executed
- var humanize = Math.round(Math.random() * (100 - 30)) + this.typeSpeed;
- var self = this;
- // ------------- optional ------------- //
- // backpaces a certain string faster
- // ------------------------------------ //
- // if (self.arrayPos == 1){
- // self.backDelay = 50;
- // }
- // else{ self.backDelay = 500; }
- // contain typing function in a timeout humanize'd delay
- self.timeout = setTimeout(function() {
- // check for an escape character before a pause value
- // format: \^\d+ .. eg: ^1000 .. should be able to print the ^ too using ^^
- // single ^ are removed from string
- var charPause = 0;
- var substr = curString.substr(curStrPos);
- if (substr.charAt(0) === '^') {
- var skip = 1; // skip atleast 1
- if(/^\^\d+/.test(substr)) {
- substr = /\d+/.exec(substr)[0];
- skip += substr.length;
- charPause = parseInt(substr);
- }
- // strip out the escape character and pause value so they're not printed
- curString = curString.substring(0,curStrPos)+curString.substring(curStrPos+skip);
- }
- // timeout for any pause after a character
- self.timeout = setTimeout(function() {
- if(curStrPos === curString.length) {
- // fires callback function
- self.options.onStringTyped(self.arrayPos);
- // is this the final string
- if(self.arrayPos === self.strings.length-1) {
- // animation that occurs on the last typed string
- self.options.callback();
- self.curLoop++;
- // quit if we wont loop back
- if(self.loop === false || self.curLoop === self.loopCount)
- return;
- }
- self.timeout = setTimeout(function(){
- self.backspace(curString, curStrPos);
- }, self.backDelay);
- } else {
- /* call before functions if applicable */
- if(curStrPos === 0)
- self.options.preStringTyped(self.arrayPos);
- // start typing each new char into existing string
- // curString: arg, self.baseText: original text inside element
- var nextString = self.baseText + curString.substr(0, curStrPos+1);
- if (self.attr) {
- self.el.attr(self.attr, nextString);
- } else {
- self.el.text(nextString);
- }
- // add characters one by one
- curStrPos++;
- // loop the function
- self.typewrite(curString, curStrPos);
- }
- // end of character pause
- }, charPause);
- // humanized value for typing
- }, humanize);
- }
- , backspace: function(curString, curStrPos){
- // exit when stopped
- if (this.stop === true) {
- return;
- }
- // varying values for setTimeout during typing
- // can't be global since number changes each time loop is executed
- var humanize = Math.round(Math.random() * (100 - 30)) + this.backSpeed;
- var self = this;
- self.timeout = setTimeout(function() {
- // ----- this part is optional ----- //
- // check string array position
- // on the first string, only delete one word
- // the stopNum actually represents the amount of chars to
- // keep in the current string. In my case it's 14.
- // if (self.arrayPos == 1){
- // self.stopNum = 14;
- // }
- //every other time, delete the whole typed string
- // else{
- // self.stopNum = 0;
- // }
- // ----- continue important stuff ----- //
- // replace text with base text + typed characters
- var nextString = self.baseText + curString.substr(0, curStrPos);
- if (self.attr) {
- self.el.attr(self.attr, nextString);
- } else {
- self.el.text(nextString);
- }
- // if the number (id of character in current string) is
- // less than the stop number, keep going
- if (curStrPos > self.stopNum){
- // subtract characters one by one
- curStrPos--;
- // loop the function
- self.backspace(curString, curStrPos);
- }
- // if the stop number has been reached, increase
- // array position to next string
- else if (curStrPos <= self.stopNum) {
- self.arrayPos++;
- if(self.arrayPos === self.strings.length) {
- self.arrayPos = 0;
- self.init();
- } else
- self.typewrite(self.strings[self.arrayPos], curStrPos);
- }
- // humanized value for typing
- }, humanize);
- }
- // Start & Stop currently not working
- // , stop: function() {
- // var self = this;
- // self.stop = true;
- // clearInterval(self.timeout);
- // }
- // , start: function() {
- // var self = this;
- // if(self.stop === false)
- // return;
- // this.stop = false;
- // this.init();
- // }
- // Reset and rebuild the element
- , reset: function(){
- var self = this;
- clearInterval(self.timeout);
- var id = this.el.attr('id');
- this.el.after('<span id="' + id + '"/>')
- this.el.remove();
- this.cursor.remove();
- // Send the callback
- self.options.resetCallback();
- }
- };
- $.fn.typed = function (option) {
- return this.each(function () {
- var $this = $(this)
- , data = $this.data('typed')
- , options = typeof option == 'object' && option;
- if (!data) $this.data('typed', (data = new Typed(this, options)));
- if (typeof option == 'string') data[option]();
- });
- };
- $.fn.typed.defaults = {
- strings: ["These are the default values...", "You know what you should do?", "Use your own!", "Have a great day!"],
- // typing speed
- typeSpeed: 0,
- // time before typing starts
- startDelay: 0,
- // backspacing speed
- backSpeed: 0,
- // time before backspacing
- backDelay: 500,
- // loop
- loop: false,
- // false = infinite
- loopCount: false,
- // show cursor
- showCursor: true,
- // character for cursor
- cursorChar: "|",
- // attribute to type (null == text)
- attr: null,
- // call when done callback function
- callback: function() {},
- // starting callback function before each string
- preStringTyped: function() {},
- //callback for every typed string
- onStringTyped: function() {},
- // callback for reset
- resetCallback: function() {}
- };
- }(window.jQuery);
typed.js的更多相关文章
- 使用typed.js实现页面上的写字功能
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- JS框架_(Typed.js)彩色霓虹灯发光文字动画
百度云盘 传送门 密码:8oei 发光文字动画效果: <!doctype html> <html> <head> <meta charset="ut ...
- 第二章 Js语法
来博客园已经有二年了,看了看自己发布的内容,少的可怜.完全背离了自己的初衷.how time fly,想着自己两年后,还是没写什么东西,岂不是白白浪费时间!有感于王宝强说的那句话,“好好活着,做有意义 ...
- JS 常用库汇总收集
本文不定期更新, 用于汇总记录一些看着 ok 的 JS 库. 库名 简介 项目地址 macy.js 仅 4 kb的 原生 流布局插件 http://macyjs.com/ Driver.js 仅 4 ...
- 推荐 11 个好用的 JS 动画库
为了保证的可读性,本文采用意译而非直译. 1.Three.js 超过46K的星星,这个流行的库提供了非常多的3D显示功能,以一种直观的方式使用 WebGL.这个库提供了<canvas>. ...
- CSS 实现打字效果
JS实现 最近做项目的时候需要实现一个字符逐个出现的打字效果,在网上一搜有个不错的jQuery插件Typed.js,效果很赞 <div class="element"> ...
- javascript实现键盘自动打字效果
最近在网上看到一个字符逐个出现的打字效果,觉得挺有趣的,想一想基本实现思路就是设置一个定时器逐然后逐个向容器中添加字符,于是就基于jQuery写了一个简单版的. <!DOCTYPE html&g ...
- jquery plugins
jQuery官网插件 jQuery自定义滚动条样式插件 jQuery custom content scroller examples Twitter typeahead typeahead.js t ...
- 67 个JavaScript和CSS实用工具、库与资源
在这篇文章中,我不会与大家谈论大型的前端框架,如 React.Angular.Vue 等,也没有涉及那些流行的代码编辑器,如 Atom.VS Code.Sublime,我只想与大家分享一个有助于提升开 ...
随机推荐
- Sublime PlantUML环境配置
参考[http://www.jianshu.com/p/e92a52770832]在安装中遇到不少问题,总结一次成功的步骤如下 一.安装步骤: 1)准备java 环境 jdk1.7 2)安装Subl ...
- RDMA卡的检测方法
1. udaddy This script covers RDMA_CM UD connections. (It establishes a set of unreliable RDMA datagr ...
- 分布式搜索elasticsearch几个概念解析
原文链接:http://blog.csdn.net/july_2/article/details/24367177 介绍下es的几个概念:cluster 代表一个集群,集群中有多个节点,其中有 ...
- 数据库操作相关(sql语句-命令行)
创建数据库: create database books; 创建用户: mysql> grant select,insert,delete,uptate -> on books.* ...
- web.xml之context-param,listener,filter,servlet加载顺序及其周边
先以加载spring为例子看看加载顺序的作用: Spring加载可以利用ServletContextListener 实现,也可以采用load-on-startup Servlet 实现,但比如fil ...
- 关于Python的集合set
网上那么多说创建集合的语句是: >>>a=set([1,2,3]) python 3.6.3,你们真的能运行吗? 我这里报: Traceback (most recent call ...
- php安装redis扩展'checking for igbinary includes... configure: error: Cannot find igbinary.h'解决方法
今天准备给yii2安装redis扩展,先安装了redis服务,然后安装redis php官方扩展,在make的时候提示' checking for igbinary includes... confi ...
- Integral transform 积分变换
总结: 1.为了更加便于求解,将方程从原域映射到另外一个域 Integral transform - Wikipedia https://en.wikipedia.org/wiki/Integral_ ...
- 网站编辑须知9个SEO技巧
1. 文章当中最好需要出现一个网站核心关键词所谓的网站核心的关键词就是指与网站内容相关性比较高并且是比较受欢迎的关键词,当然还有相当高的转化率 2. 文章标题当中需要出现关键词.关键字在标题标签< ...
- flask之SQLAlchemy语法和原生mysql语法
作为一个程序员,我想把有限的大脑空间留给有价值的知识,本人偏向于原生语法 特随笔于易查阅 # -*- encoding: utf-8 -*- from flask import Flask,rende ...