python打字机效果_打字效果动画的4种实现方法(超简单)
方法一(纯css实现):
html代码:
打字动画打字动画打字动画
css样式:
.typing{
font-size: 1rem;
padding-top: 6%;
margin-bottom: 5%;
font-weight: normal;
letter-spacing: .3rem;
-webkit-animation: type 2s steps(50, end) forwards;
animation: type 2s steps(50, end) forwards;
}
.typing-item{
text-align: center;
color: black;
width:100%;
white-space:nowrap;
overflow:hidden;
}
@-webkit-keyframes type{
from { width: 0;}
}
@keyframes type{
from { width: 0;}
}
缺点:只能实现一行式的打字效果,无法打出一段落文字
方法二(jquery):
html代码:
|
jquery代码:
$(function() {
myPrint(["hello my word!"], 100);
function myPrint(arr, speed) {
var index = 0;
var str = arr[index];
var i = 0;
var add = true;
var mySetInterval = setInterval(function() {
// 延时4个字符的时间
if (i == str.length + 4) {
add = false;
// 如果是最后一个字符串则终止循环函数
if (index + 1 >= arr.length) {
clearInterval(mySetInterval);
}
} else if (i == -2) {
// 删除后延时2个字符的时间
add = true;
str = arr[++index];
}
setTimeout(function() {
if (add) {
i++;
$(".text").html(str.substring(0, i));
} else {
$(".text").html(str.substring(0, i - 1));
i--;
}
})
}, speed)
}
})
方法三(jquery-typed插件):
html代码:
引入Typed.js,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);
// attribute to type into
this.isInput = this.el.is('input');
this.attr = this.options.attr;
// show cursor
this.showCursor = this.isInput ? false : this.options.showCursor;
// text content of element
this.elContent = this.attr ? this.el.attr(this.attr) : this.el.text()
// html or plain text
this.contentType = this.options.contentType;
// 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;
// custom cursor
this.cursorChar = this.options.cursorChar;
// shuffle the strings
this.shuffle = this.options.shuffle;
// the order of strings
this.sequence = [];
// 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() {
for (var i=0;i
// shuffle the array if true
if(self.shuffle) self.sequence = self.shuffleArray(self.sequence);
// Start typing
self.typewrite(self.strings[self.sequence[self.arrayPos]], self.strPos);
}, self.startDelay);
}
,
build: function() {
// Insert cursor
if (this.showCursor === true) {
this.cursor = $("" + this.cursorChar + "");
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);
}
if (self.contentType === 'html') {
// skip over html tags while typing
var curChar = curString.substr(curStrPos).charAt(0)
if (curChar === '
var tag = '';
var endTag = '';
if (curChar === '
endTag = '>'
} else {
endTag = ';'
}
while (curString.substr(curStrPos).charAt(0) !== endTag) {
tag += curString.substr(curStrPos).charAt(0);
curStrPos++;
}
curStrPos++;
tag += endTag;
}
}
// 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.el.html: original text inside element
var nextString = curString.substr(0, curStrPos + 1);
if (self.attr) {
self.el.attr(self.attr, nextString);
} else {
if (self.isInput) {
self.el.val(nextString);
} else if (self.contentType === 'html') {
self.el.html(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;
// }
if (self.contentType === 'html') {
// skip over html tags while backspacing
if (curString.substr(curStrPos).charAt(0) === '>') {
var tag = '';
while (curString.substr(curStrPos).charAt(0) !== '
tag -= curString.substr(curStrPos).charAt(0);
curStrPos--;
}
curStrPos--;
tag += '
}
}
// ----- continue important stuff ----- //
// replace text with base text + typed characters
var nextString = curString.substr(0, curStrPos);
if (self.attr) {
self.el.attr(self.attr, nextString);
} else {
if (self.isInput) {
self.el.val(nextString);
} else if (self.contentType === 'html') {
self.el.html(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;
// Shuffle sequence again
if(self.shuffle) self.sequence = self.shuffleArray(self.sequence);
self.init();
} else
self.typewrite(self.strings[self.sequence[self.arrayPos]], curStrPos);
}
// humanized value for typing
}, humanize);
}
/**
* Shuffles the numbers in the given array.
* @param {Array} array
* @returns {Array}
*/
,shuffleArray: function(array) {
var tmp, current, top = array.length;
if(top) while(--top) {
current = Math.floor(Math.random() * (top + 1));
tmp = array[current];
array[current] = array[top];
array[top] = tmp;
}
return array;
}
// 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('')
this.el.remove();
if (typeof this.cursor !== 'undefined') {
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,
// shuffle the strings
shuffle: false,
// 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,
// either html or text
contentType: 'html',
// 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
var string = $('#content').val();
$('#content-item').typed({
strings: [" " + string],
typeSpeed: 50,
backDelay: 800,
loop: false,
loopCount: false
});
方法四(纯js):
html代码:
js代码:
var str = '内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容';
var i = 0;
function typing(){
var divTyping = document.getElementById('typing');
if (i <= str.length) {
divTyping.innerHTML = str.slice(0, i++) + '_';
setTimeout('typing()', 200);
}
else{
divTyping.innerHTML = str;
}
}
typing();
最后,发现实现打字效果还是蛮简单的对吧,css的思路是利用width和overflow:hidden,动画让宽度变宽实现像打字的样式,但是样式有限,不能实现多行显示。js/jquery,普遍都是先得到字的内容,然后再逐字往容器中添加文字。问题不难,主要是要善于思考!!
以上这篇打字效果动画的4种实现方法(超简单)就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持脚本之家。
python打字机效果_打字效果动画的4种实现方法(超简单)相关推荐
- python 字符串拼接_面试官让用 3 种 python 方法实现字符串拼接 ?对不起我有8种……...
点击上方 蓝字关注我们 点击上方"印象python",选择"星标"公众号重磅干货,第一时间送达!之前发过很多关于 Python 学习的文章,收到大家不少的好评, ...
- 安卓开机logo和开机动画的几种实现方法
安卓4.2可用方法2-4,第一种方法未验证. 从理论上来说,android 有4个开机启动画面. 第一个应该是U-BOOT的启动画面,有些设备为了满足按动电源即有显示,在UBOOT里加了开机画面,实现 ...
- java mysql防重复提交_防止数据重复提交的6种方法(超简单)!
有位朋友,某天突然问磊哥:在 Java 中,防止重复提交最简单的方案是什么? 这句话中包含了两个关键信息,第一:防止重复提交:第二:最简单. 于是磊哥问他,是单机环境还是分布式环境? 得到的反馈是单机 ...
- getclass方法_防止数据重复提交的6种方法(超简单)!
有位朋友,某天突然问磊哥:在 Java 中,防止重复提交最简单的方案是什么? 这句话中包含了两个关键信息,第一:防止重复提交:第二:最简单. 于是磊哥问他,是单机环境还是分布式环境? 得到的反馈是单机 ...
- 使用android开发移动学习平台_移动学习平台有几种开发方法,你造吗?
平板电脑.软件以及宽带互联网的接入等技术都在发生改变,为固定式学习转向移动式学习提供了强大的动力.因此,移动学习越来越火热.那么,移动学习平台有几种开发方法? 基于现有的移动平台和设备,主要有三种开发 ...
- python中保留小数_python保留小数位的三种实现方法
前言 保留小数位是我们经常会碰到的问题,尤其是刷题过程中.那么在python中保留小数位的方法也非常多,但是笔者的原则就是什么简单用什么,因此这里介绍几种比较简单实用的保留小数位的方法: 方法一:fo ...
- android缩放动画的两种实现方法
在android开发.我们会常常使用到缩放动画,普通情况下缩放动画有两种实现方式.一种是直接通过java代码去实现,第二种是通过配置文件实现动画,以下是两种动画的基本是用法: Java代码实现: // ...
- 苹果手机怎么在照片上添加文字_手机照片如何添加文字?原来方法这么简单,花1分钟手把手教...
手机照片如何添加文字?原来方法这么简单,花1分钟手把手教 最近很多人私信笔者,手机拍摄的照片怎样添加好看的文字,今天笔者准备了4种方法,一起来看看吧! 1.自带水印功能(安卓手机) 一般人都是直接拍照 ...
- python做按键精灵脚本_Python 实现按键精灵的功能,超简单详细(MAC版)
前言: 想看Windows版的同窗能够看Python 实现按键精灵的功能,超简单详细(Windows版) 以前写了Windows版的python实现按键精灵功能.如今我鸟枪换炮换了个新的mac.发现以 ...
最新文章
- u一点·料:阿里巴巴1688ued体验设计践行之路. 导读
- gitlab 迁移、升级打怪之路:8.8.5-- 8.10.8 -- 8.17.8 -- 9.5.9 -- 10.1.4 -- 10.2.5
- 栅格单元值的选取方法_计算机求解微分方程的六大数值计算方法
- 简单易用的IT运维服务器管理程序分享!
- 手把手教你如何逐步安装OpenStack
- javascrip部分
- Web前端开发必备工具推荐
- C++实现模板方法模式--问卷调查实战
- EBS API及接口
- html新浪短域名api,新浪短链接API接口示例
- React Native引用三方库报错underfined is not an object(evaluating 'viewproptypes.style')
- Java 选择视频文件对话窗口
- 这三个自媒体平台,你都了解嘛?
- 移动端设置overflow-x:hiden后scrollTop失效并一直为0
- Mtk Camera中Hal1/Hal3的Picture size和Preview size配置
- “城市大脑”治城一年 杭州“变”了
- 零伽壹产业研究:新商业奇观元宇宙,多种技术的集大成者
- 0002数学建模的重要意义
- zookeeper介绍
- “npm”‘ 不是内部或外部命令,也不是可运行的程序 或批处理文件。
热门文章
- 计算机专业的女孩穿搭,大学里女生一般喜欢男生怎样穿搭
- 模拟器+Appium+Python抓取App内容
- ST表 详解(C语言描述)
- 传奇手游服务器搭建_传奇私服服务器端在云服务器架设,全版本通用的传奇游戏架设教程...
- 如何使用计算机查询本机网卡信息,本机mac地址查询的三种方法
- chrome五十大实用插件集合!
- 如何实现移动端点击下拉箭头显示全部文字
- 计算机卡怎么解决,电脑卡怎么办,详细教您电脑卡怎么解决
- HCIA网工数通Datacom之网工初级
- 《罗密欧与朱丽叶》--[英]莎士比亚