我如何淡入和淡出这个Swing效果?

时间:2016-01-26 18:56:33

标签: html css

我这样编写代码:https://jsfiddle.net/9dgjkc9r/



.question{
  display: inline-block;
  position: absolute;
  width: 250px;
  height: 50px;
  background: #272822;
  box-shadow: 0.5px 0.5px 5px #000000;
  text-align: center;
}
.question:hover{
  -moz-animation: 3s ease 0s normal none swing;
  -moz-transform-origin: center top;
  -webkit-animation:swing 3s ease-in-out;
  -webkit-transform-origin:top;
}
@-moz-keyframes swing{
  0%{-moz-transform:rotate(-3deg)}
  50%{-moz-transform:rotate(3deg)}
  100%{-moz-transform:rotate(-3deg)}
}
@-webkit-keyframes swing{
  0%{-webkit-transform:rotate(-3deg)}
  50%{-webkit-transform:rotate(3deg)}
  100%{-webkit-transform:rotate(-3deg)}
}

<div class="question"></div>
&#13;
&#13;
&#13;

但它并没有淡入和淡出,因为当我将鼠标移动到另一个位置时,它显示出一种丑陋的动作。我不知道如何添加它们。

2 个答案:

答案 0 :(得分:1)

只需从初始位置开始转换,然后将转换分解为4个关键帧,如下所示:

0% {
    -webkit-transform: rotate(0deg)
  }

  25% {
    -webkit-transform: rotate(-3deg)
  }
  75% {
    -webkit-transform: rotate(3deg)
  }
  100% {
    -webkit-transform: rotate(0deg)
  }

Here is the JSFiddle demo

(至少我理解你想要的:))

答案 1 :(得分:0)

这就是你想要的。复制完整代码并试一试。

 <html>
    <head>
    <title>Welcome !</title>
     <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"></script>

    <style type="text/css">
      body{
        margin:0;
        padding: 0;
      }

    .question{
      display: inline-block;
      position: absolute;
      width: 250px;
      height: 50px;
      background: #272822;
      box-shadow: 0.5px 0.5px 5px #000000;
      text-align: center;
    }

    .question:hover{
      -moz-animation: 3s ease 0s normal none swing;
        -moz-transform-origin: center top;
        -webkit-animation:swing 3s ease-in-out;
        -webkit-transform-origin:top;
    }

    @-moz-keyframes swing{
        0%{-moz-transform:rotate(-3deg)}
        50%{-moz-transform:rotate(3deg)}
        100%{-moz-transform:rotate(-3deg)}
    }
    @-webkit-keyframes swing{
        0%{-webkit-transform:rotate(-3deg)}
        50%{-webkit-transform:rotate(3deg)}
        100%{-webkit-transform:rotate(-3deg)}
    }

    </style>


     <body>

    <div class="question"></div>

    <script type="text/javascript">
      $(document).ready(function(){
     $("div.question").mouseenter(function(){
      $(this).fadeOut(1000).fadeIn(1000);
     });
    });

    </script>

     </body>
    </html>

或者如果你想做fadein并且无限时间,那么试试这个

<html>
<head>
<title>Welcome !</title>
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"></script>

<style type="text/css">
  body{
    margin:0;
    padding: 0;
  }

.question{
  display: inline-block;
  position: absolute;
  width: 250px;
  height: 50px;
  background: #272822;
  box-shadow: 0.5px 0.5px 5px #000000;
  text-align: center;
}

.question:hover{
  -moz-animation: 3s ease 0s normal none swing;
    -moz-transform-origin: center top;
    -webkit-animation:swing 3s ease-in-out;
    -webkit-transform-origin:top;
}

@-moz-keyframes swing{
    0%{-moz-transform:rotate(-3deg)}
    50%{-moz-transform:rotate(3deg)}
    100%{-moz-transform:rotate(-3deg)}
}
@-webkit-keyframes swing{
    0%{-webkit-transform:rotate(-3deg)}
    50%{-webkit-transform:rotate(3deg)}
    100%{-webkit-transform:rotate(-3deg)}
}

</style>


 <body>

<div class="question"></div>

<script type="text/javascript">
  $(document).ready(function(){
    check(1000);
});

function check(i)
{
  $(".question").fadeOut(1000).fadeIn(1000);
  setTimeout("check()",i);
}

</script>

 </body>
</html>