1 jQuery 尺寸
- 以上参数为空,则是获取相应值,返回的是数字型
- 如果参数为数字,则是修改相应值
- 参数可以不必写单位
<style>
div {
width: 200px;
height: 200px;
background-color: pink;
padding: 10px;
border: 15px solid red;
margin: 20px;
}
</style>
<body>
<div></div>
<script>
$(function () {
// 1. width() / height() 获取设置元素width和height大小
console.log($("div").width());// 200 返回的数值不含单位
$("div").width("300px");//可以不加单位直接写数字
// 2. innerWidth() / innerHeight() 获取设置元素width和height + padding大小
console.log($("div").innerWidth());// 320
// 3. outerWidth() / outerHeight 获取设置元素width和height + padding + border大小
console.log($("div").outerWidth());// 350
//$("div").width("300px");
// 4. outerWidth(true) / outerHeight(true) 获取设置元素width和height + padding + border + margin大小
console.log($("div").outerWidth(true));// 390
// $("div").width("300px");
})
</script>
</body>
</html>
2 jQuery 位置
位置主要有三个:offset()、position()、scrollTop()/scrollLeft()
1. offset()设置或获取元素的偏移
- offset() 方法设置或返回被选元素相对于文档的偏移坐标,跟父级没有关系。
- 该方法有2个属性left、top。 offset().top用于获取距离文档顶部的距离,offset().left用于获取距离文档左侧的距离。
- 可以设置元素的偏移:offset({top:10,left:30});
2. position() 获取元素的偏移
- position()方法用于返回被选元素相对于带有定位的父级偏移坐标,如果父级都没有定位,则以文档为准。
- 这个方法只能获取不能设置偏移
3. scrollTop()/scrollLeft() 设置或获取元素被卷去的头部和左侧
- scrollTop() 方法设置或获取被选元素被卷去的头部
- scrollLeft()方法设置或获取被选元素被卷去的左侧