【问题标题】:JavaScript - add transition between display:none and display:blockJavaScript - 在 display:none 和 display:block 之间添加过渡
【发布时间】:2017-03-19 16:52:44
【问题描述】:

我正在使用 JavaScript 来切换通知,如下所示。 如何在display: blockdisplay: none; 之间添加过渡

我不想添加像 jQuery 这样的外部库,因为我只会单独使用 toggle 效果。

var btn = document.querySelector('button');

btn.addEventListener('click', function(){
  var hint = document.getElementById('hint');
  if(hint.style.display == 'none'){
    hint.style.display = 'block';
  }
  else{
    hint.style.display = 'none';
  }

});
div#hint{
  background: gold;
  color: orangered;
  padding: .5em;
  font-weight: bold;
}
<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>

我知道我可以使用 jQuery 来实现这一点,如下所示。

$(document).ready(function(){

$('button').click(function(){
$('#hint').toggle('slow');

});

});
div#hint{
      background: gold;
      color: orangered;
      padding: .5em;
      font-weight: bold;
    }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<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>

#hint 像上面的jQuery 示例中那样切换时,我可以让按钮逐渐上下移动吗?我不希望按钮从一个位置跳转到另一个位置。

【问题讨论】:

    标签: javascript transition display


    【解决方案1】:

    您好,我不使用 display: blockdisplay:none 而是更改不透明度、高度和填充 请查看这个:

    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>

    这种方法的缺点是我们必须保留 div#hint 高度并在需要时使用 javascript 更改它。

    【讨论】:

      【解决方案2】:

      试试这样的:

      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>

      【讨论】:

        【解决方案3】:

        请参阅下面的示例:

        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>

        【讨论】:

        • 全部 100,000 个元素 "$el.style.display: none" 非常慢。因为重排了 HTML DOM。 "$el.style.visibility: hidden" 更快。但这仍然是空间。但是“高度:0”解决了它。好答案!
        【解决方案4】:

        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>

        我认为使用可见性而不是显示是更好的选择

        【讨论】:

        • 太酷了!!!这种过渡效果似乎是标准而清醒的……不错的一个;
        【解决方案5】:

        我也尝试过这样做
        看看能不能帮到你

        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;
        
          }
        
        });
        

        【讨论】:

        • 可能因为您没有提及 display 属性而被否决。
        【解决方案6】:

        @vothaison 的建议:CSS 过渡

        从技术上讲,@vothaison 想使用 setInterval 而不是 setTimeout,但我认为没有必要。这只是更多的工作。

        var hint = document.getElementById('hint');
        var btn = document.getElementById('btn_show');
        
        btn.addEventListener('click', function(){
          var ctr = 1;
          hint.className = hint.className !== 'show' ? 'show' : 'hide';
          if (hint.className === 'show') {
            hint.style.display = 'block';
            window.setTimeout(function(){
              hint.style.opacity = 1;
              hint.style.transform = 'scale(1)';
            },0);
          }
          if (hint.className === 'hide') {
            hint.style.opacity = 0;
            hint.style.transform = 'scale(0)';
            window.setTimeout(function(){
              hint.style.display = 'none';
            },700); // timed to match animation-duration
          }
         
        });
        #hint {
          background: yellow;
          color: red;
          padding: 16px;
          margin-bottom: 10px;
          opacity: 0;
          transform: scale(0);
          transition: .6s ease opacity,.6s ease transform;
        }
        <div id="hint" style="display: none;">
          <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 id="btn_show"> Show hint </button>

        使用 CSS 动画

        var hint = document.getElementById('hint');
        var btn = document.getElementById('btn_show');
        
        btn.addEventListener('click', function(){
          hint.className = hint.className !== 'show' ? 'show' : 'hide';
          if (hint.className === 'show') {
            setTimeout(function(){
              hint.style.display = 'block';
            },0); // timed to occur immediately
          }
          if (hint.className === 'hide') {
            setTimeout(function(){
              hint.style.display = 'none';
            },700); // timed to match animation-duration
          }
        });
        @-webkit-keyframes in {
          0% { -webkit-transform: scale(0) rotate(12deg); opacity: 0; visibility: hidden;  }
          100% { -webkit-transform: scale(1) rotate(0); opacity: 1; visibility: visible; }
        }
        
        @keyframes in {
          0% { transform: scale(0) rotate(12deg); opacity: 0; visibility: hidden;  }
          100% { transform: scale(1) rotate(0); opacity: 1; visibility: visible; }
        }
        
        @-webkit-keyframes out {
          0% { -webkit-transform: scale(1) rotate(0); opacity: 1; visibility: visible; }
          100% { -webkit-transform: scale(0) rotate(-12deg); opacity: 0; visibility: hidden; }
        }
        
        @keyframes out {
          0% { transform: scale(1) rotate(0); opacity: 1; visibility: visible; }
          100% { transform: scale(0) rotate(-12deg); opacity: 0; visibility: hidden;  }
        }
        
        #hint {
          background: yellow;
          color: red;
          padding: 16px;
          margin-bottom: 10px;
        }
        
        #hint.show {
          -webkit-animation: in 700ms ease both;
          animation: in 700ms ease both;
        }
        
        #hint.hide {
          -webkit-animation: out 700ms ease both;
          animation: out 700ms ease both;
        }
        <div id="hint" style="display: none;">
          <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 id="btn_show"> Show hint </button>

        使用原生 JavaScript

        使用 vanilla JavaScript 有很多很多方法可以做这种事情,所以这里是一种方法的速写:

        // you may need to polyfill requestAnimationFrame
        
        var hint = document.getElementById('hint');
        var btn = document.getElementById('btn_show');
        
        btn.addEventListener('click', function(){
          var ctr = 0;
          hint.className = hint.className !== 'show' ? 'show' : 'hide';
          
          if (hint.className === 'show') {
            window.setTimeout(function(){
              hint.style.display = 'block';
              fadein();
            },0); // do this asap        
          }
          
          if (hint.className === 'hide') {
            fadeout();
            window.setTimeout(function(){
              hint.style.display = 'none';
            },700); // time this to fit the animation
          }
          
          function fadein(){
            hint.style.opacity = ctr !== 10 ? '0.'+ctr : 1;
            hint.style.transform = ctr !== 10 ? 'scale('+('0.'+ctr)+')' : 'scale(1)';
            ctr++;
            
            if (ctr < 11)
              requestAnimationFrame(fadein);
            
            else
              ctr = 0;
          }
        
          function fadeout(){
            hint.style.opacity = 1 - ('0.'+ctr);
            hint.style.transform = 'scale('+(1 - ('0.'+ctr))+')';
            ctr++;
            
            if (ctr < 10)
              requestAnimationFrame(fadeout);
            else
              ctr = 0;
          }
        });
        #hint {
          background: yellow;
          color: red;
          padding: 16px;
          margin-bottom: 10px;
          opacity: 0;
        }
        <div id="hint" style="display: none;">
          <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 id="btn_show"> Show hint </button>

        说出你对 GreenSock、Velocity.js、jQuery 等的看法——它们都使这个显示和隐藏事物的过程变得微不足道。为什么不直接从 jQuery 的源代码中借用 show 和 hide 函数呢?

        【讨论】:

        • 感谢您的回答。您已经给了我足够的选项来选择,但是在问题的 jQuery 示例中,您是否注意到在切换 #hint 时按钮如何逐渐上下移动?你能帮我实现吗?我不希望按钮像您提供的答案那样从一个位置跳转到另一个位置。
        • @Abk 你需要修改元素的height,而不是像我在示例中那样使用transform: scale(x)
        • 好的。让我试试看。
        【解决方案7】:

        不使用css3 transition,可以使用js setInterval来改变div的一些css属性,如:

        • 将不透明度从 0 更改为 1

        • 将高度从 0 更改为全高

        • 将宽度从 0 更改为全宽

        最初,您应该拥有display: none; opacity: 0; height: 0; width: 0'

        那么你必须先将display: none改为display: block;,然后再使用setInterval来改变其他属性。

        (我猜你知道如何隐藏 div)

        你也可以使用 setTimeout(),带有递归的技巧。

        【讨论】:

        • 能否给问题中的sn-p添加一些sn-p来提供更多帮助?
        猜你喜欢
        • 2019-08-26
        • 2015-03-25
        • 2012-11-29
        • 2021-09-30
        • 1970-01-01
        • 2021-10-17
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多