css.js 2.9 KB

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