博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
difference of top and left between Javascript and Jquery
阅读量:2232 次
发布时间:2019-05-09

本文共 1344 字,大约阅读时间需要 4 分钟。

1, top and left relative to the document

jquery 
.offset()

Get the current coordinates of the first element, or set the coordinates of every element, in the set of matched elements, relative to the document.

var top = $('#elementID').offset().top;var left = $('#elementID').offset().left;
native javascript
getBoundingClientRect
, top-left relative to the top-left of the viewport.
var viewportOffset = el.getBoundingClientRect();// these are relative to the viewportvar top = viewportOffset.top;var left = viewportOffset.left;
2, top and left relative to the parent
jquery
.position()
Get the current coordinates of the first element in the set of matched elements, relative to the offset parent.

var topToParent = $('#elementID').position().top;var leftToParent = $('#elementID').position().left;
native javascript

.offsetTop, .offsetLeft

The HTMLElement.offsetTop read-only property returns the distance of the current element relative to the top of the offsetParent node.

var topToParent = element.offsetTop;var leftToParent = element.offsetLeft;
3,  Border 

native javascript
element.clientTop;
The width of the top border of an element in pixels. It does not include the top margin or padding. clientTop is read-only.
see:  https://developer.mozilla.org/en-US/docs/Web/API/Element/clientTop

转载于:https://www.cnblogs.com/ljbguanli/p/6900982.html

你可能感兴趣的文章
微信小程序-调用-腾讯视频-解决方案
查看>>
phpStudy安装yaf扩展
查看>>
密码 加密 加盐 常用操作记录
查看>>
TP 分页后,调用指定页。
查看>>
Oracle数据库中的(+)连接
查看>>
java-oracle中几十个实用的PL/SQL
查看>>
PLSQL常用方法汇总
查看>>
几个基本的 Sql Plus 命令 和 例子
查看>>
PLSQL单行函数和组函数详解
查看>>
Oracle PL/SQL语言初级教程之异常处理
查看>>
Oracle PL/SQL语言初级教程之游标
查看>>
Oracle PL/SQL语言初级教程之操作和控制语言
查看>>
Oracle PL/SQL语言初级教程之过程和函数
查看>>
Oracle PL/SQL语言初级教程之表和视图
查看>>
Oracle PL/SQL语言初级教程之完整性约束
查看>>
PL/SQL学习笔记
查看>>
如何分析SQL语句
查看>>
结构化查询语言(SQL)原理
查看>>
SQL教程之嵌套SELECT语句
查看>>
日本語の記号の読み方
查看>>