前面的话

  对于javascript来说,元素尺寸有scrolloffsetclient三大属性,以及一个强大的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元素尺寸和位置操作的更多相关文章

  1. jquery css事件编程 位置 操作

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  2. jacascript 判断元素尺寸和位置

    前言:这是笔者学习之后自己的理解与整理.如果有错误或者疑问的地方,请大家指正,我会持续更新! getBoundingClientRect() 判断一个元素的尺寸和位置是简单的方法就是使用 obj.ge ...

  3. 第一百一十七节,JavaScript,DOM元素尺寸和位置

    学习要点: 1.获取元素CSS大小 2.获取元素实际大小 3.获取元素周边大小 本章,我们主要讨论一下页面中的某一个元素它的各种大小和各种位置的计算方式,以便更好的理解. 一.获取元素CSS大小 1. ...

  4. 深入学习jQuery元素过滤

    × 目录 [1]索引过滤 [2]内容过滤 前面的话 过滤是jQuery扩展的一个重要的内容.jQuery选择器中的一个重要部分就是过滤选择器.除了过滤选择器,还有专门的元素过滤的方法.本文将详细介绍j ...

  5. JavaScript--DOM元素尺寸和位置(22)

    一 获取元素的CSS大小 1.通过style内联获取元素的大小 var box = document.getElementById('box'); // 获得元素; box.style.width; ...

  6. JavaScript(第二十一天)【DOM元素尺寸和位置】

    学习要点: 1.获取元素CSS大小 2.获取元素实际大小 3.获取元素周边大小 本章,我们主要讨论一下页面中的某一个元素它的各种大小和各种位置的计算方式,以便更好的理解.   一.获取元素CSS大小 ...

  7. DOM元素尺寸和位置

    一.获取元素 CSS大小 1.通过style 内联获取元素的大小 var box = document.getElementById('box'); //获取元素 box.style.width; / ...

  8. JavaScript获取元素尺寸和大小操作总结

    一.获取元素的行内样式 复制代码 代码如下: var obj = document.getElementById("test"); alert(obj.height + " ...

  9. DOM元素尺寸和位置(clientwidth ,scrollwidth , offsetwidth.......)

    [1] slientWidth 和 sclientHeight slientWidth:获取的是可视宽度 slientHeight:获取的是可视高度 <html> <head> ...

随机推荐

  1. Curator leader 选举(一)

    要想使用Leader选举功能,需要添加recipes包,可以在maven中添加如下依赖: <dependency> <groupId>org.apache.curator< ...

  2. 在mvc里面有htmlhelper方法,在webform里面有什么?

    终于是找到原来在webform里面已经提供了htmlcontrol这样的控件,可以直接拿来用.以前一直在想mvc有htmlhelper,webform里面不能用,其实是webform里面已经有了. 例 ...

  3. AmazeUI 框架知识点-元素

    1.按钮  .am-btn 圆角按钮 .am-radius 椭圆形按钮 .am-round 按钮激活状态 .am-active 禁用状态 .am-disabled 2.按钮尺寸.am-btn-xl . ...

  4. Python之路【第六篇】python基础 之面向对象进阶

    一 isinstance(obj,cls)和issubclass(sub,super) isinstance(obj,cls)检查是否obj是否是类 cls 的对象  和  issubclass(su ...

  5. Shell下突破安全狗远程桌面守护

    首先在Shell下把安全狗的安装配置给下来 默认安装路径: C:\Program Files\SafedogServer\SafeDogGuardCenter  找到 ProGuardData.ini ...

  6. 推荐几个Android自定义的进度条(转载)

    CustomLoading ElasticDownload Circle-Progress-View lzyzsdCircleProgress SquareProgressBar materialis ...

  7. 浅谈C#中常见的委托<Func,Action,Predicate>(转)

    一提到委托,浮现在我们脑海中的大概是听的最多的就是类似C++的函数指针吧,呵呵,至少我的第一个反应是这样的. 关于委托的定义和使用,已经有诸多的人讲解过,并且讲解细致入微,尤其是张子阳的那一篇.我就不 ...

  8. JAVA演算法---約瑟夫問題

    1 public class Josephus { public static int[] arrayOfJosephus( int number, int per) { 3 int[] man = ...

  9. mac显示和隐藏文件

    封装了一下显示和隐藏的脚本,方便mac上的文件隐藏和显示 if [ `defaults read com.apple.finder AppleShowAllFiles` = "1" ...

  10. IT人生知识分享:概率与运气

    前言: 最近的人生多了些体验,也读了些许书,感觉还是有些知识是可以分享的. 今天难得周六,特意开电脑了,花几个小时写写,和大伙分享分享点知识. 以下内容,更多的需要读者思考,所以结论不会写太清晰,但一 ...