-
Notifications
You must be signed in to change notification settings - Fork 82
/
Copy pathcss-box.ts
69 lines (46 loc) · 1.81 KB
/
css-box.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
/// <reference path="../requirements.ts" />
interface ModuleHub { (file: 'core:css-box'): CSSBox.CSSBoxModule; }
module CSSBox {
/**
* Provides an easier way to deal with the different css style objects of an element.
*/
export interface CSSBoxModule extends Object {
/**
* Computes the local distance between the various boxes of an element (only works properly if the element has only one box).
*
* @param element The element for which the box is computed.
* @param boxType of one: "content-box", "padding-box", "border-box" and "margin-box".
* @return {top/left/width/height} for 'content/padding/border/margin-box' (relative to the border box top-left corner).
*/
getBox(
element: HTMLElement,
boxType: string
)
: Box
}
export interface Box {
top: number
left: number
width: number
height: number
}
}
/////////////////////////////////////////////////////////////////////////
//// EXAMPLES ///////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////
/** How do you get the border-box of an element? */
function get_border_box_of_element() {
var cssBox = require('core:css-box');
var box = cssBox.getBox(document.body, 'border-box');
return { top: box.top, left: box.left, right: box.left+box.width, bottom: box.top+box.height };
}
/** How do you get the content-box of an element? */
function get_content_box_of_element() {
var cssBox = require('core:css-box');
var box = cssBox.getBox(document.body, 'content-box');
return { top: box.top, left: box.left, right: box.left+box.width, bottom: box.top+box.height };
}
/** What kind of boxes can you get for an element? */
function get_boxes_kinds_of_element() {
return ["content-box", "padding-box", "border-box", "margin-box"];
}