not really known
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.

163 lines
5.9 KiB

  1. /**
  2. * jQuery.Ruler v0.9
  3. * Add Photoshop-like rulers and mouse position to a container element using jQuery.
  4. * http://ruler.hilliuse.com
  5. *
  6. * Dual licensed under the MIT and GPL licenses.
  7. * Copyright 2013 Hillius Ettinoffe http://hilliuse.com
  8. */
  9. ;(function( $ ){
  10. $.fn.ruler = function(options) {
  11. var defaults = {
  12. vRuleSize: 18,
  13. hRuleSize: 18,
  14. showCrosshair : true,
  15. showMousePos: true
  16. };//defaults
  17. var settings = $.extend({},defaults,options);
  18. var hRule = '<div class="ruler hRule"></div>',
  19. vRule = '<div class="ruler vRule"></div>',
  20. corner = '<div class="ruler corner"></div>',
  21. vMouse = '<div class="vMouse"></div>',
  22. hMouse = '<div class="hMouse"></div>',
  23. mousePosBox = '<div class="mousePosBox">x: 50%, y: 50%</div>';
  24. if (!Modernizr.touch) {
  25. // Mouse crosshair
  26. if (settings.showCrosshair) {
  27. $('body').append(vMouse, hMouse);
  28. }
  29. // Mouse position
  30. if (settings.showMousePos) {
  31. $('body').append(mousePosBox);
  32. }
  33. // If either, then track mouse position
  34. if (settings.showCrosshair || settings.showMousePos) {
  35. $(window).mousemove(function(e) {
  36. if (settings.showCrosshair) {
  37. $('.vMouse').css("top",e.pageY-($(document).scrollTop())+1);
  38. $('.hMouse').css("left",e.pageX+1);
  39. //-($(window).scrollTop())
  40. }
  41. if (settings.showMousePos) {
  42. $('.mousePosBox').html("x:" + (e.pageX-settings.vRuleSize) + ", y:" + (e.pageY-settings.hRuleSize) ).css({
  43. top: e.pageY-($(document).scrollTop()) + 16,
  44. left: e.pageX + 12
  45. });
  46. }
  47. });
  48. }
  49. }
  50. //resize
  51. $(window).resize(function(e){
  52. var $hRule = $('.hRule');
  53. var $vRule = $('.vRule');
  54. $hRule.empty();
  55. $vRule.empty().height(0).outerHeight($vRule.parent().outerHeight());
  56. // Horizontal ruler ticks
  57. var tickLabelPos = settings.vRuleSize;
  58. var newTickLabel = "";
  59. while ( tickLabelPos <= $hRule.width() ) {
  60. if ((( tickLabelPos - settings.vRuleSize ) %50 ) == 0 ) {
  61. newTickLabel = "<div class='tickLabel'>" + ( tickLabelPos - settings.vRuleSize ) + "</div>";
  62. $(newTickLabel).css( "left", tickLabelPos+"px" ).appendTo($hRule);
  63. } else if ((( tickLabelPos - settings.vRuleSize ) %10 ) == 0 ) {
  64. newTickLabel = "<div class='tickMajor'></div>";
  65. $(newTickLabel).css("left",tickLabelPos+"px").appendTo($hRule);
  66. } else if ((( tickLabelPos - settings.vRuleSize ) %5 ) == 0 ) {
  67. newTickLabel = "<div class='tickMinor'></div>";
  68. $(newTickLabel).css( "left", tickLabelPos+"px" ).appendTo($hRule);
  69. }
  70. tickLabelPos = (tickLabelPos + 5);
  71. }//hz ticks
  72. // Vertical ruler ticks
  73. tickLabelPos = settings.hRuleSize;
  74. newTickLabel = "";
  75. while (tickLabelPos <= $vRule.height()) {
  76. if ((( tickLabelPos - settings.hRuleSize ) %50 ) == 0) {
  77. newTickLabel = "<div class='tickLabel'><span>" + ( tickLabelPos - settings.hRuleSize ) + "</span></div>";
  78. $(newTickLabel).css( "top", tickLabelPos+"px" ).appendTo($vRule);
  79. } else if (((tickLabelPos - settings.hRuleSize)%10) == 0) {
  80. newTickLabel = "<div class='tickMajor'></div>";
  81. $(newTickLabel).css( "top", tickLabelPos+"px" ).appendTo($vRule);
  82. } else if (((tickLabelPos - settings.hRuleSize)%5) == 0) {
  83. newTickLabel = "<div class='tickMinor'></div>";
  84. $(newTickLabel).css( "top", tickLabelPos+"px" ).appendTo($vRule);
  85. }
  86. tickLabelPos = ( tickLabelPos + 5 );
  87. }//vert ticks
  88. });//resize
  89. return this.each(function() {
  90. var $this = $(this);
  91. // Attach rulers
  92. // Should not need 1 min padding-top of 1px but it does
  93. // will figure it out some other time
  94. $this.css("padding-top", settings.hRuleSize + 1 + "px");
  95. if (settings.hRuleSize > 0) {
  96. $(hRule).height(settings.hRuleSize).prependTo($this);
  97. }
  98. if (settings.vRuleSize > 0) {
  99. var oldWidth = $this.outerWidth();
  100. $this.css("padding-left", settings.vRuleSize + 1 + "px").outerWidth(oldWidth);
  101. $(vRule).width(settings.vRuleSize).height($this.outerHeight()).prependTo($this);
  102. }
  103. if ( (settings.vRuleSize > 0) && (settings.hRuleSize > 0) ) {
  104. $(corner).css({
  105. width: settings.vRuleSize,
  106. height: settings.hRuleSize
  107. }).prependTo($this);
  108. }
  109. var $hRule = $this.children('.hRule');
  110. var $vRule = $this.children('.vRule');
  111. // Horizontal ruler ticks
  112. var tickLabelPos = settings.vRuleSize;
  113. var newTickLabel = "";
  114. while ( tickLabelPos <= $hRule.width() ) {
  115. if ((( tickLabelPos - settings.vRuleSize ) %50 ) == 0 ) {
  116. newTickLabel = "<div class='tickLabel'>" + ( tickLabelPos - settings.vRuleSize ) + "</div>";
  117. $(newTickLabel).css( "left", tickLabelPos+"px" ).appendTo($hRule);
  118. } else if ((( tickLabelPos - settings.vRuleSize ) %10 ) == 0 ) {
  119. newTickLabel = "<div class='tickMajor'></div>";
  120. $(newTickLabel).css("left",tickLabelPos+"px").appendTo($hRule);
  121. } else if ((( tickLabelPos - settings.vRuleSize ) %5 ) == 0 ) {
  122. newTickLabel = "<div class='tickMinor'></div>";
  123. $(newTickLabel).css( "left", tickLabelPos+"px" ).appendTo($hRule);
  124. }
  125. tickLabelPos = (tickLabelPos + 5);
  126. }//hz ticks
  127. // Vertical ruler ticks
  128. tickLabelPos = settings.hRuleSize;
  129. newTickLabel = "";
  130. while (tickLabelPos <= $vRule.height()) {
  131. if ((( tickLabelPos - settings.hRuleSize ) %50 ) == 0) {
  132. newTickLabel = "<div class='tickLabel'><span>" + ( tickLabelPos - settings.hRuleSize ) + "</span></div>";
  133. $(newTickLabel).css( "top", tickLabelPos+"px" ).appendTo($vRule);
  134. } else if (((tickLabelPos - settings.hRuleSize)%10) == 0) {
  135. newTickLabel = "<div class='tickMajor'></div>";
  136. $(newTickLabel).css( "top", tickLabelPos+"px" ).appendTo($vRule);
  137. } else if (((tickLabelPos - settings.hRuleSize)%5) == 0) {
  138. newTickLabel = "<div class='tickMinor'></div>";
  139. $(newTickLabel).css( "top", tickLabelPos+"px" ).appendTo($vRule);
  140. }
  141. tickLabelPos = ( tickLabelPos + 5 );
  142. }//vert ticks
  143. });//each
  144. };//ruler
  145. })( jQuery );