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

前端 未结 4 2086
轻奢々
轻奢々 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:27

    I went into this same problem a while ago, my workaround was very hacky but mostly works

    When you change a, lets say, non transitionable property, like 'display' thing go wrong really fast, i figured that if you use a timmer to change the not transitionable property and a few miliseconds later you change another transitionable property, it kind of works, an you also need to use another timer for turning things back

    The HTML

    
    

    The CSS

    .content_menu_item{
      vertical-align: top;
      display:inline-block;
      width: 140px;
      height: 32px;
      position:relative;
      border:1px solid #b388ff;
      text-align: center;
        background-color: #6200ea;
    }
    
    .content_menu_item a{
      line-height: 32px;
      display: inline-block;
      text-decoration: none;
      color:white;
      width: 140px;
    }
    
    ul{
      padding: 0;
      list-style:none;
      display:none;
      margin: 0;
      opacity:0.5;
    }
    .content_menu_item ul li{
      color:white;
      background: #1e88e5;
      line-height: 26px;
      vertical-align: top;
      transition:all 385ms cubic-bezier(0.895, 0.03, 0.685, 0.22);
      opacity:0;
    }
    
    .content_menu_item ul li.on{
      opacity:1;
    }
    
    .content_menu_item ul li.on:nth-child(1){
      transition-delay:0ms;
    }
    .content_menu_item ul li.on:nth-child(2){
      transition-delay:50ms;
    }
    .content_menu_item ul li.on:nth-child(3){
      transition-delay:100ms;
    }
    
    .content_menu_item ul li.off{
      opacity:0;
    }
    
    .content_menu_item ul li.off:nth-child(3){
      transition-delay:0ms;
    }
    .content_menu_item ul li.off:nth-child(2){
      transition-delay:50ms;
    }
    .content_menu_item ul li.off:nth-child(1){
      transition-delay:100ms;
    }
    

    jQuery for handlig the states of the mouse

    $('.content_menu_item').hover(
     function(){
      // mouse over
      $(this).find('ul').show(); // show the sub list of the menu, basicaly display block
      timmeron = setTimeout(()=>{ // 10 miliseconds later add the class to change the opacity, the on class has a transition-delay for every element usin nth-child
        $(this).find('li').addClass('on');
      },10);
     },function(){
        //mouse out
        $(this).find('li').removeClass('on'); // remove the on class 
        $(this).find('li').addClass('off'); // add the off class to invert the transition-delay
            timmeroff = setTimeout(()=>{
              $(this).find('ul').hide(); // hide the element with time after the transition completes 
              $(this).find('li').removeClass('off'); // remove both classes
              $(this).find('li').removeClass('on');
          },500);
    })
    

    And here is a working example https://codepen.io/Teobis/pen/QxmqGQ

    Hope this helps

提交回复
热议问题