Pre-formatting text to prevent reflowing
Posted
by mattjn
on Stack Overflow
See other posts from Stack Overflow
or by mattjn
Published on 2010-05-24T02:37:46Z
Indexed on
2010/05/24
2:40 UTC
Read the original article
Hit count: 229
JavaScript
|string-manipulation
I've written a fairly simple script that will take elements (in this case, <p>
elements are the main concern) and type their contents out like a typewriter, one by one.
The problem is that as it types, when it reaches the edge of the container mid-word, it reflows the text and jumps to the next line (like word wrap in any text editor).
This is, of course, expected behavior; however, I would like to pre-format the text so that this does not happen.
I figure that inserting <br>
before the word that will wrap would be the best solution, but I'm not quite sure what the best way to go about doing that is that supports all font sizes and container widths, while also keeping any HTML tags intact.
I figure something involving a hidden <span>
element, adding text to it gradually and checking its width against the container width might be on the right track, but I'm not quite sure how to actually put this together. Any help or suggestions on better methods would be appreciated.
© Stack Overflow or respective owner