-
Notifications
You must be signed in to change notification settings - Fork 24
/
jquery.teletype.min.js
1 lines (1 loc) · 2.35 KB
/
jquery.teletype.min.js
1
!function(e){e.fn.teletype=function(t){var i=e.extend({},e.fn.teletype.defaults,t),n=this,o=e(this),s=null,l={string:"",index:0,position:0,loop:0},a=function(){return l.index++,l.index>=i.text.length&&(l.index=0,l.loop++,i.loop!==!1&&i.loop==l.loop)?!1:(l.position=0,c(),"function"==typeof i.callbackNext&&i.callbackNext(l,n),!0)},r=function(){i.prefix&&0===l.position&&0===l.loop&&0===l.index&&e("<span />").addClass("teletype-prefix").html(i.prefix).prependTo(o);var t=l.string.split(""),c=t[l.position],d=l.position+1;if("^"==c||"~"==c){var f=l.string.substr(d).search(/[^0-9]/);-1==f&&(f=l.string.length);var y=l.string.substr(d,f);if(e.isNumeric(y)){if(l.string=l.string.replace(c+y,""),"^"==c)window.setTimeout(function(){window.setTimeout(r,u(i.typeDelay))},y);else{var h=l.position-y;l.string=l.string.substr(0,h-1)+l.string.substr(l.position-1),window.setTimeout(function(){p(Math.max(h,0))},u(i.backDelay))}return}}else if("\\"==c){var m=l.string.substr(d,1);"n"==m&&(l.position++,c="<br />")}void 0!==c&&s.html(s.html()+c),l.position++,l.position<l.string.length?window.setTimeout(r,u(i.typeDelay)):i.preserve===!1?window.setTimeout(function(){window.setTimeout(p,u(i.backDelay))},i.delay):(s.html(s.html()+'<span class="teletype-prefix">'+i.prefix+"</span>"),a()?window.setTimeout(function(){window.setTimeout(r,u(i.typeDelay))},i.delay):"function"==typeof i.callbackFinished&&i.callbackFinished(n)),"function"==typeof i.callbackType&&i.callbackType(c,l,n)},p=function(e){if(e||(e=0),l.position>e)s.html(s.html().slice(0,-1)),window.setTimeout(function(){p(e)},u(i.backDelay)),l.position--;else{if(0===e&&a()===!1)return;window.setTimeout(r,u(i.typeDelay))}},u=function(e){var t=parseInt(e);return i.humanise&&(t+=Math.floor(200*Math.random())),t},c=function(){l.string=i.text[l.index].replace(/\n/g,"\\n")};return this.setCursor=function(t){e(".teletype-cursor",o).text(t)},this.each(function(){if(c(),o.addClass("teletype").empty(),s=e("<span />").addClass("teletype-text").appendTo(o),i.cursor){var t=e("<span />").addClass("teletype-cursor").appendTo(o);n.setCursor(i.cursor),setInterval(function(){t.animate({opacity:0}).animate({opacity:1})},i.blinkSpeed)}r()})},e.fn.teletype.defaults={text:["one","two","three"],typeDelay:100,backDelay:50,blinkSpeed:1e3,delay:2e3,cursor:"|",preserve:!1,prefix:"",loop:0,humanise:!0,callbackNext:null,callbackType:null,callbackFinished:null}}(jQuery);