JavaScript - add transition between display:none and display:block

后端 未结 7 1412
别跟我提以往
别跟我提以往 2020-11-29 08:27

I am using JavaScript to toggle notification like below. How can I add transition between display: block and display: none;

I don\'

7条回答
  •  伪装坚强ぢ
    2020-11-29 08:57

    see my example below:

    var btn = document.querySelector('button');
    var hint = document.getElementById('hint');
    var height = hint.clientHeight;
    var width = hint.clientWidth;
    console.log(width + 'x' + height);
    // initialize them (within hint.style)
    hint.style.height = height + 'px';
    hint.style.width = width + 'px';
    
    btn.addEventListener('click', function(){
      if(hint.style.visibility == 'hidden'){
        hint.style.visibility = 'visible';
        //hint.style.opacity = '1';
        hint.style.height = height + 'px';
        hint.style.width = width + 'px';
        hint.style.padding = '.5em';
      }
      else{
        hint.style.visibility = 'hidden';
        //hint.style.opacity = '0';
        hint.style.height = '0';
        hint.style.width = '0';
        hint.style.padding = '0';
      }
    
    });
    div#hint{
      background: gold;
      color: orangered;
      padding: .5em;
      box-sizing: border-box;
      overflow: hidden;
    
      font-weight: bold;
      transition: height 1s, width 1s, padding 1s, visibility 1s, opacity 0.5s ease-out;
    }

    This is some hint on how to be safe in this community

    This is another hint on how to be safe in this community

提交回复
热议问题