javascript - Is there any good solution for showing and hiding animations?
阿神
阿神 2017-06-30 09:59:55
0
5
1056

example

It has the same effect as this animation. p is hidden first. After clicking the button, it is displayed and has animation effect. It is the same when it is hidden. But I definitely wrote it too complicatedly. Is there a simpler solution (without third-party libraries)?

阿神
阿神

闭关修行中......

reply all(5)
漂亮男人

Try using transition:
demo

为情所困

The simpler idea is:

  1. The block does not need to be hidden, just set the height to 0 and it will be invisible

  2. Use transition to achieve animation effects

  3. There is no need to use the two class names hidden and show to control it. In fact, it only has two states, so it can be considered that without show it is hidden

  4. In addition, there is no need to write a show() and hide() to bind them separately. In fact, you can click this button to expand it, click it again to hide it, and use a toggle() to switch the display state

I made some modifications to your code, as follows:
https://jsfiddle.net/boxsnake...

女神的闺蜜爱上我
<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <title></title>
    </head>
    <style type="text/css">
        .box{
            background: red;
            height: 200px;
            width: 200px;
            transition: height 0.8s;
        }
    </style>
    <body>
        <button onclick="changeHeight()">click me</button>
        <p class="box" style="height: 0;"></p>
    </body>
    <script src="https://cdn.bootcss.com/jquery/2.2.3/jquery.min.js" type="text/javascript" charset="utf-8"></script>
    <script type="text/javascript">
        function changeHeight(){
            var box=$('.box')
            if($('.box').height()!=0){
                $('.box').height(0)
            }else{
                $('.box').height(200)
            }
            
        }
    </script>
</html>
習慣沉默

The question can be solved with CSS3 (if it does not need to be compatible with IE)

小葫芦

Can it be implemented with jquery?

//头部引入jquery,toggle()
<body>
    <p>bugbugbug</p>
    <button>Toggle</button>
    <script type="text/javascript">
    $(document).ready(function() {
        $("button").click(function() {
            $("p").toggle(1000);
        });
    });
    </script>
</body>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template