通過 jQuery,您可以實現(xiàn)元素的淡入淡出效果。
jQuery fadeIn() 用于淡入已隱藏的元素檀何。
1.先獲取按鈕,并寫一個點擊事件廷支,此處jQuery只需要寫chlik就可以频鉴。
2.下面的三個div元素分別寫一個“fadeIn”方法,“slow”是慢的意思酥泞,可以達到一個延遲
的效果砚殿,也可以在括號內(nèi)寫毫秒數(shù)或是“fast”。
<script>
$(document).ready(function(){
$("button").click(function(){
$("#div1").fadeIn();
$("#div2").fadeIn("slow");
$("#div3").fadeIn(3000);
});
});
</script>
jQuery fadeOut() 方法
1.和前面一樣芝囤,它可以取以下值:"slow"似炎、"fast" 或毫秒辛萍。
$("button").click(function(){
$("#div1").fadeOut();
$("#div2").fadeOut("slow");
$("#div3").fadeOut(3000);
});
jQuery fadeToggle() 方法
1.jQuery fadeToggle() 方法可以在 fadeIn() 與 fadeOut() 方法之間進行切換。
2.如果元素已淡出羡藐,則 fadeToggle() 會向元素添加淡入效果贩毕。
3.如果元素已淡入,則 fadeToggle() 會向元素添加淡出效果仆嗦。
4.取值和上面一樣辉阶。
$("button").click(function(){
$("#div1").fadeToggle();
$("#div2").fadeToggle("slow");
$("#div3").fadeToggle(3000);
});
jQuery fadeTo() 方法
1.jQuery fadeTo() 方法允許漸變?yōu)榻o定的不透明度(值介于 0 與 1 之間)。
2.fadeTo() 方法中必需的 opacity 參數(shù)將淡入淡出效果設(shè)置為給定的不透明度(值介于 0 與 1 之間)瘩扼。
$("button").click(function(){
$("#div1").fadeTo("slow",0.15);
$("#div2").fadeTo("slow",0.4);
$("#div3").fadeTo("slow",0.7);
});
簡單的拼接一下谆甜,感興趣就自己嘗試一下。
導(dǎo)入的jQuery包需要自己另修改集绰,
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>js動畫</title>
</head>
<script type="text/javascript" src="../js/jquery-1.12.4.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$("button").click(function(){
$("#div1").fadeIn();
$("#div2").fadeIn("slow");
$("#div3").fadeIn(3000);
$("#div1").fadeToggle();
$("#div2").fadeToggle("slow");
$("#div3").fadeToggle(3000);
});
});
</script>
<body>
<!-- <p>演示帶有不同參數(shù)的 fadeOut() 方法规辱。</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>