JQuery实现DIV的显示与隐藏

1.动画

animate

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<script type="text/javascript" src="jQuery/jquery-1.12.1.js"></script>
<script type="text/javascript">
$(function(){
	$("#div1").animate({height:"500px",width:"500px"},4000);
	// JQuery链式操作
	//$("#div1").animate({height:"500px"},2000).animate({width:"500px"},2000);
});
</script>
<style type="text/css">
#div1
{
	width:100px;
	height:100px;
	background-color:red;
	border:1px black solid;
}
</style>
</head>

<body>
<div id="div1"></div>
</body>
</html>


2.隐藏与显示

显示:show() 隐藏: hide()

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<script type="text/javascript" src="jQuery/jquery-1.12.1.js"></script>
<script type="text/javascript">
$(function(){
	$("#btn").click(function(){
		if($("#div1").css("display")=="none")
		{
			$("#info").html("正在显示...");
			$("#div1").show(2000,function(){
				$("#info").html("显示完毕");
			});
		}
		else
		{
			$("#info").html("正在隐藏...");
			$("#div1").hide(2000,function(){
				$("#info").html("隐藏完毕");
			});
		}
		
		/*$("#div1").toggle(2000);*/
	});
});
</script>
<style type="text/css">
#div1
{
	width:200px;
	height:200px;
	background-color:red;
	border:1px black solid;
	/*clear 属性设置一个元素的侧面是否允许其他的浮动元素。  both左右两侧均不允许浮动元素*/
	clear:both;
}
#btn,#info
{
	float:left;
}
#info
{
	margin-bottom:20px;
}
</style>
</head>

<body>
<input type="button" value="替换" id="btn"/>
<div id="info"></div>
<div id="div1"></div>
</body>
</html>


为什么我们要用到clear呢?

因为浮动的框可以向左或向右移动,直到它的外边缘碰到包含框或另一个浮动框的边框为止。由于浮动框不在文档的普通流中,所以文档的普通流中的块框表现得就像浮动框不存在一样。

所以我们需要clear:both  来清除掉左右的浮动框

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值