如何讓 jQuery 動畫更順暢?


使用 easing 庫來讓 jQuery 動畫更順暢。正確地使用動畫速度來形成網頁的完美動畫。不要加速動畫,在使用 animate() 時你應該知道在哪裡停止動畫。

例如,設定一個按鈕,單擊後逐漸消失並顯示文字區域

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

下面是使用 animation() 和 fadeout() 生成更順暢的動畫的文字區域

<div class="middle">
<textarea id="question-content"></textarea>
</div>

你可以嘗試執行以下程式碼來讓 jQuery 動畫更順暢

示例

 即時演示

<!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>


更新於: 22-6-2020

565 次瀏覽

開啟職業生涯

完成課程後獲得認證

開始
廣告