UNPKG

3.24 kBJavaScriptView Raw
1"use strict";
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6exports.get = get;
7exports.getClientSize = getClientSize;
8exports.getDocSize = getDocSize;
9exports.getOffset = getOffset;
10exports.getOuterHeight = getOuterHeight;
11exports.getOuterWidth = getOuterWidth;
12exports.getScroll = getScroll;
13exports.set = set;
14/* eslint-disable no-nested-ternary */
15var PIXEL_PATTERN = /margin|padding|width|height|max|min|offset/;
16var removePixel = {
17 left: true,
18 top: true
19};
20var floatMap = {
21 cssFloat: 1,
22 styleFloat: 1,
23 float: 1
24};
25function getComputedStyle(node) {
26 return node.nodeType === 1 ? node.ownerDocument.defaultView.getComputedStyle(node, null) : {};
27}
28function getStyleValue(node, type, value) {
29 type = type.toLowerCase();
30 if (value === 'auto') {
31 if (type === 'height') {
32 return node.offsetHeight;
33 }
34 if (type === 'width') {
35 return node.offsetWidth;
36 }
37 }
38 if (!(type in removePixel)) {
39 removePixel[type] = PIXEL_PATTERN.test(type);
40 }
41 return removePixel[type] ? parseFloat(value) || 0 : value;
42}
43function get(node, name) {
44 var length = arguments.length;
45 var style = getComputedStyle(node);
46 name = floatMap[name] ? 'cssFloat' in node.style ? 'cssFloat' : 'styleFloat' : name;
47 return length === 1 ? style : getStyleValue(node, name, style[name] || node.style[name]);
48}
49function set(node, name, value) {
50 var length = arguments.length;
51 name = floatMap[name] ? 'cssFloat' in node.style ? 'cssFloat' : 'styleFloat' : name;
52 if (length === 3) {
53 if (typeof value === 'number' && PIXEL_PATTERN.test(name)) {
54 value = "".concat(value, "px");
55 }
56 node.style[name] = value; // Number
57 return value;
58 }
59 for (var x in name) {
60 if (name.hasOwnProperty(x)) {
61 set(node, x, name[x]);
62 }
63 }
64 return getComputedStyle(node);
65}
66function getOuterWidth(el) {
67 if (el === document.body) {
68 return document.documentElement.clientWidth;
69 }
70 return el.offsetWidth;
71}
72function getOuterHeight(el) {
73 if (el === document.body) {
74 return window.innerHeight || document.documentElement.clientHeight;
75 }
76 return el.offsetHeight;
77}
78function getDocSize() {
79 var width = Math.max(document.documentElement.scrollWidth, document.body.scrollWidth);
80 var height = Math.max(document.documentElement.scrollHeight, document.body.scrollHeight);
81 return {
82 width: width,
83 height: height
84 };
85}
86function getClientSize() {
87 var width = document.documentElement.clientWidth;
88 var height = window.innerHeight || document.documentElement.clientHeight;
89 return {
90 width: width,
91 height: height
92 };
93}
94function getScroll() {
95 return {
96 scrollLeft: Math.max(document.documentElement.scrollLeft, document.body.scrollLeft),
97 scrollTop: Math.max(document.documentElement.scrollTop, document.body.scrollTop)
98 };
99}
100function getOffset(node) {
101 var box = node.getBoundingClientRect();
102 var docElem = document.documentElement;
103
104 // < ie8 不支持 win.pageXOffset, 则使用 docElem.scrollLeft
105 return {
106 left: box.left + (window.pageXOffset || docElem.scrollLeft) - (docElem.clientLeft || document.body.clientLeft || 0),
107 top: box.top + (window.pageYOffset || docElem.scrollTop) - (docElem.clientTop || document.body.clientTop || 0)
108 };
109}
\No newline at end of file