码迷,mamicode.com
首页 > 编程语言 > 详细

difference of top and left between Javascript and Jquery

时间:2017-05-24 22:32:09      阅读:137      评论:0      收藏:0      [点我收藏+]

标签:margin   offset   property   position   nbsp   mozilla   ati   set   pos   

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 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

标签:margin   offset   property   position   nbsp   mozilla   ati   set   pos   

原文地址:http://www.cnblogs.com/ljbguanli/p/6900982.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!