I'm using jQuery to shrink a div
's width, like this:
$('div#foo').animate({width: 0},someSpeed);
While it's shrinking, the text reflows to fit the ever-thinner div
. I don't want this to happen - I want the text to stay as it is, just disappearing from view as the width decreases, as happens if you do $('div#foo').slideUp()
.
Is there a CSS trick or some other way to make this work?
Is it possible to manipulate ALL CSS properties with the animate() method? Yes, almost!
The animate() is an inbuilt method in jQuery which is used to change the state of the element with CSS style. This method can also be used to change the CSS property to create the animated effect for the selected element. Syntax: (selector).
jQuery can be several times slower than CSS when talking about animations. CSS animations and transitions have the advantage of being hardware accelerated by the GPU, which is really good in moving pixels.
The animate() method performs a custom animation of a set of CSS properties. This method changes an element from one state to another with CSS styles. The CSS property value is changed gradually, to create an animated effect.
Live demo: http://jsfiddle.net/AvYyT/1/
The trick is to use an inner container and set its with to the width of the outer container.
One option is to use white-space: nowrap;
on the div
. The only problem there is that you will have to manually add line breaks if you have a long body of text.
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