jQuery实现显示/消失/渐显/渐隐功能
1. 在Html的body部分定义两个input及一个div
<body>
<input type = "button" id = shows value = "显示" onclick = "shows()"/>
<input type = "button" id = hides value = "消失" onclick = "hides()"/>
<div id="divs"></div>
</body>
2.在head中加入css样式:
<style type = "text/css">
*{
margin:0;
padding:0;
list-style-type:none;
}
#divs{
width:100px;
height:100px;
background:#008B8B;
margin-top:10px;
}
</style>
效果如下:
3.导入jQuery(不做解释)
4.开始写script:
<script type="text/javascript">
function shows() {
// 显示
// $('#divs').show();
// 渐显
$('#divs').fadeIn(1000);
}
function hides() {
// 消失
// $('#divs').hide();
// 渐隐
$('#divs').fadeOut(1000);
}
</script>
完整代码展示:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
<script src="https://cdn.staticfile.org/jquery/1.10.2/jquery.min.js" type="text/javascript" charset="utf-8"></script>
<script type="text/javascript">
function shows() {
// 显示
// $('#divs').show();
// 渐显
$('#divs').fadeIn(1000);
}
function hides() {
// 消失
// $('#divs').hide();
// 渐隐
$('#divs').fadeOut(1000);
}
</script>
<style type="text/css">
* {
margin: 0px;
padding: 0;
list-style-type: none;
}
#divs {
width: 100px;
height: 100px;
background-color: #008B8B;
margin-top: 10px;
}
</style>
</head>
<body>
<input type="button" id="shows" value="显示" onclick="shows()" />
<input type="button" id="hides" value="消失" onclick="hides()" />
<div id="divs">
</div>
</body>
</html>
总结:
共4个关键字:
show()
hide()
fadeIn()
fadeOut()
其中
fadeIn()
fadeOut()
的括号中可以传时间参数,即:fadeIn(1000)
,其中的1000
表示1秒,即1s内div渐显;fadeOut()
同理