jQuery和javascript的那些宽高

jQuery和javascript的那些宽高
1.jQuery获取容器的宽高
$(ele).height() -- 获取容器的内容高度
只包含内容高度,不包含padding和border;
注意:(1)、父元素添加box-sizing属性
<!doctype html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title></title>
	<style>	
		body, p{
			margin: 0;	
			padding: 0;
		}
		.body{
			padding: 100px;
			background: #f00;
			font-style: 40px;
			height: 100px;	
		}
		.wrap{
                        height: 300px
			padding: 20px;
                        box-sising: border-box;
		}
	</style>
</head>
<body>
		
	<div class="wrap">	
		<div class="body">	
			我是内容
		</div>
	</div>
	<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.js"></script>
	<script>
		var getHeight = function(){
		      var wrapH = $('.wrap').height();
		      console.log('wrapH = '+wrapH); /*输出为260*/
		}
		getHeight();

	</script>	
</body>
</html>
由此Demo可知,虽然box-sising: border-box在css布局中起到height包含padding和border,但jquery获取则不如此;
(2)、通过$(ele).css('height')获取的高度带单位,而$.height()获取的高度不带单位。

$.innerHeight() -- 容器的宽高(包括padding)
包含容器的padding
<!doctype html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title></title>
	<style>	
		body, p{
			margin: 0;	
			padding: 0;
		}
		.body{
			padding: 100px;
			background: #f00;
			font-style: 40px;
			height: 100px;	
		}
		.wrap{
                        height: 300px
			padding: 20px;
                        box-sising: border-box;
		}
	</style>
</head>
<body>
		
	<div class="wrap">	
		<div class="body">	
			我是内容
		</div>
	</div>
	<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.js"></script>
	<script>
		var getHeight = function(){
		      var wrapH = $('.wrap').innerHeight();
		      console.log('wrapH = '+wrapH); /*输出为300*/
		}
		getHeight();
	</script>	
</body>
</html>
$.outerHeight() -- 容器的宽高(包含padding和border)
注意:(1). 通过传入参数 true,则获取的outerHeight包含margin
Demo:
<!doctype html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title></title>
	<style>	
		body, p{
			margin: 0;	
			padding: 0;
		}
		.body{
			padding: 100px;
			background: #00f;
			font-style: 40px;
			height: 100px;	
		}
		.wrapNoMar{
            height: 300px;
			margin: 0;
		}
		.wrapMar{
			height: 300px;
			margin: 20px;
		}
	</style>
</head>
<body>
		
	<div class="wrapNoMar">	
		<div class="body">	
			我是内容
		</div>
	</div>
	<div class="wrapMar">	
		<div class="body">	
			我是内容
		</div>
	</div>
	<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.js"></script>
	<script>
		var getHeight = function(){
		      var wrapH = $('.wrap').outerHeight();
		      console.log('wrapH = '+wrapH); /*输出为300*/
		      var wrapH = $('.wrap').outerHeight(true);
		      console.log('wrapH = '+wrapH); /*输出为340*/
		      var wrapH = $('.wrap').outerHeight();
		      console.log('wrapH = '+wrapH); /*输出为300*/
		}
		getHeight();
	</script>	
</body>
</html>
window.height()、window.innerHeight()、window.outerHeight()不推荐设置宽高
2.原生Javascript的宽高(属性)
element.clientHeight -- 返回元素的可见高度
容器 高度+padding(不包括滚动条的宽度)

element.offsetHeight -- 返回元素的高度
容器 高度+padding+border(包括滚动条的宽度)

element.offsetTop -- 返回元素垂直方向的偏移量
(1)、父元素设置position: relative;属性
offsetTop的相对元素为设置相对定位的父元素
(2)、父元素不设置相对定位
offsetTop的相对元素为html,包含html的padding和border、body的padding和border

element.scrollHeight -- 返回元素内容的整体高度
容器内容的高度
element.style.scrollTop -- 返回元素内容上边缘距离容器顶部的距离
容器被隐藏部分的内容高度

element.style.height -- 返回元素的高度
由于style对象属性是从元素对象中获取,所以如果HTML元素没有在内联设置style的属性,获取的将是默认值‘’(空);且style的border不受box-sizing属性的影响

3.jQuery的offset()与position()方法
$.offset() -- 返回一个对象,对象包含top和left属性,分别存放垂直偏移值和水平偏移值;包含html的padding、body的padding
(1).父子元素均没有设置position属性
参照对象为html元素
(2).父元素设置position属性值,子元素没有设置position属性
参照对象为html元素
(3).父子元素均设置position属性
参照对象为html元素
$.position() -- 返回对象,对象是设置了position属性的临近父元素,且包含html的padding、body的padding以及border;但需要减去html的border;
(1).父子元素均没有设置position属性
参照对象为html元素
(2).父元素设置position属性值,子元素没有设置position属性
参照对象为父元素元素
(3).父子元素均设置position属性
参照对象为父元素元素

总结:ele.offsetLeft/ele.offsetTop、$.offset()以及$.position()
ele.offsetLeft/ele.offsetTop: 包含html的border与padding、body的border与padding
$.offset():包含html的padding、body的border与padding
$.position():包含html的padding、body的border与padding,但需要减去html的border

ele.offsetLeft/ele.offsetTop 与 $.offset()无论父元素是否设置position属性,其相对的对象均为html
$.position()如果父元素没有设置position属性,则相对的对象是html,如果有则为临近设置position属性的父元素;

4.事件坐标
原生js: clientX/clientY, layerX/layerY, movementX/movementY, offsetX/offsetYpageX/pageYscreenX/screenY,  x/y (常用的标注红色)

clientX/clientY:浏览器可视区域的水平/垂直的坐标(坐标原点是可视区域的左上角)
pageX/pageY:与clientX/clientY一样
screenX/screenY:相对屏幕的水平/垂直的坐标

jQuery:clientX/clientY,offsetX/offsetY, pageX/pageY, screenX/screenY


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值