a.隱藏效果
$(selector).hide(speed,callback);
$(selector).show(speed,callback);
(1)可選的 speed 參數規定隱藏/顯示的速度,可以取以下值:”slow”、”fast” 或毫秒。
(2)可選的 callback 參數是隱藏或顯示完成后所履行的函數名稱。
<!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(){
//設置緩慢隱藏
// $(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>中國辦事處</h3>
<div class="ex">
<button class="hide" type="button">隱藏</button><br>
<p>聯系人:張先生<br/>
北3環中路 100 號<br/>
北京</p>
</div>
<h3>美國辦事處</h3>
<div class="ex">
<button class="hide" type="button">隱藏</button>
<p>聯系人:David<br />
第5大街 200 號<br />
紐約</p>
</div>
</body>
</html>
$("button").click(function(){
$("p").hide(1000);
});
通過 jQuery,您可使用 toggle() 方法來切換 hide() 和 show() 方法。
顯示被隱藏的元素,并隱藏已顯示的元素:
$(document).ready(function(){
$("button").click(function(){
$("p").toggle();
});
});
</script>
通過 jQuery,您可以實現元素的淡入淡出效果。
jQuery 具有下面4種 fade 方法:
jQuery fadeIn() 用于淡入已隱藏的元素。
語法:
$(selector).fadeIn(speed,callback);
可選的 speed 參數規定效果的時長。它可以取以下值:”slow”、”fast” 或毫秒。
可選的 callback 參數是 fading 完成后所履行的函數名稱
<!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>演示帶有不同參數的 fadeIn() 方法。</p>
<button>點擊這里,使3個矩形淡入</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() 方法用于淡出可見元素。
語法:
$(selector).fadeOut(speed,callback);
可選的 speed 參數規定效果的時長。它可以取以下值:”slow”、”fast” 或毫秒。
可選的 callback 參數是 fading 完成后所履行的函數名稱。
<!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>演示帶有不同參數的 fadeOut() 方法。</p>
<button>點擊這里,使3個矩形淡出</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() 方法之間進行切換。
如果元素已淡出,則 fadeToggle() 會向元素添加淡入效果。
如果元素已淡入,則 fadeToggle() 會向元素添加淡出效果。
語法:
$(selector).fadeToggle(speed,callback);
可選的 speed 參數規定效果的時長。它可以取以下值:”slow”、”fast” 或毫秒。
可選的 callback 參數是 fading 完成后所履行的函數名稱。
<!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>演示帶有不同參數的 fadeToggle() 方法。</p>
<button>點擊這里,使3個矩形淡入淡出</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 之間)。
語法:
$(selector).fadeTo(speed,opacity,callback);
必須的 speed 參數規定效果的時長。它可以取以下值:”slow”、”fast” 或毫秒。
fadeTo() 方法中必須的 opacity 參數將淡入淡出效果設置為給定的不透明度(值介于 0 與 1 之間)。
可選的 callback 參數是該函數完成后所履行的函數名稱。
<!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>演示帶有不同參數的 fadeTo() 方法。</p>
<button>點擊這里,使3個矩形淡出</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,您可以在元素上創建滑動效果。
jQuery 具有以下滑動方法:
slideDown()
slideUp()
slideToggle()
jQuery slideDown() 方法用于向下滑動元素。
語法:
$(selector).slideDown(speed,callback);
可選的 speed 參數規定效果的時長。它可以取以下值:”slow”、”fast” 或毫秒。
可選的 callback 參數是滑動完成后所履行的函數名稱。
<!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 - 領先的 Web 技術教程站點</p>
<p>在 W3School,你可以找到你所需要的所有網站建設教程。</p>
</div>
<p class="flip">請點擊這里</p>
</body>
</html>
jQuery slideUp() 方法用于向上滑動元素。
語法:
$(selector).slideUp(speed,callback);
可選的 speed 參數規定效果的時長。它可以取以下值:”slow”、”fast” 或毫秒。
可選的 callback 參數是滑動完成后所履行的函數名稱。
<!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 - 領先的 Web 技術教程站點</p>
<p>在 W3School,你可以找到你所需要的所有網站建設教程。</p>
</div>
<p class="flip">請點擊這里</p>
</body>
</html>
jQuery slideToggle() 方法可以在 slideDown() 與 slideUp() 方法之間進行切換。
如果元素向下滑動,則 slideToggle() 可向上滑動它們。
如果元素向上滑動,則 slideToggle() 可向下滑動它們。
$(selector).slideToggle(speed,callback);
可選的 speed 參數規定效果的時長。它可以取以下值:”slow”、”fast” 或毫秒。
可選的 callback 參數是滑動完成后所履行的函數名稱。
<!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 - 領先的 Web 技術教程站點</p>
<p>在 W3School,你可以找到你所需要的所有網站建設教程。</p>
</div>
<p class="flip">請點擊這里</p>
</body>
</html>
jQuery 動畫 - animate() 方法
jQuery animate() 方法用于創建自定義動畫。
語法:
$(selector).animate({params},speed,callback);
必須的 params 參數定義構成動畫的 CSS 屬性。
可選的 speed 參數規定效果的時長。它可以取以下值:”slow”、”fast” 或毫秒。
可選的 callback 參數是動畫完成后所履行的函數名稱。
//1個實現從左到右移動的動畫
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>開始動畫</button>
<p>默許情況下,所有 HTML 元素的位置都是靜態的,并且沒法移動。如需對位置進行操作,記得首先把元素的 CSS position 屬性設置為 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>開始動畫</button>
<p>默許情況下,所有 HTML 元素的位置都是靜態的,并且沒法移動。如需對位置進行操作,記得首先把元素的 CSS position 屬性設置為 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>開始動畫</button>
<p>默許情況下,所有 HTML 元素的位置都是靜態的,并且沒法移動。如需對位置進行操作,記得首先把元素的 CSS position 屬性設置為 relative、fixed 或 absolute。</p>
<div style="background:#98bf21;height:100px;width:100px;position:absolute;">
</div>
</body>
</html>
//屬性的動畫值設置為 "show"、"hide" 或 "toggle":
$(document).ready(function(){
$("button").click(function(){
$("div").animate({
height:'toggle'
});
});
});
默許地,jQuery 提供針對動畫的隊列功能。
這意味著如果您在彼此以后編寫多個 animate() 調用,jQuery 會創建包括這些方法調用的“內部”隊列。然后逐1運行這些 animate 調用。
<!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>開始動畫</button>
<p>默許情況下,所有 HTML 元素的位置都是靜態的,并且沒法移動。如需對位置進行操作,記得首先把元素的 CSS position 屬性設置為 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>開始動畫</button>
<p>默許情況下,所有 HTML 元素的位置都是靜態的,并且沒法移動。如需對位置進行操作,記得首先把元素的 CSS position 屬性設置為 relative、fixed 或 absolute。</p>
<div style="background:#98bf21;height:100px;width:200px;position:absolute;">HELLO</div>
</body>
</html>
jQuery stop() 方法用于停止動畫或效果,在它們完成之前。
stop() 方法適用于所有 jQuery 效果函數,包括滑動、淡入淡出和自定義動畫。
<!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">停止滑動</button>
<div id="flip">點擊這里,向下滑動面板</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 樂趣10足!</p>
<button>點擊這里</button>
</body>
</html>