亚洲香蕉成人av网站在线观看_欧美精品成人91久久久久久久_久久久久久久久久久亚洲_热久久视久久精品18亚洲精品_国产精自产拍久久久久久_亚洲色图国产精品_91精品国产网站_中文字幕欧美日韩精品_国产精品久久久久久亚洲调教_国产精品久久一区_性夜试看影院91社区_97在线观看视频国产_68精品久久久久久欧美_欧美精品在线观看_国产精品一区二区久久精品_欧美老女人bb

首頁 > 編程 > JavaScript > 正文

打字效果動畫的4種實現方法(超簡單)

2019-11-19 15:08:58
字體:
來源:轉載
供稿:網友

方法一(純css實現):

html代碼:

<h1 class="typing typing-item">打字動畫打字動畫打字動畫</h1>

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代碼:

<div class="text-container"> <span class="text-static"></span> <span class="text"></span> <span class="cursor">|</span></div>

jquery代碼:

<script type="text/javascript" src="../js/jquery.js"></script><script>  $(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)   }  }) </script>

方法三(jquery-typed插件):

html代碼:

<div class="content"> <div class="content-div">  <span id="content-item"></span> </div></div><input type="hidden" id="content" value="內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容內容" />

引入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<self.strings.length;++i) self.sequence[i]=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 = $("<span class=/"typed-cursor/">" + this.cursorChar + "</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);    }    if (self.contentType === 'html') {     // skip over html tags while typing     var curChar = curString.substr(curStrPos).charAt(0)     if (curChar === '<' || 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('<span id="' + id + '"/>')   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代碼:

<div id="typing"></div>

js代碼:

<script> 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();</script>

最后,發現實現打字效果還是蠻簡單的對吧,css的思路是利用width和overflow:hidden,動畫讓寬度變寬實現像打字的樣式,但是樣式有限,不能實現多行顯示。js/jquery,普遍都是先得到字的內容,然后再逐字往容器中添加文字。問題不難,主要是要善于思考?。?/p>

以上這篇打字效果動畫的4種實現方法(超簡單)就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支持武林網。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
亚洲香蕉成人av网站在线观看_欧美精品成人91久久久久久久_久久久久久久久久久亚洲_热久久视久久精品18亚洲精品_国产精自产拍久久久久久_亚洲色图国产精品_91精品国产网站_中文字幕欧美日韩精品_国产精品久久久久久亚洲调教_国产精品久久一区_性夜试看影院91社区_97在线观看视频国产_68精品久久久久久欧美_欧美精品在线观看_国产精品一区二区久久精品_欧美老女人bb
91日韩在线播放| 国产日韩在线亚洲字幕中文| 国产一区玩具在线观看| 亚洲精选在线观看| 日韩欧美精品在线观看| 中文字幕亚洲欧美日韩2019| 97婷婷大伊香蕉精品视频| 亚洲国产成人精品女人久久久| 久久男人av资源网站| 亚洲二区中文字幕| www欧美日韩| 国产精品欧美激情在线播放| 亚洲片国产一区一级在线观看| 久久久久久久国产精品视频| 7777kkkk成人观看| 91精品视频一区| 欧美中文字幕精品| 日韩av网址在线| 91免费精品视频| 国产精品成人播放| 国产精品国语对白| 啪一啪鲁一鲁2019在线视频| 另类图片亚洲另类| 亚洲级视频在线观看免费1级| 国产又爽又黄的激情精品视频| 黄网动漫久久久| 欧美精品福利视频| 久久伊人精品天天| 国产欧美一区二区白浆黑人| 成人福利网站在线观看| 国产亚洲精品久久| 久久久久国产精品免费| 国产精品久久久久久久电影| 久久久女人电视剧免费播放下载| 日韩h在线观看| 尤物yw午夜国产精品视频| 亚洲xxxx视频| 欧美精品在线免费播放| 欧美自拍视频在线观看| 91亚洲国产成人精品性色| 97视频在线观看免费| 亚洲欧美一区二区三区在线| 一本色道久久88综合亚洲精品ⅰ| 在线观看久久av| 国产精品欧美一区二区三区奶水| 亚洲日本中文字幕| 欧美精品一区二区三区国产精品| 久久精品视频网站| 日韩av在线网页| 国产不卡一区二区在线播放| 亚洲福利精品在线| 日韩成人黄色av| 狠狠色狠狠色综合日日五| 国产精品欧美激情在线播放| 国产美女扒开尿口久久久| 国产一区二区三区在线播放免费观看| 欧美成年人视频| 成人福利网站在线观看11| 91sao在线观看国产| 久久久久五月天| 亚洲人精选亚洲人成在线| 国产精品视频网址| 亚洲一区二区三区久久| 最新69国产成人精品视频免费| 亚洲精品一区在线观看香蕉| 51ⅴ精品国产91久久久久久| 色老头一区二区三区在线观看| 亚洲xxx自由成熟| 久久中文精品视频| 久久影视免费观看| 中文字幕国产亚洲2019| 亚洲天堂第二页| 亚洲欧洲日产国码av系列天堂| 亚洲精品98久久久久久中文字幕| 日韩在线观看免费| 日韩动漫免费观看电视剧高清| 日韩av在线一区二区| 日韩电影免费观看中文字幕| 久久五月情影视| 2020国产精品视频| 日韩一级裸体免费视频| 国产精品私拍pans大尺度在线| 91在线中文字幕| 日韩av电影在线免费播放| 国内精品视频久久| 欧美高跟鞋交xxxxhd| 九九精品在线观看| 日韩一区二区三区国产| 国产精品高潮视频| 国模gogo一区二区大胆私拍| 亚洲的天堂在线中文字幕| 深夜精品寂寞黄网站在线观看| 亚洲欧美国产制服动漫| 91精品国产亚洲| 日韩免费av一区二区| 日本在线观看天堂男亚洲| 久久精品男人天堂| 国产成人高潮免费观看精品| 亚洲精品电影在线观看| 欧美大片第1页| 欧美成人精品一区二区| 日韩精品一区二区视频| 久久久久久久久亚洲| 精品福利樱桃av导航| 久久久久久亚洲精品中文字幕| 久久久免费观看视频| 性欧美暴力猛交69hd| 日韩精品极品视频| 国产亚洲一区二区在线| 高清亚洲成在人网站天堂| 中文字幕精品影院| 国产欧美日韩精品丝袜高跟鞋| 日韩av有码在线| 亚洲无限乱码一二三四麻| 亚洲欧美福利视频| 色偷偷偷亚洲综合网另类| 久久久久久久久久久久av| 欧美国产乱视频| 欧美日韩国产综合视频在线观看中文| 亚洲第一福利网站| 国产日韩综合一区二区性色av| 色综合久久久888| 欧美性猛交xxxx免费看漫画| 久久躁日日躁aaaaxxxx| 美女视频黄免费的亚洲男人天堂| 亚洲第一视频网| 中文日韩电影网站| 欧美日韩国产第一页| 国产精品久久久久影院日本| 日韩在线中文视频| 欧美激情视频三区| 亚洲精品视频二区| 亚洲精品久久久久久久久久久| 久久精品精品电影网| 亚洲精品自产拍| 亚洲最大成人在线| 亚洲石原莉奈一区二区在线观看| 亚洲国产欧美日韩精品| 国产精品久久久久久久久粉嫩av| 久久久爽爽爽美女图片| 91九色精品视频| 欧美在线一区二区三区四| 欧美成人自拍视频| 欧美洲成人男女午夜视频| 5566成人精品视频免费| 久久久久久久久久久久av| 精品免费在线观看| 91欧美精品午夜性色福利在线| 国产精品久久久久久久9999| 国产一区二区黄| 97国产精品视频| 久久久亚洲精品视频| 懂色aⅴ精品一区二区三区蜜月| 日韩中文字幕在线视频播放| 国产午夜一区二区| 88xx成人精品| 精品中文字幕在线观看| 日韩中文字幕在线视频| 色噜噜狠狠色综合网图区| 热久久免费国产视频| 福利视频导航一区| 色琪琪综合男人的天堂aⅴ视频| 欧美激情在线一区| 国产精品爽黄69天堂a|