jQuery的位置插件Dimensions

概述

dimensions插件是一个获得元素尺寸的插件.


有以下函数

height

扩展了核心的 height 方法 enable it for the window and document objects as well as elements. When used on an element a value can be passed in to set the height. 对可见与不可见的元素都有效

Returns:像素值
Arguments:
value

A positive number representing the desired height. If just a number, 'px' will be added for you.

Chainable:
Yes, when a value is passed in.
Example:
$

(window

).height();

$

(document

).height();

$

("#myElement"

).height();

$

("#myElement"

).height(100

);

innerHeight

Gets the inner height (excludes the border and includes the padding) for the first matched element. This method works for both visible and hidden elements.

Returns:
A number in pixels
Chainable:
No
Example:
$

("#myElement"

).innerHeight();

innerWidth

Gets the inner width (excludes the border and includes the padding) for the first matched element. This method works for both visible and hidden elements.

Returns:
A number in pixels
Chainable:
No
Example:
$

("#myElement"

).innerHeight();

offset

Gets the position of an element relative to the viewport. For accurate calculations make sure to use pixel values for margins, borders and padding. This method only works with visible elements.

Returns:
An object with top and left properties that are numbers that represent the offset in pixels. If the scroll option is true, two more properties are added: scrollTop and scrollLeft.
Arguments:
options
Settings to configure the way the offset is calculated.
margin (Boolean)
Should the margin of the element be included in the calculations? True by default.
border (Boolean)
Should the border of the element be included in the calculations? False by default.
padding (Boolean)
Should the padding of the element be included in the calculations? False by default.
scroll (Boolean)
Should the scroll offsets of the parent elements be included in the calculations? True by default.
lite (Boolean)
When true it will bypass the browser normalizations like when dealing with borders and margins on parent elements. This provides a nice boost in performance but at the price of accuracy. False by default.
relativeTo (HTML Element)
This should be a parent of the element and should have position (like absolute or relative). It will retrieve the offset relative to this parent element. By default it is the body element.
returnObject
An object to store the return value in, so as not to break the chain. If passed in the chain will not be broken and the result will be assigned to this object.
Chainable:
Yes, when an object is passed as the second argument.
Example:
$

("#myElement"

).offset();

Results:
{ top: 100

, left: 100

, scrollTop: 10

, scrollLeft: 10

 }

Example:
$

("#myElement"

).offset({ scroll: false

 });

Results:
{ top: 100

, left: 100

 }

Example:
 
var

 offset

 = {}; 
$

("#myElement"

).offset({ scroll: false

 }, offset); 
                    

Results:
offset == { top: 100

, left: 100

 }

offsetParent

Returns a jQuery collection with the positioned parent of the first matched element. This is the first parent of the element that has position (as in relative or absolute). This method only works with visible elements.

Returns:
jQuery collection
Chainable:
Yes
Example:
$

("#myElement"

).offsetParent();

outerHeight

Gets the offsetHeight (includes the border and padding by default) for the first matched element. This method works for both visible and hidden elements. The margin can be included by passing an options map with margin set to true. Optional margin option added in 1.1

Returns:
A number in pixels
Arguments:
options
Settings to configure the way the outer height is calculated.
margin (Boolean)
Should the margin of the element be included in the calculations? False by default.
Chainable:
No
Example:
$

("#myElement"

).outerHeight();

Example:
$

("#myElement"

).outerHeight({ margin: true

 });

outerWidth

Gets the offsetWidth (includes the border and padding by default) for the first matched element. This method works for both visible and hidden elements. The margin can be included by passing an options map with margin set to true. Optional margin option added in 1.1

Returns:
A number in pixels
Arguments:
options
Settings to configure the way the outer height is calculated.
margin (Boolean)
Should the margin of the element be included in the calculations? False by default.
Chainable:
No
Example:
$

("#myElement"

).outerWidth();

Example:
$

("#myElement"

).outerWidth({ margin: true

 });

position

Gets the top and left position of an element relative to its offset parent. For accurate calculations make sure to use pixel values for margins, borders and padding. This method only works with visible elements.

Returns:
An object with top and left properties that are numbers representing the offset in pixels.
Chainable:
Yes, when an object is passed as the second argument.
Example:
$

("#myElement"

).position();

Results:
{ top: 10

, left: 10

 }

Example:
 
var

 position

 = {}; 
$

("#myElement"

).position(position); 
                    

Results:
position == { top: 10

, left: 10

 }

scrollLeft

Acts as both a getter and a setter. When no value is passed in, it gets the scroll left offset of the first matched element. When a value is passed in, the scroll left offset is set to that value on all matched elements. This method works for both visible and hidden elements.

Returns:
A number in pixels
Arguments:
value

A positive number representing the desired scroll left offset.

Chainable:
Yes, when a value is passed in.
Example:
$

("#myElement"

).scrollLeft(100

);

Example:
$

("#myElement"

).scrollLeft();

Results:
100



scrollTop

Acts as both a getter and a setter. When no value is passed in, it gets the scroll top offset of the first matched element. When a value is passed in, the scroll left offset is set to that value on all matched elements. This method works for both visible and hidden elements.

Returns:
A number in pixels
Arguments:
value

A positive number representing the desired scroll top offset.

Chainable:
Yes, when a value is passed in.
Example:
$

("#myElement"

).scrollTop(100

);

Example:
$

("#myElement"

).scrollTop();

Results:
100



width

This is an extension of the core width method to enable it for the window and document objects as well as elements. When used on an element a value can be passed in to set the width. This method works for both visible and hidden elements.

Returns:
A number in pixels
Arguments:
value

A positive number representing the desired width. If just a number, 'px' will be added for you.

Chainable:
Yes, when a value is passed in.
Example:
$

(window

).width();

$

(document

).width();

$

("#myElement"

).width();

$

("#myElement"

).width(100

);

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值