jQuery delay()方法
delay()方法是jQuery中的一个内置方法,用于设置一个定时器来延迟队列中下一个项目的执行。
语法:
$(selector).delay(para1, para2);
JavaScript
参数:它接受两个参数,具体如下。
- para1:它规定了延迟的速度。
- para2:它是可选的,指定了队列的名称。
返回值:它以指定的速度返回选定的元素。
例子1:在这个例子中,定时器被设置为所有的块。
<!DOCTYPE html>
<html>
<head>
<script src=
"https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js">
</script>
<!-- jQuery code to demonstrate delay method -->
<script>
(document).ready(function () {
("button").click(function () {
("#d1").delay("slow").fadeIn();
("#d2").delay("fast").fadeIn();
("#d3").delay(1000).fadeIn();
("#d4").delay(4000).fadeIn();
});
});
</script>
</head>
<body>
<!-- Click on this button -->
<button>Click Me!</button>
<br><br>
<div id="d1" style="width:50px;height:50px;display:
none;background-color:lightgreen;"></div>
<br>
<div id="d2" style="width:50px;height:50px;display:
none;background-color:green;"></div>
<br>
<div id="d3" style="width:50px;height:50px;display:
none;background-color:orange;"></div>
<br>
<div id="d4" style="width:50px;height:50px;display:
none;background-color:yellow;"></div>
<br>
</body>
</html>
HTML
输出:
例子2:在这个例子中,已经展示了如何使用这种方法来延迟一个动画。
<!DOCTYPE html>
<html>
<head>
<script src=
"https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js">
</script>
<!--jQuery code to show the working of delay method-->
<script>
(document).ready(function () {
("#btn1").click(function () {
("div").animate({
height: "150px"
});
("div").animate({
width: "150px"
});
$("div").delay(1200).animate({
width: "300px",
padding: "30px"
});
});
});
</script>
<style>
#d {
display: block;
width: 200px;
height: 100px;
background-color: green;
font-size: 30px;
padding: 10px;
}
</style>
</head>
<body>
<div id="d">GeeksforGeeks !</div>
<!-- Click on this button to show
the effect of delay method -->
<p>
<button id="btn1">Click Me!</button>
</p>
</body>
</html>
HTML
输出: