Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery: Is there a way to animate a div's width without any text movement?

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?

like image 877
Nathan Long Avatar asked Feb 03 '11 13:02

Nathan Long


People also ask

Is it possible to manipulate all CSS properties with the jQuery animate method?

Is it possible to manipulate ALL CSS properties with the animate() method? Yes, almost!

Can the jQuery animate () method be used to animate any CSS property?

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).

Is jQuery slower for animations?

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.

Which method is available in jQuery for animation?

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.


2 Answers

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.

like image 170
Šime Vidas Avatar answered Oct 19 '22 23:10

Šime Vidas


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.

like image 28
Michael Mior Avatar answered Oct 20 '22 01:10

Michael Mior