这篇文章将为大家详细讲解有关jQuery中如何实现HTML元素的淡入和淡出,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。

jQuery Fading 方法
通过 jQuery,您可以实现元素的淡入淡出效果。jQuery 拥有下面四种 fade 方法:

fadeIn()fadeOut()fadeToggle()fadeTo()


jQuery fadeIn() 方法
jQuery fadeIn() 用于淡入已隐藏的元素。语法:

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

可选的 speed 参数规定效果的时长。它可以取以下值:"slow"、"fast" 或毫秒。
可选的 callback 参数是 fading 完成后所执行的函数名称。
下面的例子演示了带有不同参数的 fadeIn() 方法:

<!DOCTYPEhtml><html><head><scriptsrc="../jquery/jquery-1.11.1.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>点击这里,使三个矩形淡入</button><br><br><divid="div1"style="width:80px;height:80px;display:none;background-color:red;"></div><br><divid="div2"style="width:80px;height:80px;display:none;background-color:green;"></div><br><divid="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() 方法:

<!DOCTYPEhtml><html><head><scriptsrc="../jquery/jquery-1.11.1.min.js"></script><scripttype="text/javascript">$(document).ready(function(){$("button").click(function(){$("#div1").fadeOut();$("#div2").fadeOut("slow");$("#div3").fadeOut(3000);});});</script></head><body><p>演示带有不同参数的fadeOut()方法。</p><button>点击这里,使三个矩形淡出</button><br><br><divid="div1"style="width:80px;height:80px;background-color:red;"></div><br><divid="div2"style="width:80px;height:80px;background-color:green;"></div><br><divid="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() 方法:

<!DOCTYPEhtml><html><head><scriptsrc="../jquery/jquery-1.11.1.min.js"></script><script>$(document).ready(function(){$("button").click(function(){$("#div1").fadeToggle();$("#div2").fadeToggle("slow");$("#div3").fadeToggle(3000);});});</script></head><body><p>演示带有不同参数的fadeToggle()方法。</p><button>点击这里,使三个矩形淡入淡出</button><br><br><divid="div1"style="width:80px;height:80px;background-color:red;"></div><br><divid="div2"style="width:80px;height:80px;background-color:green;"></div><br><divid="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() 方法:

<!DOCTYPEhtml><html><head><scriptsrc="../jquery/jquery-1.11.1.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><divid="div1"style="width:80px;height:80px;background-color:red;"></div><br><divid="div2"style="width:80px;height:80px;background-color:green;"></div><br><divid="div3"style="width:80px;height:80px;background-color:blue;"></div></body></html>

关于“jQuery中如何实现HTML元素的淡入和淡出”这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,使各位可以学到更多知识,如果觉得文章不错,请把它分享出去让更多的人看到。