Last active
June 15, 2018 14:36
-
-
Save danielme85/556abd14851694d08acc3c15b665c346 to your computer and use it in GitHub Desktop.
Typewriter effect, made for js/jQuery practice... Try it out at https://codepen.io/anon/pen/rKGwGK
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Created by mellu on 3/3/2017. | |
*/ | |
(function($) { | |
$.fn.danTyper = function(strings, options) { | |
var $this = $(this); | |
var opts = $.extend({}, $.fn.danTyper.defaults, options); | |
var speed = opts.speed; | |
var enableCursor = opts.cursor; | |
var lines = strings.length; | |
var lineDelay = opts.delay; | |
var runforever = opts.loop; | |
var cursor = '<span class="cursor-blinky">|</span>'; | |
var i = 0; | |
var loopStrings = function(strings) { | |
var string = strings[i]; | |
addLine(string, function() { | |
if (i < lines || runforever) { | |
if (i >= lines && runforever) { | |
i = 0; | |
} | |
setTimeout(function () { | |
removeLine($this.text(), function () { | |
loopStrings(strings); | |
}); | |
}, lineDelay); | |
} | |
}); | |
i++; | |
}; | |
function addLine(string, callback) { | |
var stringout = ''; | |
var chars = string.split(''); | |
var reverseto = 0; | |
$this.stop(true, true); | |
chars.forEach(function (char, i) { | |
var delay = speed + Math.floor(Math.random() * (speed + 1)); | |
$this.delay(delay).queue(function (addChar) { | |
if (char === '<') { | |
var leftovers = string.substr(i+1); | |
var startstring = removeCharsTypo(stringout, reverseto, function(){ | |
var newstring = startstring+leftovers; | |
addLine(newstring, function(){}); | |
}); | |
} | |
else { | |
if (char === '$') { | |
reverseto = 0; | |
} | |
else if (char !== '$' && char !== '<'){ | |
reverseto++; | |
stringout += char; | |
} | |
$(this).text(stringout); | |
if (enableCursor) { | |
$(this).append(cursor); | |
} | |
addChar(); | |
} | |
}); | |
}); | |
$this.promise().done(function () { | |
callback(); | |
}); | |
} | |
function removeCharsTypo(string, steps, callback) { | |
$this.stop(true, true); | |
var chars = string.split(''); | |
var counter = 0; | |
while (counter < steps) { | |
var delay = speed + Math.floor(Math.random() * (speed + 1)); | |
$this.delay(delay).queue(function (removeChar) { | |
chars.pop(); | |
var stringout = chars.join(''); | |
$(this).text(stringout); | |
if (enableCursor) { | |
$(this).append(cursor); | |
} | |
removeChar(); | |
}); | |
counter++; | |
} | |
$this.promise().done(function () { | |
callback(); | |
}); | |
return string.substring(0, string.length-steps); | |
} | |
function removeLine(string, callback) { | |
var chars = string.split(''); | |
$this.stop(true, true); | |
chars.forEach(function (char, i) { | |
var delay = speed + Math.floor(Math.random() * (speed + 1)); | |
$this.delay(delay).queue(function (removeChar) { | |
chars.pop(); | |
var stringout = chars.join(''); | |
$(this).text(stringout); | |
if (enableCursor) { | |
$(this).append(cursor); | |
} | |
removeChar(); | |
}); | |
}); | |
$this.promise().done(function () { | |
callback(); | |
}); | |
} | |
if (enableCursor) { | |
setInterval(function(){ | |
$('.cursor-blinky').fadeOut().fadeIn(); | |
},speed); | |
} | |
loopStrings(strings); | |
return this; | |
}; | |
$.fn.danTyper.defaults = { | |
speed: 50, | |
delay: 2000, | |
cursor: true, | |
loop: false | |
}; | |
}(jQuery)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment