Treehouse: Grow your CSS skills. Land your dream job.

Last updated on:

Combine Slide and Fade Functions

$.fn.slideFadeToggle  = function(speed, easing, callback) {
        return this.animate({opacity: 'toggle', height: 'toggle'}, speed, easing, callback);
}; 

Usage

$("#something").click(function() {
   $(this).slideFadeToggle();
});

Comments

  1. Permalink to comment#

    As if you read my thoughts. Thanks Chris :D

  2. This is beautiful! I can imagine using this effect on many things.

    Thanks!

  3. Any demo? I would like to see it if there is some.

  4. Steviss
    Permalink to comment#

    Thanks, m8! Your site is great! Found a lot of usefull tips!

  5. El garch Hicham
    Permalink to comment#

    Hello, is there any Demo to download, it doesn’t work for me.
    thx for the nice post.

  6. it’s nice , but I think it would be better if there was a demo page just like “Downloads”

  7. This is an awesome post Chris cheers.

  8. Excellent effect and very easy to modify!
    Thanks!

  9. zing
    Permalink to comment#

    Always nice to see a demo or example.

  10. JeyKeu
    Permalink to comment#

    Thanks @Chris.

    @Amir Monfared: there you go, http://jsfiddle.net/jeykeu/ejcn8/

  11. Permalink to comment#

    Thanks once again Chris. Over & over again I find what I’m looking for through your site.

  12. Kalrav Bhatnagar
    Permalink to comment#

    Thanks a lot Chris! Nice work

  13. Frederik
    Permalink to comment#

    Nice one, just in case someone else wants to use this:

    You might want to add padding: 'toggle' in order to prevent a jumpy animation when you’re using paddings on the element you want to toggle.

  14. Nithin

    Check out the working demo here

    Thanks,
    Nithin

  15. Here are the corresponding functions for explicitly “slide-fading” in or out.

    $.fn.slideFadeToggle  = function(speed, easing, callback) {
        return this.animate({opacity: 'toggle', height: 'toggle'}, speed, easing, callback);
    };
    
    $.fn.slideFadeIn  = function(speed, easing, callback) {
        return this.animate({opacity: 'show', height: 'show'}, speed, easing, callback);
    };
    
    $.fn.slideFadeOut  = function(speed, easing, callback) {
        return this.animate({opacity: 'hide', height: 'hide'}, speed, easing, callback);
    };
    
    
  16. Hey Guys,
    If I want this to toggle between display: none and display: flex with all the proper compatibility, is that possible? Or should I just create a new element within to display: flex ?

Leave a Comment

Posting Code

We highly encourage you to post problematic HTML/CSS/JavaScript over on CodePen and include the link in your post. It's much easier to see, understand, and help with when you do that.

Markdown is supported, so you can write inline code like `<div>this</div>` or multiline blocks of code in in triple backtick fences like this:

```
<script>
  function example() {
    element.innerHTML = "<div>code</div>";
  }
</script>
```