ToggleClass animate jQuery?

后端 未结 6 2051
生来不讨喜
生来不讨喜 2020-12-01 18:05

I have a section on my website that when a user clicks I would like it to expand, I\'m using the jQuery\'s toggleClass for this...

expandable: f         


        
6条回答
  •  甜味超标
    2020-12-01 18:17

    I attempted to use the toggleClass method to hide an item on my site (using visibility:hidden as opposed to display:none) with a slight animation, but for some reason the animation would not work (possibly due to an older version of jQuery UI).

    The class was removed and added correctly, but the duration I added did not seem to make any difference - the item was simply added or removed with no effect.

    So to resolve this I used a second class in my toggle method and applied a CSS transition instead:

    The CSS:

    .hidden{
        visibility:hidden;
        opacity: 0;
        -moz-transition: opacity 1s, visibility 1.3s;
        -webkit-transition: opacity 1s, visibility 1.3s;
        -o-transition: opacity 1s, visibility 1.3s;
        transition: opacity 1s, visibility 1.3s;
    }
    .shown{
        visibility:visible;
        opacity: 1;
        -moz-transition: opacity 1s, visibility 1.3s;
        -webkit-transition: opacity 1s, visibility 1.3s;
        -o-transition: opacity 1s, visibility 1.3s;
        transition: opacity 1s, visibility 1.3s;
    }
    

    The JS:

        function showOrHide() {
            $('#element').toggleClass("hidden shown");
        }
    

    Thanks @tomas.satinsky for the awesome (and super simple) answer on this post.

提交回复
热议问题