My application is performing poorly with jQuery's slideDown and slideUp. I'm looking to use a CSS3 equivalent in browsers which support it.
Is it possible, using CSS3 transitions, to change an element from display: none;
to display: block;
while sliding the item down or up?
Aight fam, after some research and experimenting, I think the best approach is to have the thing's height at
0px
, and let it transition to an exact height. You get the exact height with JavaScript. The JavaScript isn't doing the animating, it's just changing the height value. Check it:Whenever the page loads or is resized, the element with class
info
will get itsh
attribute updated. Then you could have a button trigger thestyle="height: __"
to set it to that previously seth
value.Here's the styling for the
info
class.I used this on one of my projects so class names are specific. You can change them up however you like.
The styling might not be supported cross-browser. Works fine in chrome.
Below is the live example for this code. Just click on the
?
icon to start the animationCodePen
you can't make easisly a slideup slidedown with css3 tha's why I've turned JensT script into a plugin with javascript fallback and callback.
in this way if you have a modern brwowser you can use the css3 csstransition. if your browser does not support it gracefuly use the old fashioned slideUp slideDown.
the plugin
how to use it
you can find a demo page here http://www.mosne.it/playground/mosne_slide_up_down/
I would recommend using the jQuery Transit Plugin which uses the CSS3 transform property, which works great on mobile devices due to the fact that most support hardware acceleration to give that native look and feel.
JS Fiddle Example
HTML:
Javascript:
why not to take advantage of modern browsers css transition and make things simpler and fast using more css and less jquery
Here is the code for sliding up and down
Here is the code for sliding left to right
Similarly we can change the sliding from top to bottom or right to left by changing transform-origin and transform: scaleX(0) or transform: scaleY(0) appropriately.
So I've gone ahead and answered my own question :)
@True's answer regarded transforming an element to a specific height. The problem with this is I don't know the height of the element (it can fluctuate).
I found other solutions around which used max-height as the transition but this produced a very jerky animation for me.
Here it is (works only in WebKit browsers): http://jsfiddle.net/XUjAH/6/
Although not purely CSS, my solution involves transitioning the height, which is determined by some JS.
Getting height transitions to work can be a bit tricky mainly because you have to know the height to animate for. This is further complicated by padding in the element to be animated.
Here is what I came up with:
use a style like this:
Wrap your content into another container so that the container you're sliding has no padding/margins/borders:
Then use some script (or declarative markup in binding frameworks) to trigger the CSS classes.
Example here: http://plnkr.co/edit/uhChl94nLhrWCYVhRBUF?p=preview
This works fine for fixed size content. For a more generic soltution you can use code to figure out the size of the element when the transition is activated. The following is a jQuery plug-in that does just that:
which can be triggered like this:
$("#Trigger").click(function () {
against markup like this:
Example: http://plnkr.co/edit/Wpcgjs3FS4ryrhQUAOcU?p=preview
I wrote this up recently in a blog post if you're interested in more detail:
http://weblog.west-wind.com/posts/2014/Feb/22/Using-CSS-Transitions-to-SlideUp-and-SlideDown