首页 > 代码库 > difference of top and left between Javascript and Jquery
difference of top and left between Javascript and Jquery
1, top and left relative to the document
.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 viewport var 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
difference of top and left between Javascript and Jquery
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。