You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

102 lines
3.5 KiB

  1. (function(win, lib){
  2. var doc = win.document;
  3. var docEl = doc.documentElement;
  4. var gridEl = doc.querySelector('meta[name="grid"]');
  5. var styleEl;
  6. var flexible = lib.flexible || (lib.flexible = {});
  7. function makeGrid(params) {
  8. var designWidth = parseFloat(params.designWidth);
  9. var designUnit = parseFloat(params.designUnit);
  10. var columnCount = parseFloat(params.columnCount);
  11. var columnXUnit = parseFloat(params.columnXUnit);
  12. var gutterXUnit = parseFloat(params.gutterXUnit);
  13. var edgeXUnit = parseFloat(params.edgeXUnit);
  14. var className = params.className || 'grid';
  15. if (!(params.designWidth && params.designUnit && params.columnCount && params.columnXUnit && params.gutterXUnit && params.edgeXUnit)) {
  16. throw new Error('参数错误');
  17. }
  18. lib.flexible.gridParams = params;
  19. var ratio = designUnit / designWidth * 10;
  20. var columnWidth = columnXUnit * ratio;
  21. var gutterWidth = gutterXUnit * ratio;
  22. var edgeWidth = edgeXUnit * ratio;
  23. var cssText = [
  24. '.' + className + ' {',
  25. 'box-sizing:content-box;',
  26. 'padding-left: ' + edgeWidth + 'rem;',
  27. 'padding-right: ' + edgeWidth + 'rem;',
  28. 'margin-left: -' + gutterWidth + 'rem;',
  29. '}',
  30. '.' + className + ':before,',
  31. '.' + className + ':after{',
  32. 'content: " ";',
  33. 'display: table;',
  34. '}',
  35. '.' + className + ':after {',
  36. 'clear: both;',
  37. '}',
  38. '.' + className + ' [class^="col-"] {',
  39. 'margin-left: ' + gutterWidth + 'rem;',
  40. 'float: left;',
  41. '}'
  42. ];
  43. for (var i = 1; i <= columnCount; i++) {
  44. var width = columnWidth * i + gutterWidth * (i - 1);
  45. cssText.push('.' + className + ' .col-' + i + ' {width: ' + width + 'rem;}');
  46. }
  47. if (styleEl && styleEl.parentNode) {
  48. styleEl.parentNode.removeChild(styleEl);
  49. }
  50. styleEl = doc.createElement('style');
  51. styleEl.innerHTML = cssText.join('');
  52. var el = doc.querySelector('head') || docEl.firstElementChild || docEl;
  53. el.appendChild(styleEl);
  54. }
  55. var gridMode = {
  56. '750-12': {designWidth:750,designUnit:6,columnCount:12,columnXUnit:7,gutterXUnit:3,edgeXUnit:4},
  57. '750-6': {designWidth:750,designUnit:6,columnCount:6,columnXUnit:17,gutterXUnit:3,edgeXUnit:4},
  58. '640-12': {designWidth:640,designUnit:4,columnCount:12,columnXUnit:11,gutterXUnit:2,edgeXUnit:3},
  59. '640-6': {designWidth:640,designUnit:4,columnCount:6,columnXUnit:24,gutterXUnit:2,edgeXUnit:3}
  60. }
  61. function makeGridMode(modeName) {
  62. var mode = gridMode[modeName];
  63. if (mode) {
  64. makeGrid(mode);
  65. } else {
  66. throw new Error('不支持这个预设模式');
  67. }
  68. }
  69. if (gridEl) {
  70. var content = gridEl.getAttribute('content');
  71. if (content) {
  72. var reg = /([^=]+)=([\d\.\-]+)/g;
  73. var matched;
  74. var params = {};
  75. while (!!(matched = reg.exec(content))) {
  76. params[matched[1]] = matched[2];
  77. }
  78. if (params.modeName){
  79. makeGridMode(params.modeName);
  80. } else {
  81. makeGrid(params);
  82. }
  83. }
  84. }
  85. flexible.makeGrid = makeGrid;
  86. flexible.makeGridMode = makeGridMode;
  87. })(window, window['lib'] || (window['lib'] = {}));