I want to try create an effect where by when I trigger an event an animated line strikes through a piece of text. The effect should be done in Java Script.
Can someone suggest some ways to do this? I have text already on a page and I would like the text to strike through from left to right as if a line is being drawn
jsFiddle.
\n
) using regex and recursively apply function with callback to wrap each individual matched character with aspan
.span
elements and then usesetInterval()
to traverse them, addingstyle="text-decoration: line-through
via thestyle
object of thespan
.span
.The disadvantage of using
innerHTML
is when you serialize the HTML, you lose all events, etc. In the fiddle above, thestrong
element is still clickable (you will click thespan
, which will bubble up to the parent).You could use jQuery to animate a background image that looks like a strikethrough. Perhaps something like this:
This might look a little smoother than trying to do something involving
<s>
tags. Your HTML markup would be like this:And you'd need to make sure that
.someclass
had default CSS that hid the background image by using background-position, ie:Just came here from google, ended up writing my own simple little function. This is how I did it:
You could add an
<s>
tag to the beginning of the string and iteratively move the closing</s>
tag one character further towards the end of the string, probably best usingsetTimeout()
.Something along these lines (not tested):
Here's a basic implementation that works in current versions of IE, Firefox and Chrome:
Using jQuery it's possible with little tweak: http://jsfiddle.net/yahavbr/EbNh7/
JS in use:
This will strike through
myDiv
text, making the line appear with animation.As it's not using any heavy jQuery stuff it can be converted to plain JavaScript pretty easily so if you prefer not to use jQuery I'll edit my answer.