简体   繁体   中英

JQuery Toggle animation interfering with CSS height transition

I have a JQuery function to toggle a Div with ( toggle(200) ) so there is a small sliding animation.

However I have another function that will add a class to this div, which change the height of the div with a 0.3s transition.

#navbar {
    height: 270px;
    transition: height 0.3s ease-out;
}

#navbar.change-size {
    height: 430px !important;
}

My problem is that the transition isn't just applying to the height (which was the intended purpose) but it also applies to the first JQuery function toggling when the div slides in.

My question is how can I set up my css so the height transition effect only applies to .change-size height ?

PS: I didn't add my JQuery toggle code as I believe it is not relevant. The JQuery sliding animation is probably tweaking the div's height/width to create the sliding effect, therefore being influenced by the transition I set up.

Please let me know if I'm unclear. Thank you !

Hope this helps

little fiddle

<!DOCTYPE html>
<html>
    <head>
        <style>
        #navbar {
            height: 270px;
            -ms-transition: all .3s ease-out; /*IE*/
            -moz-transition: all .3s ease-out; /*firefox*/
            -webkit-transition: all .3s ease-out;/*safari and chrome*/
            -o-transition: all .3s ease-out; /*opera*/
            background-color: #999;
        }

        #navbar.change-size {
            height: 430px;
        }
        </style>

        <script src="http://code.jquery.com/jquery-2.2.4.min.js"></script>
    </head>

    <body>
        <div id="navbar">
            Hello
        </div>

        <button id="toggle">.change-size</button>
        <script>
        $(function(){
            $("#toggle").on("click", function(){
                $("#navbar").addClass("change-size");
            })
        })
        </script>
    </body>
</html>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM