search

Home  >  Q&A  >  body text

javascript - Is there any good solution for showing and hiding animations?

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)?

阿神阿神2693 days ago1009

reply all(5)I'll reply

  • 漂亮男人

    漂亮男人2017-06-30 10:01:55

    Try using transition:
    demo

    reply
    0
  • 为情所困

    为情所困2017-06-30 10:01:55

    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...

    reply
    0
  • 女神的闺蜜爱上我

    女神的闺蜜爱上我2017-06-30 10:01:55

    <!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>

    reply
    0
  • 習慣沉默

    習慣沉默2017-06-30 10:01:55

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

    reply
    0
  • 高洛峰

    高洛峰2017-06-30 10:01:55

    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>

    reply
    0
  • Cancelreply