jQuery 获得/控制 元素的大小
2017-05-07 16:17
183 查看
width([纯数字/带单位的字符串]) 返回宽度/设置宽度 (返回值是数字)
width(function(index,width){}) 采用匿名函数
height() 返回高度,用法与width()相同
效果和css(“width”)类似,css() 返回字符串(px)
使用匿名函数控制宽度,index表示序号,width表示宽度
- innerWidth([value]) 宽度 包含内边距
- innerHeight([value]) 高度 包含内边距
- outerWidth([value]) 宽度 包含内边距和边框
- outerHeight([value]) 高度 包含内边距和边框
- outerWidth(true) 高度 包含内边距、边框、外边距
对于以下元素:
- offset() 获取某个元素相对于可视区域的相对位置 返回一个对象两个属性left-top
- position() 相对于父元素的位置 返回对象
对于以下元素:
- scrollTop ([value]) 返回/设置滚动条的高
- scrollLeft(value) 返回/设置滚动条的宽
width(function(index,width){}) 采用匿名函数
height() 返回高度,用法与width()相同
效果和css(“width”)类似,css() 返回字符串(px)
alert("width:"+$("#div").width()); // 获得元素的宽度 $("#div").width('400px'); // 也可以设置宽度 400 / '400px' alert($("#div").css("width")); // 返回带单位的字符串 400px
使用匿名函数控制宽度,index表示序号,width表示宽度
$("div").width(function(index,width){ if(index % 2 == 0 ) return width+100; return width-100 ; });
- innerWidth([value]) 宽度 包含内边距
- innerHeight([value]) 高度 包含内边距
- outerWidth([value]) 宽度 包含内边距和边框
- outerHeight([value]) 高度 包含内边距和边框
- outerWidth(true) 高度 包含内边距、边框、外边距
对于以下元素:
<body> <div style="width:300px;height:300px;background-color:skyblue; padding:10px;border:solid 10px green; margin:10px;" id="div"></div> </body>
alert("innerWidth:"+$("#div").innerWidth()); // 包含内边距 320 alert("outerWidth():"+$("#div").outerWidth()); // 包含内边距和边框 340 alert("outerWidth(true):"+$("#div").outerWidth(true)); // 包含内边距边框和外边距 360
- offset() 获取某个元素相对于可视区域的相对位置 返回一个对象两个属性left-top
- position() 相对于父元素的位置 返回对象
对于以下元素:
<body> <div style="position:absolute; left:100px ; top:100px;"> <div style="width:300px;height:300px;background-color:skyblue; padding:10px;border:solid 10px green; margin:10px;" id="div"></div> </div> </body>
alert($("#div").offset().left); //110 在屏幕中的位置加上外边距的效果 alert($("#div").offset().top); //110 alert($("#div").position().left); // 0 alert($("#div").position().top); // 0
- scrollTop ([value]) 返回/设置滚动条的高
- scrollLeft(value) 返回/设置滚动条的宽
alert($(document).scrollTop());
相关文章推荐
- js与jquery获得页面大小、滚动条位置、元素位置
- js与jquery获得页面大小、滚动条位置、元素位置
- js与jquery获得页面大小、滚动条位置、元素位置
- js与jquery获得页面大小、滚动条位置、元素位置
- js与jquery获得页面大小、滚动条位置、元素位置
- js与jquery获得页面大小、滚动条位置、元素位置
- Jquery获得相同id的元素
- 使用JQuery控制iframe自适应窗口大小
- 如何获得jquery this 的特定子元素
- JQuery获取元素文档大小、偏移和位置和滚动条位置的方法集合
- jQuery获得页面元素的绝对/相对位置
- Jquery获得相同id的元素
- JQuery获取元素文档大小、偏移和位置和滚动条位置的方法集合
- JQuery获取元素文档大小、偏移和位置和滚动条位置的方法集合
- Jquery提供了获得和设置元素的value的方法。如input, textarea等元素的value值。
- [分享] jQuery 版“元素拖拽改变大小”原型
- jquery 控制radio元素
- 用C#调用Windows API和其它进程通信及C# 获得另外一程序 句柄 后控制该程序 控制其透明 窗体大小
- 用jQuery在IFRAME控制父窗口元素
- jQuery获得页面元素的绝对/相对位置