NOTE: There's newer library that does the same as this one and more. Try to use The DOM Box instead.
Set of utilities for working with dimensions, positions and relations between DOM elements, viewport and the document itself.
It does not actually change properties (position, size, etc.) of any element. It just calculates the elements' bounding box, represents it as an abstract object and lets you do various operations with it.
Returns abstract Box representation of target. Target can be:
- element reference
- CSS selector - If it matches more than one element, it will return bounding box around all of these elements.
- DomBox.Box - It will return unchanged object.
Returns horizontal and vertical distance between the two boxes. If the boxes overlap, the distance is 0
.
Example:
DomBox.getDistance(box1, box2); // -> {horizontal: 100, vertical: 200}
Returns true
if the two boxes overlap.
Returns absolute distance between pivots of the two boxes.
Returns angle between pivots of the two boxes, in degrees.
Abstract representation of a box around single element or bounding box around collection of elements. It has these properties:
width
height
left
top
right
bottom
view_left
view_top
view_right
view_bottom
These properties are not updated automatically. If the position or size of the element(s) has changed, you should call the update()
method manually.
Adds a padding to the box and updates it. The padding will be added automatically on each update.
Returns an object the top
and left
position of box's pivot point.
Adjust all box's properties as if it was moved to provided coordinates.
Adjust all box's properties as if it was moved by provided distances.
Adjust all box's properties as if it was resized to provided size.
Adjust all box's properties as if it was resized by provided size.
Adjusts box's left position and recalculates other properties accordingly
Adjusts box's right position and recalculates other properties accordingly
Adjusts box's top position and recalculates other properties accordingly
Adjusts box's bottom position and recalculates other properties accordingly
Returns string representation of box's properties. This is handy for debuging, since the object itself contains cyclic references and is impossible to flatten using JSON.stringify()
.
Methods to get current size of the document in pixels.
NOTE: If the content of the document is smaller than viewport (e.g. empty document), it returns viewport size instead.
Returns current width of the document.
Returns current height of the document.
Returns object with current width
and height
of the document.
Returns current width of viewport in pixels.
Returns current height of viewport in pixels.
Returns object with current width
and height
of viewport.
Returns current horizontal scroll position of viewport in pixels.
Returns current vertical scroll position of viewport in pixels.
Returns object with current left
and top
scroll position of viewport.
Returns object with all of the properties of viewport:
- width
- height
- left
- top
- right
- bottom
Adjusts box's properties so that it is contained within viewport. If the box can not fit, its top-left corner is matched with viewports top-left corner. If the box is already within viewport, its properties do not change.
Returns true
if the box is completely contained within viewport. Returns false
if box is completely or partially outside viewport.
Returns true
if box is completely or partially contained within viewport. Returns false
if box is completely outside viewport.
Returns true
if the viewport can fit the box completely.
Returns true
if both boxes can fit the viewport without any overlaps. It does not matter if the boxes could fit horizontally, vertically or both.
This considers only dimensions of the boxes, not their respective positions.
Returns true
if box2 can fit into the viewport without overlaping with box1.
This considers current dimensions and position of box1 within the viewport.
Adjusts properties of box2 so that it fits into the viewport without coliding with box1.
Returns string representation of box's properties. This is handy for debuging, since the object itself contains cyclic references and is impossible to flatten using JSON.stringify()
.
If you found any bugs, if you have feature requests or any questions, please, either file an issue at GitHub or send me an e-mail at riki@fczbkk.com.
DOM Box is published under the UNLICENSE license. Feel free to use it in any way.