首页 > 其他分享 >jquery获取设置元素宽高位置height()、width()、offset()、position()、scrollTop()、scrollLeft()

jquery获取设置元素宽高位置height()、width()、offset()、position()、scrollTop()、scrollLeft()

时间:2023-03-10 11:36:57浏览次数:37  
标签:jquery position 元素 宽高 width offset cr



全栈工程师开发手册 (作者:栾鹏)

​​jquery系列教程2-style样式操作全解 ​​

jquery获取设置元素宽高位置

jquery的通过height()、width()、offset()、position()、scrollTop()、scrollLeft()等函数获取和设置元素宽高,具体功能如代码中注释。

代码如下:

var width = $cr.width();                                        //读取宽高相关样式:height、width、innerHeight、innerWidth、outerHeight、outerWidth
$cr.width(800); //设置宽高相关样式:height、width、innerHeight、innerWidth、outerHeight、outerWidth
var offset = $cr.offset(); //偏移类。获取元素在当前视窗中的相对偏移,只对可见元素有效,包含offset.left和offset.top两个属性
var position = $cr.position(); //位置类。获取元素相对于最近的一个position属性设置为relative或absolute的祖父节点的相对偏移,包含position.left和position.top两个属性
$cr.scrollTop(); //获取元素的滚动条距顶端的距离
$cr.scrollLeft(300); //获取元素的滚动条距左端的距离,也可以通过参数设置滚动左边的距离


标签:jquery,position,元素,宽高,width,offset,cr
From: https://blog.51cto.com/u_15858929/6112847

相关文章