学习jQuery尺寸相关、滚动事件

jQuery尺寸相关、滚动事件

在这里插入图片描述
1.获取元素尺寸

width()、height()		获取元素width和height
innerWidth()、innerHeight()		包括padding的width和height
outerWidth()、outerHeight()		包括padding和border的width和height
outerWidth(true)、outerHeight(true)		包括padding和border以及margin的width和height

2.获取元素相对页面的绝对位置

offset()
var X = $('#DivID').offset().top;
var Y = $('#DivID').offset().left;
获取相对(父元素)位置
var X = $('#DivID').position().top;
var Y = $('#DivID').position().left;

练习一刻

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title></title>
		<script src="https://cdn.staticfile.org/jquery/1.10.2/jquery.min.js"></script>
		<script type="text/javascript">
			$(function(){
				console.log($('.div1').width());//200 width
				console.log($('.div1').innerWidth());//240 width+padding
				console.log($('.div1').outerWidth());//244 width+padding+border
				console.log($('.div1').outerWidth(true));//284 width+padding+border+margin
				console.log($('.div1').offset().top);//20 padding
				console.log($('.div2').position().top);//42 padding+border+margin
			})
		</script>
	</head>
	<body>
		<div class="div1" style="width: 200px;height: 200px;background-color: #0000CC;padding: 20px;border: 2px solid red;margin: 20px;">
			<div class="div2" style="width: 50px;height: 50px;background: #006400;"></div>
		</div>
	</body>
</html>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值