I'm trying to use Jquery/Javascript to mimic a broken typewriter font (since I could not find one). But I want to make it random which letter gets broken. I was able to split the string of the id that I wanted and use a bit of code I found to get a random number between 0 and the total length of the string. What I'm having problem with now is doing something with that specific character. I want to push it down or up a few pixels. I was trying to give it a class so I could add some margin or padding, but it doesn't work. So I'm stuck where I am now.
here's the page, I'm trying to do it to the word "ABOUT":
http://www.franciscog.com/bs/about.php
here's the script:
<script type="text/javascript">
function randomXToY(minVal,maxVal,floatVal)
{
var randVal = minVal+(Math.random()*(maxVal-minVal));
return typeof floatVal=='undefined'?Math.round(randVal):randVal.toFixed(floatVal);
}
var str = $('#typehead').text();
var strcnt = str.length;
var exploded = str.split('');
var rdmltr =randomXToY(0,strcnt);
var theLetter = exploded[rdmltr];
theLetter.addClass("newClass");
var toffset = $('.newClass').offset();
alert(toffset.left + "," + toffset.top);
</script>
How to find if a word or a substring is present in the given string. In this case, we will use the includes() method which determines whether a string contains the specified word or a substring. If the word or substring is present in the given string, the includes() method returns true; otherwise, it returns false.
jQuery offset() Method The offset() method set or returns the offset coordinates for the selected elements, relative to the document. When used to return the offset: This method returns the offset coordinates of the FIRST matched element. It returns an object with 2 properties; the top and left positions in pixels.
$ sign is just a valid javascript identifier which is used as an alias for jQuery. Prototype, jQuery, and most javascript libraries use the $ as the primary base object (or function). Most of them also have a way to relinquish the $ so that it can be used with another library that uses it.
JavaScript String indexOf() The indexOf() method returns the position of the first occurrence of a value in a string. The indexOf() method returns -1 if the value is not found.
EDIT: Updated to ensure that the matched character is not a space character, and added a little style suggested by @abelito.
How about this: http://jsfiddle.net/cgXa3/4/
function randomXToY(minVal,maxVal,floatVal){
var randVal = minVal+(Math.random()*(maxVal-minVal));
return typeof floatVal=='undefined'?Math.round(randVal):randVal.toFixed(floatVal);
}
var exploded = $('#typehead').text().split('');
var rdmltr = randomXToY(0,exploded.length);
// Make sure we don't get a space character
while(exploded[rdmltr] == ' ') {
rdmltr = randomXToY(0,exploded.length);
}
// Wrap the letter with a span that has the newClass
// and update it in the array
exploded[rdmltr] = '<span class="newClass">' + exploded[rdmltr] + '</span>';
// Update the content
$('#typehead').html(exploded.join(''));
var toffset = $('.newClass').offset();
alert(toffset.left + "," + toffset.top);
Update: If you want to apply it to several: http://jsfiddle.net/cgXa3/5/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With