css.js 3.2 KB

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