How to transition child when parent goes from display: none to block

前端 未结 4 2093
轻奢々
轻奢々 2021-02-13 15:55

I\'m having trouble creating a flyout menu with a specific effect. The flyout goes from display:none to block and then I use jquery to animate opacity from 0 to 1 (and vice vers

4条回答
  •  庸人自扰
    2021-02-13 16:17

    if you want only to change the opacity you can use FadeIn and FadeOut functions of JQuery but if you want more complex transition you can use CSS3 (this is a realy good library). See this DEMO where you can see this two different way.

    You can also add a controll to the class like this:

        $("OBJECT").click(function(){
        if ($("OBJECT").hasClass("CLASS")){
            $("OBJECT").removeClass("CLASS");
        } else {
            $("OBJECT").addClass("CLASS");
        }
    });
    

    to make two way functions.

    $(document).ready(function(){
    $("#fadeOut").click(function(){
        var duration = 500;
        $("#div").fadeOut(duration);
    });
    $("#css").click(function(){
        $("#div").addClass("out");
        setTimeout(
           function() {
              $("#div").css("display", "none");
           },
        2001);
    });
    });
    #div {
        width:200px;
        height:200px;
        background-color:red;
        text-align:center;
        vertical-align:middle;
        /* Animation CSS */
        -webkit-animation-duration: 2s;
        animation-duration: 2s;
        -webkit-animation-fill-mode: both;
        animation-fill-mode: both;
    }
    /* Setup CSS3 animations */
    @-webkit-keyframes out {
      0% {
        -webkit-transform-origin: top left;
        transform-origin: top left;
        -webkit-animation-timing-function: ease-in-out;
        animation-timing-function: ease-in-out;
      }
    
      20%, 60% {
        -webkit-transform: rotate3d(0, 0, 1, 80deg);
        transform: rotate3d(0, 0, 1, 80deg);
        -webkit-transform-origin: top left;
        transform-origin: top left;
        -webkit-animation-timing-function: ease-in-out;
        animation-timing-function: ease-in-out;
      }
    
      40%, 80% {
        -webkit-transform: rotate3d(0, 0, 1, 60deg);
        transform: rotate3d(0, 0, 1, 60deg);
        -webkit-transform-origin: top left;
        transform-origin: top left;
        -webkit-animation-timing-function: ease-in-out;
        animation-timing-function: ease-in-out;
        opacity: 1;
      }
    
      to {
        -webkit-transform: translate3d(0, 700px, 0);
        transform: translate3d(0, 700px, 0);
        opacity: 0;
      }
    }
    
    @keyframes out {
      0% {
        -webkit-transform-origin: top left;
        transform-origin: top left;
        -webkit-animation-timing-function: ease-in-out;
        animation-timing-function: ease-in-out;
      }
    
      20%, 60% {
        -webkit-transform: rotate3d(0, 0, 1, 80deg);
        transform: rotate3d(0, 0, 1, 80deg);
        -webkit-transform-origin: top left;
        transform-origin: top left;
        -webkit-animation-timing-function: ease-in-out;
        animation-timing-function: ease-in-out;
      }
    
      40%, 80% {
        -webkit-transform: rotate3d(0, 0, 1, 60deg);
        transform: rotate3d(0, 0, 1, 60deg);
        -webkit-transform-origin: top left;
        transform-origin: top left;
        -webkit-animation-timing-function: ease-in-out;
        animation-timing-function: ease-in-out;
        opacity: 1;
      }
    
      to {
        -webkit-transform: translate3d(0, 700px, 0);
        transform: translate3d(0, 700px, 0);
        opacity: 0;
      }
    }
    .out {
        -webkit-animation-name: out;
        animation-name: out;
    }
    
    
        
        
        
    
    
    

提交回复
热议问题