Once I've told You how You could improve headers on Your website, using Google Font Effects. This was quite a neat idea, especially given that: 1) You could combine multiple effects and apply them to a single header - and 2) You could apply those effects to other elements as well (not only headers! :) ).
But now it's time for the next step ;) - another creative idea You could use, making headers even more attractive and unpredictable (!). I didn't invent the name for this concept yet - maybe You could call it "Candy Letters" or "Watercolor Letters"... let Me explain the details.
The very essence of it is simple: to have every single letter (more precisely: character) randomly colored. To tailor the script for this purpose was quite of an interesting experience because... in order to color each and every single letter You need to target it first - but there is a trick here: it seems not possible to fish them out of words which they are making up, because - as such - they are not considered standalone HTML elements. So in order to target single letters independently You may need a little trick: to wrap every single character with some well-known HTML tag (in fact I wouldn't be surprised if any fictional tag would work here - e.g. <x></x>).
If You have all characters already wrapped - next thing You need is to attribute a random CSS class to them. That's the theory. Let's have a look on practice:
.s1 { color: white; }
.s2 { color: #fde4cc; }
.s3 { color: #dad7fd; }
.s4 { color: #e9fef0; }
.s5 { color: #fed8f4; }
$(document).ready(function(){
...here You paste the actual code...
});
var classes = ["s1", "s2", "s3", "s4", "s5"];
$('.post-body h3').html(function (i, html) {
return html.replace(/(.)/g, '<b>$1</b>');
});
$(".post-body h3 b").each(function(){
$(this).addClass(classes[~~(Math.random()*classes.length)]);
});
That's all. The whole code would then look like:
/* Fancy Headers - start */
$(document).ready(function(){
var classes = ["s1", "s2", "s3", "s4", "s5"];
$('.post-body h3').html(function (i, html) {
return html.replace(/(.)/g, '<b>$1</b>');
});
$(".post-body h3 b").each(function(){
$(this).addClass(classes[~~(Math.random()*classes.length)]);
});
});
/* Fancy Headers - end */
In step 4 I use a regular expression which targets every single character: /(.)/g.
As a result You should see a header of which various letters have various colors. Moreover, that combination is always fresh - because it's rendered from the beginning and in a random way every time You load or refresh the website.
No comments:
Post a Comment