在jQuery中,许多方法的参数中都包含callback这个参数,如jQuery hide() 方法,jQuery show()方法,jQuery toggle()方法,jQuery fadeIn() 方法,jQuery fadeOut() 方法等。其实,callback为一个回调函数,在方法完成后执行。
示例;
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
div {
width: 200px;
height: 200px;
background-color: rgba(25, 172, 50, 0.705);
}
</style>
<script src="jQuery.min.js"></script>
<script>
$(function () {
$("button").click(function () {
$("div").animate({ opacity: ".5",borderRadius:"50%" },
function () {
alert('动画已完成');
})
})
})
</script>
</head>
<body>
<button>按钮</button>
<div></div>
</body>
</html>
本例中的回调函数:
function () {
alert('动画已完成');
})
点击按钮前:
点击按钮后:
执行动画并弹出了一个对话框