Created
June 25, 2015 15:20
-
-
Save rnagle/24b6a1325f33da56549b to your computer and use it in GitHub Desktop.
Rainbows for your body copy
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
var rainbows = rainbows || {}; | |
(function() { | |
var $ = jQuery; | |
rainbows.last_offset = 0; | |
rainbows.colors = [ | |
'red', 'green', 'yellow', 'blue', 'orange', | |
'purple', 'pink', 'brown', 'black', 'gray', 'white' | |
]; | |
rainbows.wrap = function(target) { | |
var target = $(target), | |
new_target = $('<div>'), | |
nodes = target.contents().clone(); | |
nodes.each(function() { | |
if (this.nodeType == 3) { // text | |
var new_html = ""; | |
text = this.wholeText; | |
for (var i=0; i < text.length; i++) { | |
if (text[i] == ' ') | |
new_html += " "; | |
else | |
new_html += "<span>" + text[i] + "</span>"; | |
} | |
new_target.append(new_html); | |
} else { | |
$(this).html(rainbows.wrap(this)); | |
new_target.append(this); | |
} | |
}); | |
return new_target.html(); | |
}; | |
rainbows.colorize = function(target, offset) { | |
var colors = $.extend([], rainbows.colors); | |
if (typeof offset !== 'undefined') | |
colors = colors.concat(colors.splice(0, offset)); | |
$(target).find('span').each(function(idx, el) { | |
$(el).attr('class', colors[(idx % colors.length)]); | |
}); | |
}; | |
rainbows.start = function(target, interval) { | |
$(target).html(rainbows.wrap(target)); | |
rainbows.intervalId = setInterval(function() { | |
rainbows.colorize(target, rainbows.last_offset); | |
rainbows.last_offset = (rainbows.last_offset + 1) % rainbows.colors.length; | |
}, interval || 500); | |
}; | |
}()); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment