Мне нравится ответ Патрика, но в качестве альтернативы я бы изменил одну и ту же букву по всему тексту.И, возможно, немного повернуть его (хотя это не сработает в IE).Я сделал демонстрационную версию , которую я раздвоил от Патрика.
CSS
.newClass {
left: 0px;
top: -1px;
color: red;
position:relative;
-webkit-transform: rotate(-5deg);
-moz-transform: rotate(-5deg);
}
Код
function randomLetter(cased){
// case = true for uppercase, false for lowercase
var base = (cased) ? 65 : 97;
// convert HTML escape code into a letter
var rand = $('<span>&#' + parseInt(base+(Math.random()*25),10) + ';</span>');
return rand.text();
};
$(document).ready(function(){
var ltr = randomLetter(false);
var reg = new RegExp( ltr, 'g');
$('#typehead').html(function(i,html){
return html.replace(reg, '<span class="newClass">' + ltr + '</span>');
});
});
Обновление: этокод, необходимый для применения к нескольким тегам h1 ( обновленная демоверсия ):
function randomXToY(minVal,maxVal,floatVal){
var randVal = minVal+(Math.random()*(maxVal-minVal));
return typeof floatVal=='undefined'?Math.round(randVal):randVal.toFixed(floatVal);
}
$('.typehead').each(function() {
//access the text and characters within the tag with the id typehead
var exploded = $(this).text().split('');
var rdmltr = randomXToY(0,exploded.length);
// Make sure we don't get a space character or undefined
while(exploded[rdmltr] == ' ' || exploded[rdmltr] == undefined) {
rdmltr = randomXToY(0,exploded.length);
}
// Wrap the letter with a span that has the new class brokenType
// and update it in the array
exploded[rdmltr] = '<span class="brokenType">' + exploded[rdmltr] + '</span>';
// Update the content
$(this).html(exploded.join(''));
});