概述
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:
-
A positive number representing the desired height. If just a number, 'px' will be added for you.
value
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 pixelsChainable:
NoExample:
$
("#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 pixelsChainable:
NoExample:
$
("#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:
- Settings to configure the way the offset is calculated.
-
- Should the margin of the element be included in the calculations? True by default.
- Should the border of the element be included in the calculations? False by default.
- Should the padding of the element be included in the calculations? False by default.
- Should the scroll offsets of the parent elements be included in the calculations? True by default.
- 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.
- 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.
margin
(Boolean)border
(Boolean)padding
(Boolean)scroll
(Boolean)lite
(Boolean)relativeTo
(HTML Element) - 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.
options
returnObject
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 collectionChainable:
YesExample:
$
("#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 pixelsArguments:
- Settings to configure the way the outer height is calculated.
-
- Should the margin of the element be included in the calculations? False by default.
margin
(Boolean)
options
Chainable:
NoExample:
$
("#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 pixelsArguments:
- Settings to configure the way the outer height is calculated.
-
- Should the margin of the element be included in the calculations? False by default.
margin
(Boolean)
options
Chainable:
NoExample:
$
("#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 pixelsArguments:
-
A positive number representing the desired scroll left offset.
value
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 pixelsArguments:
-
A positive number representing the desired scroll top offset.
value
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 pixelsArguments:
-
A positive number representing the desired width. If just a number, 'px' will be added for you.
value
Chainable:
Yes, when a value is passed in.Example:
$
(window
).width();
$
(document
).width();
$
("#myElement"
).width();
$
("#myElement"
).width(100
);