JQuery animate() on width by setting element content
Is there a way to animate the change in width upon setting a text() of an element?
开发者_运维问答Say, we have
<a id="link" href="#">container/sub1/sub2</a>
that is then changed programmatically by, say, $('#link').text('sub2');
into this:
<a id="link" href="#">sub2</a>
Normally, said element's width will change instantly, but the effect I'm looking for is its gradual animated reduction.
Thank you.
You could do something like this:
http://jsfiddle.net/HYZbA/
I have basically wrapped the <a>
in a div with overflow:hidden
. Then when the text is changed, the wrapper is animated to the size of the a
.
NOTE: The red border is just so you can see what's happening. Remove this to see it in action.
I'm not sure if to you can actually have the text animate into another text however I've done something to this effect by first using fadeOut()
then setting the text and then using fadeIn()
to have the text appear again. This will give an appearance of gradual change in text.
I was looking for my own solution, and based on James Montagne's answer, I came up with http://jsfiddle.net/HYZbA/33/ combining fadeIn
/ fadeOut
as suggested by Albinoswordfish.
精彩评论