a.隱藏效果
$(selector).hide(speed,callback);
$(selector).show(speed,callback);
(1)可選的 speed 參數(shù)規(guī)定隱藏/顯示的速度,可以取以下值:”slow”、”fast” 或毫秒。
(2)可選的 callback 參數(shù)是隱藏或顯示完成后所履行的函數(shù)名稱。
<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF⑻">
<title></title>
<script src="jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$(".ex .hide").click(function(){
//設(shè)置緩慢隱藏
// $(this).parents(".ex").hide("slow");
// });
$(this).parents(".ex").hide("slow",function(){
alert('ddd');
});
});
</script>
<style type="text/css">
.ex{
background-color: #e5eecc;
padding: 7px;
border: solid 1px #c3c3c3;
}
</style>
</head>
<body>
<h3>中國(guó)辦事處</h3>
<div class="ex">
<button class="hide" type="button">隱藏</button><br>
<p>聯(lián)系人:張先生<br/>
北3環(huán)中路 100 號(hào)<br/>
北京</p>
</div>
<h3>美國(guó)辦事處</h3>
<div class="ex">
<button class="hide" type="button">隱藏</button>
<p>聯(lián)系人:David<br />
第5大街 200 號(hào)<br />
紐約</p>
</div>
</body>
</html>
$("button").click(function(){
$("p").hide(1000);
});
通過(guò) jQuery,您可使用 toggle() 方法來(lái)切換 hide() 和 show() 方法。
顯示被隱藏的元素,并隱藏已顯示的元素:
$(document).ready(function(){
$("button").click(function(){
$("p").toggle();
});
});
</script>
通過(guò) jQuery,您可以實(shí)現(xiàn)元素的淡入淡出效果。
jQuery 具有下面4種 fade 方法:
jQuery fadeIn() 用于淡入已隱藏的元素。
語(yǔ)法:
$(selector).fadeIn(speed,callback);
可選的 speed 參數(shù)規(guī)定效果的時(shí)長(zhǎng)。它可以取以下值:”slow”、”fast” 或毫秒。
可選的 callback 參數(shù)是 fading 完成后所履行的函數(shù)名稱
<!DOCTYPE html>
<html>
<head>
<script src="/jquery/jquery⑴.11.1.min.js"></script>
<script>
$(document).ready(function(){
$("button").click(function(){
$("#div1").fadeIn();
$("#div2").fadeIn("slow");
$("#div3").fadeIn(3000);
});
});
</script>
</head>
<body>
<p>演示帶有不同參數(shù)的 fadeIn() 方法。</p>
<button>點(diǎn)擊這里,使3個(gè)矩形淡入</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() 方法用于淡出可見(jiàn)元素。
語(yǔ)法:
$(selector).fadeOut(speed,callback);
可選的 speed 參數(shù)規(guī)定效果的時(shí)長(zhǎng)。它可以取以下值:”slow”、”fast” 或毫秒。
可選的 callback 參數(shù)是 fading 完成后所履行的函數(shù)名稱。
<!DOCTYPE html>
<html>
<head>
<script src="/jquery/jquery⑴.11.1.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$("button").click(function(){
$("#div1").fadeOut();
$("#div2").fadeOut("slow");
$("#div3").fadeOut(3000);
});
});
</script>
</head>
<body>
<p>演示帶有不同參數(shù)的 fadeOut() 方法。</p>
<button>點(diǎn)擊這里,使3個(gè)矩形淡出</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() 方法可以在 fadeIn() 與 fadeOut() 方法之間進(jìn)行切換。
如果元素已淡出,則 fadeToggle() 會(huì)向元素添加淡入效果。
如果元素已淡入,則 fadeToggle() 會(huì)向元素添加淡出效果。
語(yǔ)法:
$(selector).fadeToggle(speed,callback);
可選的 speed 參數(shù)規(guī)定效果的時(shí)長(zhǎng)。它可以取以下值:”slow”、”fast” 或毫秒。
可選的 callback 參數(shù)是 fading 完成后所履行的函數(shù)名稱。
<!DOCTYPE html>
<html>
<head>
<script src="/jquery/jquery⑴.11.1.min.js"></script>
<script>
$(document).ready(function(){
$("button").click(function(){
$("#div1").fadeToggle();
$("#div2").fadeToggle("slow");
$("#div3").fadeToggle(3000);
});
});
</script>
</head>
<body>
<p>演示帶有不同參數(shù)的 fadeToggle() 方法。</p>
<button>點(diǎn)擊這里,使3個(gè)矩形淡入淡出</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>
</body>
</html>
jQuery fadeTo() 方法允許漸變成給定的不透明度(值介于 0 與 1 之間)。
語(yǔ)法:
$(selector).fadeTo(speed,opacity,callback);
必須的 speed 參數(shù)規(guī)定效果的時(shí)長(zhǎng)。它可以取以下值:”slow”、”fast” 或毫秒。
fadeTo() 方法中必須的 opacity 參數(shù)將淡入淡出效果設(shè)置為給定的不透明度(值介于 0 與 1 之間)。
可選的 callback 參數(shù)是該函數(shù)完成后所履行的函數(shù)名稱。
<!DOCTYPE html>
<html>
<head>
<script src="/jquery/jquery⑴.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>演示帶有不同參數(shù)的 fadeTo() 方法。</p>
<button>點(diǎn)擊這里,使3個(gè)矩形淡出</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>
通過(guò) jQuery,您可以在元素上創(chuàng)建滑動(dòng)效果。
jQuery 具有以下滑動(dòng)方法:
slideDown()
slideUp()
slideToggle()
jQuery slideDown() 方法用于向下滑動(dòng)元素。
語(yǔ)法:
$(selector).slideDown(speed,callback);
可選的 speed 參數(shù)規(guī)定效果的時(shí)長(zhǎng)。它可以取以下值:”slow”、”fast” 或毫秒。
可選的 callback 參數(shù)是滑動(dòng)完成后所履行的函數(shù)名稱。
<!DOCTYPE html>
<html>
<head>
<script src="/jquery/jquery⑴.11.1.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$(".flip").click(function(){
$(".panel").slideDown("slow");
});
});
</script>
<style type="text/css">
div.panel,p.flip
{
margin:0px;
padding:5px;
text-align:center;
background:#e5eecc;
border:solid 1px #c3c3c3;
}
div.panel
{
height:120px;
display:none;
}
</style>
</head>
<body>
<div class="panel">
<p>W3School - 領(lǐng)先的 Web 技術(shù)教程站點(diǎn)</p>
<p>在 W3School,你可以找到你所需要的所有網(wǎng)站建設(shè)教程。</p>
</div>
<p class="flip">請(qǐng)點(diǎn)擊這里</p>
</body>
</html>
jQuery slideUp() 方法用于向上滑動(dòng)元素。
語(yǔ)法:
$(selector).slideUp(speed,callback);
可選的 speed 參數(shù)規(guī)定效果的時(shí)長(zhǎng)。它可以取以下值:”slow”、”fast” 或毫秒。
可選的 callback 參數(shù)是滑動(dòng)完成后所履行的函數(shù)名稱。
<!DOCTYPE html>
<html>
<head>
<script src="/jquery/jquery⑴.11.1.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$(".flip").click(function(){
$(".panel").slideUp("slow");
});
});
</script>
<style type="text/css">
div.panel,p.flip
{
margin:0px;
padding:5px;
text-align:center;
background:#e5eecc;
border:solid 1px #c3c3c3;
}
div.panel
{
height:120px;
}
</style>
</head>
<body>
<div class="panel">
<p>W3School - 領(lǐng)先的 Web 技術(shù)教程站點(diǎn)</p>
<p>在 W3School,你可以找到你所需要的所有網(wǎng)站建設(shè)教程。</p>
</div>
<p class="flip">請(qǐng)點(diǎn)擊這里</p>
</body>
</html>
jQuery slideToggle() 方法可以在 slideDown() 與 slideUp() 方法之間進(jìn)行切換。
如果元素向下滑動(dòng),則 slideToggle() 可向上滑動(dòng)它們。
如果元素向上滑動(dòng),則 slideToggle() 可向下滑動(dòng)它們。
$(selector).slideToggle(speed,callback);
可選的 speed 參數(shù)規(guī)定效果的時(shí)長(zhǎng)。它可以取以下值:”slow”、”fast” 或毫秒。
可選的 callback 參數(shù)是滑動(dòng)完成后所履行的函數(shù)名稱。
<!DOCTYPE html>
<html>
<head>
<script src="/jquery/jquery⑴.11.1.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$(".flip").click(function(){
$(".panel").slideToggle("slow");
});
});
</script>
<style type="text/css">
div.panel,p.flip
{
margin:0px;
padding:5px;
text-align:center;
background:#e5eecc;
border:solid 1px #c3c3c3;
}
div.panel
{
height:120px;
display:none;
}
</style>
</head>
<body>
<div class="panel">
<p>W3School - 領(lǐng)先的 Web 技術(shù)教程站點(diǎn)</p>
<p>在 W3School,你可以找到你所需要的所有網(wǎng)站建設(shè)教程。</p>
</div>
<p class="flip">請(qǐng)點(diǎn)擊這里</p>
</body>
</html>
jQuery 動(dòng)畫(huà) - animate() 方法
jQuery animate() 方法用于創(chuàng)建自定義動(dòng)畫(huà)。
語(yǔ)法:
$(selector).animate({params},speed,callback);
必須的 params 參數(shù)定義構(gòu)成動(dòng)畫(huà)的 CSS 屬性。
可選的 speed 參數(shù)規(guī)定效果的時(shí)長(zhǎng)。它可以取以下值:”slow”、”fast” 或毫秒。
可選的 callback 參數(shù)是動(dòng)畫(huà)完成后所履行的函數(shù)名稱。
//1個(gè)實(shí)現(xiàn)從左到右移動(dòng)的動(dòng)畫(huà)
demo1:
<!DOCTYPE html>
<html>
<head>
<script src="/jquery/jquery⑴.11.1.min.js">
</script>
<script>
$(document).ready(function(){
$("button").click(function(){
$("div").animate({left:'250px'});
});
});
</script>
</head>
<body>
<button>開(kāi)始動(dòng)畫(huà)</button>
<p>默許情況下,所有 HTML 元素的位置都是靜態(tài)的,并且沒(méi)法移動(dòng)。如需對(duì)位置進(jìn)行操作,記得首先把元素的 CSS position 屬性設(shè)置為 relative、fixed 或 absolute。</p>
<div style="background:#98bf21;height:100px;width:100px;position:absolute;">
</div>
</body>
</html>
demo2:
<!DOCTYPE html>
<html>
<head>
<script src="/jquery/jquery⑴.11.1.min.js">
</script>
<script>
$(document).ready(function(){
$("button").click(function(){
$("div").animate({
left:'250px',
opacity:'0.5',
height:'150px',
width:'150px'
});
});
});
</script>
</head>
<body>
<button>開(kāi)始動(dòng)畫(huà)</button>
<p>默許情況下,所有 HTML 元素的位置都是靜態(tài)的,并且沒(méi)法移動(dòng)。如需對(duì)位置進(jìn)行操作,記得首先把元素的 CSS position 屬性設(shè)置為 relative、fixed 或 absolute。</p>
<div style="background:#98bf21;height:100px;width:100px;position:absolute;">
</div>
</body>
</html>
demo3:
<!DOCTYPE html>
<html>
<head>
<script src="/jquery/jquery⑴.11.1.min.js"></script>
<script>
$(document).ready(function(){
$("button").click(function(){
$("div").animate({
left:'250px',
height:'+=150px',
width:'+=150px'
});
});
});
</script>
</head>
<body>
<button>開(kāi)始動(dòng)畫(huà)</button>
<p>默許情況下,所有 HTML 元素的位置都是靜態(tài)的,并且沒(méi)法移動(dòng)。如需對(duì)位置進(jìn)行操作,記得首先把元素的 CSS position 屬性設(shè)置為 relative、fixed 或 absolute。</p>
<div style="background:#98bf21;height:100px;width:100px;position:absolute;">
</div>
</body>
</html>
//屬性的動(dòng)畫(huà)值設(shè)置為 "show"、"hide" 或 "toggle":
$(document).ready(function(){
$("button").click(function(){
$("div").animate({
height:'toggle'
});
});
});
默許地,jQuery 提供針對(duì)動(dòng)畫(huà)的隊(duì)列功能。
這意味著如果您在彼此以后編寫(xiě)多個(gè) animate() 調(diào)用,jQuery 會(huì)創(chuàng)建包括這些方法調(diào)用的“內(nèi)部”隊(duì)列。然后逐1運(yùn)行這些 animate 調(diào)用。
<!DOCTYPE html>
<html>
<head>
<script src="jquery.min.js"></script>
<script>
$(document).ready(function(){
$("button").click(function(){
var div=$("div");
div.animate({height:'300px',opacity:'0.4'},"slow");
div.animate({width:'300px',opacity:'0.8'},"slow");
div.animate({height:'100px',opacity:'0.4'},"slow");
div.animate({width:'100px',opacity:'0.8'},"slow");
});
});
</script>
</head>
<body>
<button>開(kāi)始動(dòng)畫(huà)</button>
<p>默許情況下,所有 HTML 元素的位置都是靜態(tài)的,并且沒(méi)法移動(dòng)。如需對(duì)位置進(jìn)行操作,記得首先把元素的 CSS position 屬性設(shè)置為 relative、fixed 或 absolute。</p>
<div style="background:#98bf21;height:100px;width:100px;position:absolute;">
</div>
</body>
</html>
<html>
<head>
<meta charset="u">
<script src="jquery.min.js"></script>
<script>
$(document).ready(function(){
$("button").click(function(){
var div=$("div");
div.animate({left:'100px',opacity:'0.6'},"slow");
div.animate({fontSize:'3em',color: '2E9ECE'},"slow");
});
});
</script>
</head>
<body>
<button>開(kāi)始動(dòng)畫(huà)</button>
<p>默許情況下,所有 HTML 元素的位置都是靜態(tài)的,并且沒(méi)法移動(dòng)。如需對(duì)位置進(jìn)行操作,記得首先把元素的 CSS position 屬性設(shè)置為 relative、fixed 或 absolute。</p>
<div style="background:#98bf21;height:100px;width:200px;position:absolute;">HELLO</div>
</body>
</html>
jQuery stop() 方法用于停止動(dòng)畫(huà)或效果,在它們完成之前。
stop() 方法適用于所有 jQuery 效果函數(shù),包括滑動(dòng)、淡入淡出和自定義動(dòng)畫(huà)。
<!DOCTYPE html>
<html>
<head>
<script src="/jquery/jquery⑴.11.1.min.js"></script>
<script>
$(document).ready(function(){
$("#flip").click(function(){
$("#panel").slideDown(5000);
});
$("#stop").click(function(){
$("#panel").stop();
});
});
</script>
<style type="text/css">
#panel,#flip
{
padding:5px;
text-align:center;
background-color:#e5eecc;
border:solid 1px #c3c3c3;
}
#panel
{
padding:50px;
display:none;
}
</style>
</head>
<body>
<button id="stop">停止滑動(dòng)</button>
<div id="flip">點(diǎn)擊這里,向下滑動(dòng)面板</div>
<div id="panel">Hello world!</div>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<script src="/jquery/jquery⑴.11.1.min.js"></script>
<script>
$(document).ready(function()
{
$("button").click(function(){
$("#p1").css("color","red").slideUp(2000).slideDown(2000);
});
});
</script>
</head>
<body>
<p id="p1">jQuery 樂(lè)趣10足!</p>
<button>點(diǎn)擊這里</button>
</body>
</html>