深入学习jQuery元素尺寸和位置操作
前面的话
对于javascript来说,元素尺寸有scroll、offset、client三大属性,以及一个强大的getBoundingClientRect()方法。而jQuery有着对应的更为简便的方法。本文将详细介绍jQuery中的元素尺寸和位置操作
尺寸设置
在CSS中,宽高有三种表示,分别是content-box、padding-box和border-box里的三种宽高。可以分别对应于jQuery中height()/width()、innerHeight()/innerWidth()和outerHeight()/outerWidth()
[注意]对于原生javascript来说,offsetWidth类属性无法获取隐藏元的值,而jQuery这三个获取宽高的方法可以
【1】设置宽高
height()/width()
当height()/width()方法中不包含任何参数时,可以获取设置宽高值
css(width)和width()之间的区别在于width()返回一个没有单位的数值(如400),而css(width)返回带有完整单位的字符串(400px)。当然,高度也类似
<div id="test" style="height:30px;width:10em">测试内容</div>
<button id="btn">获取宽高</button>
<div id="result"></div>
<script src="http://cdn.bootcss.com/jquery/1.12.4/jquery.min.js"></script>
<script>
$('#btn').click(function(){
$('#result').html('css()获取的高度:' + $('#test').css('height') + ';css()获取的宽度:' + $('#test').css('width') + ';height()获取的高度:' + $('#test').height() + ';width()获取的宽度:' + $('#test').width());
})
</script>
这个方法同样能计算出window和document的宽高
$(window).width();
$(document).width();
$(window).height();
$(document).height();
<div id="test">测试内容</div>
<button id="btn">获取宽高</button>
<div id="result"></div>
<script src="http://cdn.bootcss.com/jquery/1.12.4/jquery.min.js"></script>
<script>
$('#btn').click(function(){
$('#result').html('内容宽:' + $(this).width() +';内容高:' + $(this).height() + ';页面宽:' + $(document).width() +';页面高:' + $(document).height() + ';window宽:' + $(window).width() +';window高:' + $(window).height() )
})
</script>
height(value)/width(value)
当height()/width()方法中包含一个参数时,可以设置宽高值。这个参数可以是一个正整数代表的像素数,或是整数和一个可选的附加单位(默认是px)
<div id="test" style="background-color:pink">测试内容</div>
<button id="btn">设置宽高</button>
<script src="http://cdn.bootcss.com/jquery/1.12.4/jquery.min.js"></script>
<script>
$('#btn').click(function(){
$('#test').height(30).width(100);
})
</script>
height(function(index,currentHeight))/width(function(index,currentWidth))
height()/width()方法也可以以一个函数作为参数,该函数接受两个参数,index参数表示元素在集合中的位置,currentHeight/currentWidth参数表示原来的宽高。在这个函数中,this指向元素集合中的当前元素,最终返回设置的宽高
<button id="btn">设置宽高</button>
<div>1</div>
<div>2</div>
<div>3</div>
<div>4</div>
<div>5</div>
<script src="http://cdn.bootcss.com/jquery/1.12.4/jquery.min.js"></script>
<script>
$("#btn").click(function(){
$('div').height(30).css('background-color','orange').width(function(index,currentWidth){
return currentWidth*(index+1)/10
})
})
</script>
【2】客户区宽高
客户区宽高指设置宽高加上padding值。在javascript中,客户区宽高用clientWidth/clientHeight表示。而在jQuery中,用innerHeight()和innerWidth()方法表示
innerHeight()/innerWidth()
innerHeight()/innerWidth()方法不适用于window和document对象,可以使用height()/width()代替
<div id="test" style="width:100px;height:30px;padding:2px;">测试内容</div>
<button id="btn">设置宽高</button>
<div id="result"></div>
<script src="http://cdn.bootcss.com/jquery/1.12.4/jquery.min.js"></script>
<script>
$('#btn').click(function(){
$('#result').html('设置高:' + $('#test').height() + ';设置宽:' + $('#test').width() + ';客户区高:' + $('#test').innerHeight() + ';客户区宽:' + $('#test').innerWidth())
})
</script>
【3】全宽高
全宽高指设置宽高再加上padding、border、margin(可选)
如果获取border-box的宽高,javascript使用offsetwidth/offsetHeight获取。而在jQuery中,有着功能更强大的outerWidth()/outerHeight()方法
outerWidth()/outerHeight()
outerWidth()/outerHeight()方法用来获取元素集合中第一个元素的当前计算宽高值,包括padding,border和选择性的margin。返回一个整数(不包含px)表示的值
当参数为false或无参数时,表示不包括margin,否则包括margin
[注意]如果在一个空集合上调用该方法,则会返回null
outerWidth()/outerHeight()方法不适用于window和document对象,可以使用height()/width()代替
<div id="test" style="width:100px;height:30px;padding:2px;border:1px solid black;margin:10px">测试内容</div>
<button id="btn">设置宽高</button>
<div id="result"></div>
<script src="http://cdn.bootcss.com/jquery/1.12.4/jquery.min.js"></script>
<script>
$('#btn').click(function(){
$('#result').html('border-box的宽度' + $('#test').outerWidth() + ';border-box的高度' + $('#test').outerHeight() + ';margin-box的宽度' + $('#test').outerWidth(true) + ';margin-box的高度' + $('#test').outerHeight(true))
})
</script>
位置设置
【1】offsetParent()
jQuery通过offsetParent()找到元素的定位父级
jQuery与javascript有些不同,规则如下
1、当元素本身不是fixed定位,且父级元素存在经过定位的元素,offsetParent()的结果为离自身元素最近的经过定位的父级元素
2、当元素本身具有fixed定位,或父级元素都未经过定位,则offsetParent()的结果为html
3、body元素的offsetParent()的结果也是html
<script src="http://cdn.bootcss.com/jquery/1.12.4/jquery.min.js"></script>
<div id="box" style="position:relative;">
<div id="test1" style="position:absolute;"></div>
<div id="test2" style="position:fixed;"></div>
</div>
<script>
console.log($('#test1').offsetParent());//'<div id="box>'
console.log($('#test2').offsetParent());//'<html>'
console.log($('#box').offsetParent());//'<html>'
console.log($('body').offsetParent());//'<html>'
</script>
【2】position()
position()方法不接受参数,用来获取匹配元素中第一个元素的相对于定位父级的坐标
position()返回一个包含top和left属性的对象,相当于javascript中的offsetTop和offsetLeft
<script src="http://cdn.bootcss.com/jquery/1.12.4/jquery.min.js"></script>
<div id="box" style="position:relative;">
<div id="test1" style="position:absolute;"></div>
<div id="test2" style="position:fixed;"></div>
</div>
<script>
console.log($('#test1').position().top,$('#test1').position().left);//0 0
console.log($('#test2').position().top,$('#test2').position().left);//8 8
</script>
【3】offset()
offset()
当offset()方法没有参数时,在匹配的元素集合中,获取的第一个元素的当前坐标,坐标相对于文档
<script src="http://cdn.bootcss.com/jquery/1.12.4/jquery.min.js"></script>
<div id="box" style="position:relative;">
<div id="test1" style="position:absolute;"></div>
<div id="test2" style="position:fixed;"></div>
</div>
<script>
console.log($('#test1').offset().top,$('#test1').offset().left);//8 8
console.log($('#test2').offset().top,$('#test2').offset().left);//8 8
</script>
offset(coordinates)
offset()方法可以接受一个包含top和left属性的对象,用整数指明元素的新顶部和左边坐标
<script src="http://cdn.bootcss.com/jquery/1.12.4/jquery.min.js"></script>
<button id="btn">改变按钮位置</button>
<script>
$('#btn').click(function(){
$(this).offset({top:20,left:20})
})
</script>
offset(function(index,coords))
offset()方法可以接受一个函数作为参数。在函数中,元素在匹配的元素集合中的索引位置作为第一个参数,当前坐标作为第二个参数。这个函数返回一个包含top和left属性的对象
<script src="http://cdn.bootcss.com/jquery/1.12.4/jquery.min.js"></script>
<button id="btn">改变按钮位置</button>
<script>
$('#btn').click(function(){
$(this).offset(function(index,coords){
return {left: coords.left + 10, top:coords.top}
})
})
</script>
【4】scrollTop()/scrollLeft()
scrollTop()/scrollLeft()
scrollTop()/scrollLeft()方法不带参数时,用来获取匹配元素集合中第一个元素的当前水平或垂直滚动条位置
<script src="http://cdn.bootcss.com/jquery/1.12.4/jquery.min.js"></script>
<div id="test" style="width: 100px;height: 100px;padding: 10px;margin: 10px;border: 1px solid black;overflow:scroll;font-size:20px;line-height:200px;">
内容</div>
<button id='btn'>点击</button>
<div id="result"></div>
<script>
$('#btn').click(function(){
$('#result').html('scrollTop:' + $('#test').scrollTop() + ';scrollLeft:' + $('#test').scrollLeft())
})
</script>
scrollLeft(value)/scrollTop(value)
scrollTop()/scrollLeft()方法可以接受一个用来设置滚动条水平或垂直位置的正整数
<script src="http://cdn.bootcss.com/jquery/1.12.4/jquery.min.js"></script>
<div id="test" style="width: 100px;height: 100px;padding: 10px;margin: 10px;border: 1px solid black;overflow:scroll;font-size:20px;line-height:200px;">
内容</div>
<button id='btn1'>向下滚动</button>
<button id='btn2'>向上滚动</button>
<script>
$('#btn1').click(function(){
$('#test').scrollTop($('#test').scrollTop() + 10);
})
$('#btn2').click(function(){
$('#test').scrollTop($('#test').scrollTop() - 10);
})
</script>
最后
关于元素的位置和尺寸操作,jQuery把javascript中的scroll、offset、client和getBoundingClientRect()重新整合。对于常用的宽高尺寸设置了width/height、innerWidth/innerHeight、outerWidth/outerHeight这6个方法;而对于位置操作,则设置了position()、offset()/offsetParent()、scrollLeft()/scrollTop()这5个方法
欢迎交流
深入学习jQuery元素尺寸和位置操作的更多相关文章
- jquery css事件编程 位置 操作
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- jacascript 判断元素尺寸和位置
前言:这是笔者学习之后自己的理解与整理.如果有错误或者疑问的地方,请大家指正,我会持续更新! getBoundingClientRect() 判断一个元素的尺寸和位置是简单的方法就是使用 obj.ge ...
- 第一百一十七节,JavaScript,DOM元素尺寸和位置
学习要点: 1.获取元素CSS大小 2.获取元素实际大小 3.获取元素周边大小 本章,我们主要讨论一下页面中的某一个元素它的各种大小和各种位置的计算方式,以便更好的理解. 一.获取元素CSS大小 1. ...
- 深入学习jQuery元素过滤
× 目录 [1]索引过滤 [2]内容过滤 前面的话 过滤是jQuery扩展的一个重要的内容.jQuery选择器中的一个重要部分就是过滤选择器.除了过滤选择器,还有专门的元素过滤的方法.本文将详细介绍j ...
- JavaScript--DOM元素尺寸和位置(22)
一 获取元素的CSS大小 1.通过style内联获取元素的大小 var box = document.getElementById('box'); // 获得元素; box.style.width; ...
- JavaScript(第二十一天)【DOM元素尺寸和位置】
学习要点: 1.获取元素CSS大小 2.获取元素实际大小 3.获取元素周边大小 本章,我们主要讨论一下页面中的某一个元素它的各种大小和各种位置的计算方式,以便更好的理解. 一.获取元素CSS大小 ...
- DOM元素尺寸和位置
一.获取元素 CSS大小 1.通过style 内联获取元素的大小 var box = document.getElementById('box'); //获取元素 box.style.width; / ...
- JavaScript获取元素尺寸和大小操作总结
一.获取元素的行内样式 复制代码 代码如下: var obj = document.getElementById("test"); alert(obj.height + " ...
- DOM元素尺寸和位置(clientwidth ,scrollwidth , offsetwidth.......)
[1] slientWidth 和 sclientHeight slientWidth:获取的是可视宽度 slientHeight:获取的是可视高度 <html> <head> ...
随机推荐
- ZooKeeper个人笔记客户端watcher和AsycCallback回调
每一个Watcher具有如下属性: 1.KeeperState 2.EventType 3.path 4.process(WatchedEvent evnet)回掉方法 Watcher干嘛的?用户监听 ...
- 【BZOJ】4245: [ONTAK2015]OR-XOR
题意 给定一个长度为\(n(1 \le n \le 500000)\)的序列\(a_i(0 \le a_i \le 10^{18})\),将它划分为\(m(1 \le m \le n)\)段连续的区间 ...
- 客户端连接注册Ejabberd新用户
今天需要使用客户端注册新用户,结果发现注册失败,在管理后台添加新用户成功.编译安装ejabberd就没有管了,经过翻论坛的到解决方法 在ejabberd.yml中. access: trusted_n ...
- substring()
OPENERURL.substring(OPENERURL.indexOf('/sear'));//从/sear开始截取(包括/sear): OPENERURL.substring(OPENERURL ...
- “我爱背单词”beta版发布与使用说明
我爱背单词BETA版本发布 第二轮迭代终于画上圆满句号,我们的“我爱背单词”beta版本已经发布. Beta版本说明 项目名称 我爱背单词 版本 Beta版 团队名称 北京航空航天大学计算机学院 拒 ...
- phone 调试三种工具
1. Phonegap桌面开发工具 Phonegap Desktop-App与 手机客户端调试工具PhoneGap Developer App 此工具方便.快捷.自动.可以在真机中查看 无法设置断点. ...
- 不同类型的指针+1之后增加的大小不同(a,&a的地址是一样的,但意思不一样)
main() { ]={,,,,}; ); printf(),*(ptr-)); } *(a+1)就是a[1],*(ptr-1)就是a[4], 执行结果是2, 5.&a+1不是首地址+1,系统 ...
- 编译安装PHP的参数 --with-mysql --with-mysqli --with-apxs2默认路径
编译安装PHP时指定如下几个参数说明: --with-apxs2=/usr/local/apache/bin/apxs //整合apache,apxs功能是使用mod_so中的LoadModule指令 ...
- RPC 的概念模型与实现解析
今天分布式应用.云计算.微服务大行其道,作为其技术基石之一的 RPC 你了解多少?一篇 RPC 的技术总结文章,数了下 5k+ 字,略长,可能也不适合休闲的碎片化时间阅读,可以先收藏抽空再细读:) 全 ...
- Step by step Dynamics CRM 2013安装
原创地址:http://www.cnblogs.com/jfzhu/p/4008391.html 转载请注明出处 SQL Server可以与CRM装在同一台计算机上,也可安装在不同的计算机上.演示 ...