128
106

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

CSS3 のみで display: none からフェードインさせる

Last updated at Posted at 2014-08-24

CSS3 のみで display: none; の状態からからフェードインさせる方法を調べてみました。たとえば特定のクラスがついたときにフェードインさせるには下記のようにすればできました。 displayanimation はできないので、下記のように 1% のキーフレームを追加して 表示かつ opacity: 0; としておくのがポイントです。

<!DOCTYPE html>
<html>
<head>
  <meta charst='utf-8'>
  <title>TEST</title>
  <script src="//code.jquery.com/jquery-1.11.1.min.js" type="text/javascript"></script>
  <style>
    .box {
      background: #000000;
      display: none;
      height: 240px;
      opacity: 0;
      width: 240px;
    }

    .visible.box {
      display: block;
      opacity: 1;
      animation-duration: 0.5s;
      animation-name: fade-in;
      -moz-animation-duration: 0.5s;
      -moz-animation-name: fade-in;
      -webkit-animation-duration: 0.5s;
      -webkit-animation-name: fade-in;
    }

    @keyframes fade-in {
      0% {
        display: none;
        opacity: 0;
      }

      1% {
        display: block;
        opacity: 0;
      }

      100% {
        display: block;
        opacity: 1;
      }
    }

    @-moz-keyframes fade-in {
      0% {
        display: none;
        opacity: 0;
      }

      1% {
        display: block;
        opacity: 0;
      }

      100% {
        display: block;
        opacity: 1;
      }
    }

    @-webkit-keyframes fade-in {
      0% {
        display: none;
        opacity: 0;
      }

      1% {
        display: block;
        opacity: 0;
      }

      100% {
        display: block;
        opacity: 1;
      }
    }
  </style>
  <script>
    $(function() {
      $('#btn').click(function() {
        $('.box').toggleClass('visible');
      });
    });
  </script>
</head>
<body>
<div class='container'>
  <div class="box"></div>
  <div>
    <button id='btn'>START</button>
  </div>
</div>
</body>
</html>

参考文献

128
106
3

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
128
106

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?