jQuery停止动画
jQuery的stop()方法是用来在动画或效果完成之前停止它。
这种方法适用于所有类型的动画,如滑动、渐变和自定义动画。
语法:
$(selector).stop(stopAll, goToEnd);
例子-1:停止滑动的动画。
<!DOCTYPE html>
<html>
<head>
<title>jQuery stop() Method
</title>
<script src=
"https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js">
</script>
<style>
#panel,
#flip {
padding: 5px;
font-size: 18px;
text-align: center;
background-color: green;
color: white;
border: solid 1px #666;
border-radius: 3px;
}
#panel {
padding: 50px;
display: none;
}
</style>
</head>
<body>
<center>
<h1 style="color:green;">
GeeksForGeeks</h1>
<h2 id="GFG">
jQuery stop() Method</h2>
<br>
<button id="stop">Click
</button>
<br>
<br>
<div id="flip">
<h2>GeeksForGeeks</h2></div>
<div id="panel">
A computer science portal for geeks
</div>
<script>
(document).ready(function() {
("#flip").click(function() {
("#panel").slideDown(5000);
});
("#stop").click(function() {
$("#panel").stop();
});
});
</script>
</center>
</body>
</html>
输出:
在点击GeeksForGeeks之前:
点击GeeksForGeeks后,没有点击Button:
点击GeeksForGeeks并点击按钮后:
例子-2:停止从左到右的滑动。
<!DOCTYPE html>
<html>
<head>
<title>jQuery stop() Method</title>
<script src=
"https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js">
</script>
<style>
#panel,
#flip {
padding: 5px;
font-size: 18px;
text-align: center;
background-color: green;
color: white;
border: solid 1px #666;
border-radius: 3px;
}
#panel {
padding: 50px;
display: none;
}
</style>
</head>
<body>
<center>
<h1 style="color:green;">
GeeksForGeeks</h1>
<h2 id="GFG"> jQuery stop() Method</h2>
<br>
<button id="start">Start</button>
<button id="stop">Stop</button>
<br>
<br>
<div style="background:green;
height:100px;
width:400px;
position:absolute;">
GeeksForGeeks
</div>
<script>
(document).ready(function() {
("#start").click(function() {
("div").animate({
left: '80px'
}, 5000);
("div").animate({
fontSize: '3em'
}, 5000);
});
("#stop").click(function() {
("div").stop();
});
});
</script>
</center>
</body>
</html>
输出:
在点击 “开始 “之前:
点击 “开始 “后,没有点击 “停止”:
点击开始并点击停止后: