Created
March 2, 2017 15:16
-
-
Save danielme85/a1df06d155ec6f9d5b19d162bcff9d10 to your computer and use it in GitHub Desktop.
Simple scramble char effect jQuery plugin
This file contains hidden or 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
(function($) { | |
$.fn.danScramble = function(options) { | |
var $this = $(this); | |
var opts = $.extend({}, $.fn.danScramble.defaults, options); | |
var letters = opts.letters.split(''); | |
var maxTries = opts.maxTotalTries; | |
var randomMax = opts.randomMax; | |
var length = letters.length; | |
var string = $this.text(); | |
var array = string.split(''); | |
var origArray = string.split(''); | |
var arrayMaxTries = []; | |
var arrayTries = []; | |
var tries = 0; | |
array.forEach(function(char, i) { | |
arrayTries[i] = 0; | |
arrayMaxTries[i] = Math.floor(Math.random() * (randomMax + 1)); | |
}); | |
var interval = setInterval(function(){ | |
var alldone = true; | |
array.forEach(function(char, i) { | |
if (arrayTries[i] < arrayMaxTries[i]) { | |
var random = Math.floor(Math.random() * (length + 1)); | |
array[i] = letters[random]; | |
arrayTries[i] = tries; | |
alldone = false; | |
} | |
else { | |
array[i] = origArray[i]; | |
} | |
}); | |
$this.text(array.join('')); | |
if (tries > maxTries || alldone) { | |
clearInterval(interval); | |
$this.text(string); | |
} | |
tries++; | |
},100); | |
return this; | |
}; | |
$.fn.danScramble.defaults = { | |
maxTotalTries: 50, | |
randomMax: 20, | |
letters: 'QWERTYUIOPASDFGHJKLZXCVNM1234567890!@#$%&*' | |
}; | |
}(jQuery)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment