css.js 3.1 KB

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