如何使用 jQuery 為動畫新增緩動效果?


若要為動畫新增緩動效果,請妥善使用動畫速度來打造完美的網頁動畫。不要加速動畫,並應瞭解在使用 animate() 時應該在何處停止動畫。

此示例中,我們有一個按鈕,可將您帶到一個文字區域以新增答案


<button class="btn" id="answer">
   Add answer
</button>

現在妥善設定淡出屬性來設定緩動效果。

示例

 線上演示

<!DOCTYPE html>
<html>
<head>
   <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
   <script>
      $(document).ready(function(){
         $('body').on('click', '#answer', function() {
            var container = $('.new-answer');

            container.css('opacity', '0');

            $(this).fadeOut('fast', function() {
               $(this).hide();
               container.show();
               container.animate({ height: "200px",
               opacity: 1}).animate({
               height: "170px" });

            });
         });
      });
   </script>
<style>
.btn {
   width: 150px;
   height: 40px;
   color:#666666;
   text-decoration: none;
}
.new-answer {
   background: none repeat scroll 0 0 #00FFFF;
   border: 2px solid #94ACBE;
   border-radius: 5px 5px 5px 5px;
   margin-bottom: 40px;
}
.new-answer .middle, .top {
   padding: 0 10px;
}
.new-answer textarea {
   color: #000080;
   font: 12px;
   height: 120px;
   padding: 2px;
   width: 100%;
}
</style>
</head>
<body>
<button class="btn" id="answer">Add answer</button>
<div class="new-answer" hidden="true">
<div class="top">
<span>Add a new answer below</span>
</div>
<div class="middle">
<textarea id="question-content"></textarea>
</div>
</div>
</body>
</html>

更新時間:2020 年 6 月 21 日

529 次瀏覽

開啟你的 職業生涯

完成課程,獲得認證

立即開始
廣告