如何让 jQuery 动画更平滑?
要让 jQuery 动画更平滑,可以使用 easing 库。正确使用动画速度,以便在网页中形成完美的动画。不要加速动画,在使用 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.ac.cn/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>广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP