微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

jQuery 效果 - 淡入淡出

jQuery Fading 方法

通过 jQuery,您可以实现元素的淡入淡出效果

jQuery 拥有下面四种 fade 方法

  • fadeIn()
  • fadeOut()
  • fadetoggle()
  • fadeto()

jQuery fadeIn() 方法

jQuery fadeIn() 用于淡入已隐藏的元素。

语法:

$(selector).fadeIn(speed,callback);

可选的 speed 参数规定效果的时长。它可以取以下值:“slow”、“fast” 或毫秒。.

可选的 callback 参数是 fading 完成后所执行的函数名称

下面的例子演示了带有不同参数的 fadeIn() 方法

<!DOCTYPE html>
<html>
<head>
<Meta charset="utf-8">
<script src="https://cdn.staticfile.org/jquery/1.10.2/jquery.min.js">
</script>
<script>
$(document).ready(function(){
  $("button").click(function(){
    $("#div1").fadeIn();
    $("#div2").fadeIn("slow");
    $("#div3").fadeIn(3000);
  });
});
</script>
</head>

<body>
<p>以下实例演示了 fadeIn() 使用了不同参数的效果。</p>
<button>点击淡入 div 元素。</button>
<br><br>
<div id="div1" style="width:80px;height:80px;display:none;background-color:red;"></div><br>
<div id="div2" style="width:80px;height:80px;display:none;background-color:green;"></div><br>
<div id="div3" style="width:80px;height:80px;display:none;background-color:blue;"></div>

</body>
</html>

jQuery fadeOut() 方法

jQuery fadeOut() 方法用于淡出可见元素。

语法:

$(selector).fadeOut(speed,callback);

可选的 speed 参数规定效果的时长。它可以取以下值:“slow”、“fast” 或毫秒。

可选的 callback 参数是 fading 完成后所执行的函数名称

下面的例子演示了带有不同参数的 fadeOut() 方法

<!DOCTYPE html>
<html>
<head>
<Meta charset="utf-8">
<script src="https://cdn.staticfile.org/jquery/1.10.2/jquery.min.js">
</script>
<script>
$(document).ready(function(){
  $("button").click(function(){
    $("#div1").fadeOut();
    $("#div2").fadeOut("slow");
    $("#div3").fadeOut(3000);
  });
});
</script>
</head>

<body>
<p>以下实例演示了 fadeOut() 使用了不同参数的效果。</p>
<button>点击淡出 div 元素。</button>
<br><br>
<div id="div1" style="width:80px;height:80px;background-color:red;"></div><br>
<div id="div2" style="width:80px;height:80px;background-color:green;"></div><br>
<div id="div3" style="width:80px;height:80px;background-color:blue;"></div>

</body>
</html>

jQuery fadetoggle() 方法

jQuery fadetoggle() 方法可以在 fadeIn() 与 fadeOut() 方法之间进行切换。

如果元素已淡出,则 fadetoggle() 会向元素添加淡入效果

如果元素已淡入,则 fadetoggle() 会向元素添加淡出效果

语法:

$(selector).fadetoggle(speed,callback);

可选的 speed 参数规定效果的时长。它可以取以下值:“slow”、“fast” 或毫秒。

可选的 callback 参数是 fading 完成后所执行的函数名称

下面的例子演示了带有不同参数的 fadetoggle() 方法

<!DOCTYPE html>
<html>
<head>
<Meta charset="utf-8">
<title>菜鸟教程(runoob.com)</title>
<script src="https://cdn.staticfile.org/jquery/1.10.2/jquery.min.js">
</script>
<script>
$(document).ready(function(){
	$("button").click(function(){
		$("#div1").fadetoggle();
		$("#div2").fadetoggle("slow");
		$("#div3").fadetoggle(3000);
	});
});
</script>
</head>
<body>

<p>实例演示了 fadetoggle() 使用了不同的 speed(速度) 参数。</p>
<button>点击淡入/淡出</button>
<br><br>
<div id="div1" style="width:80px;height:80px;background-color:red;"></div>
<br>
<div id="div2" style="width:80px;height:80px;background-color:green;"></div>
<br>
<div id="div3" style="width:80px;height:80px;background-color:blue;"></div>

</body>
</html>

jQuery fadeto() 方法

jQuery fadeto() 方法允许渐变为给定的不透明度(值介于 0 与 1 之间)。

语法:

$(selector).fadeto(speed,opacity,callback);

必需的 speed 参数规定效果的时长。它可以取以下值:“slow”、“fast” 或毫秒。

fadeto() 方法中必需的 opacity 参数将淡入淡出效果设置为给定的不透明度(值介于 0 与 1 之间)。

可选的 callback 参数是该函数完成后所执行的函数名称

下面的例子演示了带有不同参数的 fadeto() 方法

<!DOCTYPE html>
<html>
<head>
<Meta charset="utf-8">
<script src="https://cdn.staticfile.org/jquery/1.10.2/jquery.min.js">
</script>
<script>
$(document).ready(function(){
  $("button").click(function(){
    $("#div1").fadeto("slow",0.15);
    $("#div2").fadeto("slow",0.4);
    $("#div3").fadeto("slow",0.7);
  });
});
</script>
</head>

<body>
<p>演示 fadeto() 使用不同参数</p>
<button>点我让颜色变淡</button>
<br><br>
<div id="div1" style="width:80px;height:80px;background-color:red;"></div><br>
<div id="div2" style="width:80px;height:80px;background-color:green;"></div><br>
<div id="div3" style="width:80px;height:80px;background-color:blue;"></div>

</body>
</html>

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐