index.js 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. /**
  2. * 栅格相关处理
  3. * @author zhaoxianlie
  4. */
  5. let GridRuler = (function () {
  6. // 加载CSS
  7. let _loadCss = function () {
  8. if (!jQuery('#_fehelper_grid_css_')[0]) {
  9. let cssUrl = chrome.extension.getURL('ruler/index.css');
  10. jQuery('<link id="_fehelper_grid_css_" href="' + cssUrl + '" rel="stylesheet" type="text/css" />').appendTo('head');
  11. }
  12. };
  13. // 创建栅格
  14. let _createGrid = function () {
  15. let box = jQuery('#fe-helper-box');
  16. if (box[0]) { //已经有栅格,则移除
  17. box.remove();
  18. }
  19. //没有栅格,则创建
  20. let $win = jQuery(window);
  21. let $body = jQuery('body');
  22. jQuery('<div id="fe-helper-box"></div>').appendTo('body').css({
  23. position: 'static'
  24. });
  25. jQuery('<div id="fe-helper-grid"></div>').appendTo('#fe-helper-box').css({
  26. width: $body.width(),
  27. height: Math.max($win.height(), $body.height())
  28. }).mousemove(function (e) {
  29. let pos = {};
  30. try {
  31. pos = document.getElementsByTagName('body')[0].getBoundingClientRect();
  32. } catch (err) {
  33. pos = {left: 0, top: 0};
  34. }
  35. //虚线框
  36. jQuery('#fe-helper-g-pos').show().css({
  37. width: e.pageX - pos.left,
  38. height: e.pageY
  39. });
  40. let _t = Math.min(e.pageY, jQuery(window).height() + $body.scrollTop() - 40);
  41. let _l = Math.min(e.pageX, jQuery(window).width() + $body.scrollLeft() - 200) + 5 - pos.left;
  42. //坐标tooltip
  43. jQuery('#fe-helper-gp-info').show().css({
  44. top: _t,
  45. left: _l
  46. }).html('top = ' + e.pageY + ' px ,left = ' + e.pageX + ' px');
  47. }).mouseout(function (e) {
  48. jQuery('#fe-helper-g-pos,#fe-helper-gp-info').hide();
  49. });
  50. jQuery('<div id="fe-helper-g-pos"></div><div id="fe-helper-gp-info"></div>').appendTo('#fe-helper-box');
  51. jQuery('<span id="fe-helper-btn-close-grid">关闭栅格层</span>')
  52. .appendTo('#fe-helper-box').click(function () {
  53. jQuery('#fe-helper-box').remove();
  54. });
  55. };
  56. // 绘制Ruler
  57. let _drawRuler = function () {
  58. let _t = 0, _h = 30, _w = 30;
  59. let $win = jQuery(window);
  60. let $page = jQuery('html');
  61. let elScroll = jQuery(document.scrollingElement || 'html');
  62. let $width = Math.max($win.width(), $page.width(), elScroll[0].scrollWidth);
  63. let $height = Math.max($win.height(), $page.height(), elScroll[0].scrollHeight);
  64. let rulerTop = jQuery('#fe-helper-ruler-top').width($width);
  65. let rulerLeft = jQuery('#fe-helper-ruler-left').height($height);
  66. if (!rulerTop.children().length || rulerTop.children().last().position().left < $width - 50) {
  67. rulerTop.html('');
  68. for (let i = 30; i <= $width; i += 10) {
  69. _t = (i % 50) ? 10 : 0;
  70. jQuery('<div class="h-line"></div>').appendTo('#fe-helper-ruler-top').css({
  71. left: i - 1,
  72. top: _t + 15,
  73. height: _h - _t - 5
  74. });
  75. if (_t === 0 && i !== 0) {
  76. jQuery('<div class="h-text">' + i + '</div>').appendTo('#fe-helper-ruler-top').css({
  77. left: i - (String(i).length * 4)
  78. });
  79. }
  80. }
  81. }
  82. if (!rulerLeft.children().length || rulerLeft.children().last().position().top < $height - 50) {
  83. rulerLeft.html('');
  84. for (let i = 0; i <= $height; i += 10) {
  85. _l = (i % 50) ? 10 : 0;
  86. jQuery('<div class="v-line"></div>').appendTo('#fe-helper-ruler-left').css({
  87. left: _l + 15,
  88. top: i - 1,
  89. width: _w - _l - 5
  90. });
  91. if (_l === 0) {
  92. jQuery('<div class="v-text">' + i + '</div>').appendTo('#fe-helper-ruler-left').css({
  93. top: i - (String(i).length * 4),
  94. left: i === 0 ? 5 : 0
  95. });
  96. }
  97. }
  98. }
  99. };
  100. // 创建页面标尺
  101. let _createPageRuler = function () {
  102. if (!jQuery('#fe-helper-box')[0]) {
  103. jQuery('<div id="fe-helper-box"></div>').appendTo('body');
  104. }
  105. jQuery('<div id="fe-helper-ruler-top"></div><div id="fe-helper-ruler-left"></div>').appendTo('#fe-helper-box');
  106. _drawRuler();
  107. };
  108. // 全局事件绑定
  109. let _bindEvent = function () {
  110. //为页面注册按键监听
  111. jQuery('body').keydown(function (e) {
  112. if (jQuery('#fe-helper-box')[0]) {
  113. if (e.which === 27) { //ESC
  114. jQuery('#fe-helper-box').remove();
  115. }
  116. }
  117. });
  118. //window.onresize
  119. jQuery(window).resize(function () {
  120. if (jQuery('#fe-helper-box')[0]) {
  121. let $win = jQuery(window);
  122. let $body = jQuery('body');
  123. jQuery('#fe-helper-grid').css({
  124. width: Math.max($win.width(), $body.width()),
  125. height: Math.max($win.height(), $body.height())
  126. });
  127. _drawRuler();
  128. }
  129. });
  130. //处理scroll的时候,标尺跟着移动
  131. jQuery(window).scroll(function (e) {
  132. if (jQuery('#fe-helper-box')[0]) {
  133. let elScroll = jQuery(document.scrollingElement || 'html');
  134. //水平标尺定位
  135. jQuery('#fe-helper-ruler-top').css('left', 0 - elScroll.scrollLeft());
  136. //垂直标尺
  137. jQuery('#fe-helper-ruler-left').css('top', 0 - elScroll.scrollTop());
  138. }
  139. });
  140. };
  141. /**
  142. * 执行栅格系统检测
  143. */
  144. let _detect = function (callback) {
  145. // 加载CSS
  146. _loadCss();
  147. //创建栅格
  148. _createGrid();
  149. //创建页面标尺
  150. _createPageRuler();
  151. // 事件绑定
  152. _bindEvent();
  153. //执行回调
  154. if (callback && typeof callback === "function") {
  155. callback.call(null);
  156. }
  157. };
  158. return {
  159. detect: _detect
  160. };
  161. })();
  162. module.exports = GridRuler;