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

后端 未结 7 1359
别跟我提以往
别跟我提以往 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:50

    var btn = document.querySelector('button');
    
    btn.addEventListener('click', function(){
      var hint = document.getElementById('hint');
      if(hint.style.visibility == 'hidden'){
        hint.style.visibility = 'visible';
         hint.style.opacity = '1';
      }
      else{
        hint.style.visibility = 'hidden';
         hint.style.opacity = '0';
      }
    
    });
    div#hint{
      background: gold;
      color: orangered;
      padding: .5em;
    
      font-weight: bold;
      transition: visibility 1s, opacity 0.5s linear;
    }
    <div id='hint'>
      
      <p>This is some hint on how to be safe in this community </p>
       <p>This is another hint on how to be safe in this community </p>
      </div>
    
    <button> show hint </button>

    I think using visibility over display is better option

    0 讨论(0)
  • 2020-11-29 08:55

    Without using css3 transition, you can use js setInterval to change some css property of the div, such as:

    • Change opacity from 0 to 1

    • Change height from 0 to full height

    • Change width from 0 to full width

    Initially, you should have display: none; opacity: 0; height: 0; width: 0'

    Then you have to change display: none to display: block; before you use setInterval to change other properties.

    (I guess you know how to hide the div)

    You can also use setTimeout(), with a trick of recursive.

    0 讨论(0)
  • 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;
    }
    <div id='hint'>
      
      <p>This is some hint on how to be safe in this community </p>
      <p>This is another hint on how to be safe in this community </p>
    </div>
    
    <button> show hint </button>

    0 讨论(0)
  • 2020-11-29 08:58

    Hi I dont use display: block to display:none but changing the opacity, height and padding instead please review this one:

    var btn = document.querySelector('button');
    
    btn.addEventListener('click', function() {
      var hint = document.getElementById('hint');
      if (hint.classList.contains('h-hide')) {
        hint.classList.remove('h-hide');
      } else {
        hint.classList.add('h-hide');
      }
    });
    div#hint {
      display: block;
      background: gold;
      color: orangered;
      padding: .5em;
      font-weight: bold;
      transition: .5s all linear;
      opacity: 1;
      overflow: hidden;
      height: 100px;
    }
    #hint.h-hide {
      padding: 0;
      opacity: .25;
      height: 0;
    }
    <div id='hint'>
    
      <p>This is some hint on how to be safe in this community</p>
      <p>This is another hint on how to be safe in this community</p>
    </div>
    
    <button>show hint</button>

    the drawback for this approach is we have to keep tract of the div#hint height and change it using javascript if needed.

    0 讨论(0)
  • 2020-11-29 08:58

    Try something like this:

    var btn = document.querySelector('button');
    
    btn.addEventListener('click', function(){
      var hint = document.getElementById('hint');
    
      hint.classList.toggle("hide");
    });
    .hint{
      background: gold;
      color: orangered;
      padding: .5em;
      font-weight: bold;
      
      visibility: visible;
      opacity: 1;
      max-height: 500px;
      transition: visibility 0s, opacity 0.3s, max-height 0.6s linear;
    }
    
    .hide {
      visibility: hidden;
      opacity: 0;
      max-height: 0px;
      transition: max-height 0.3s, opacity 0.3s, visibility 0.3s linear;
    }
    <div id='hint' class="hint">
      
      <p>This is some hint on how to be safe in this community </p>
       <p>This is another hint on how to be safe in this community </p>
      </div>
    
    <button> show hint </button>

    0 讨论(0)
  • 2020-11-29 08:59

    I have also tried to do this
    please have a look if it can help you

    var btn = document.querySelector('button');
    var hint = document.getElementById('hint');
    hint.style.opacity = 1;
    hint.style.transition = "opacity 1s";
    
    btn.addEventListener('click', function(){
    
      if(hint.style.opacity == 0 || hint.style.opacity==''){
        hint.style.opacity = 1;
    
      }
      else{
       hint.style.opacity = 0;
    
      }
    
    });
    
    0 讨论(0)
提交回复
热议问题