项目作者: qstudio

项目描述 :
Simple helper script which shows current Bootstrap breakpoint, viewport dimensions and - if clicked, wraps all elements in borders for simpler UI debugging.
高级语言: HTML
项目地址: git://github.com/qstudio/bootstrap-breakpoint-helper.git
创建时间: 2020-08-30T08:32:04Z
项目社区:https://github.com/qstudio/bootstrap-breakpoint-helper

开源协议:MIT License

下载


Bootstrap Breakpoint Helper

Simple helper script which shows current Bootstrap breakpoint, viewport dimensions and - if clicked, wraps all elements in borders for simpler UI debugging.

Example Grab

Working example on codepen

https://codepen.io/qstudio/pen/bGpWYYg?editors=1111


Full HTML

  1. <html>
  2. <head>
  3. <script type='text/javascript' src='https://code.jquery.com/jquery-1.12.4.min.js'></script>
  4. <script type='text/javascript'src='https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js'></script>
  5. <script type='text/javascript' src='https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js'></script>
  6. <style>body.debug :not(.nodebug) { border: 1px dashed pink !important; }</style>
  7. <title>Q Studio ~ Bootstrap Helper</title>
  8. </head>
  9. <body>
  10. <script>
  11. if( typeof jQuery !== 'undefined' ) {
  12. // assign target ##
  13. var $bs_element = "#bs_helper";
  14. var $bs_helper;
  15. // show breakpoint on load, tooltip will be updated to show actual document dimentions ##
  16. jQuery(window).load(function(){
  17. // assign target ##
  18. $bs_helper = jQuery($bs_element);
  19. q_bootstrap_tooltip( true, false );
  20. // click to add debug borders ##
  21. $bs_helper.click( function() {
  22. jQuery("body").toggleClass("debug");
  23. });
  24. });
  25. // resizing, open tooltip and update document dimensions as they change ##
  26. jQuery(window).on('resize', function(){
  27. q_bootstrap_tooltip( false, true );
  28. });
  29. };
  30. // Tooltip with BS breakpoint info ##
  31. function q_bootstrap_tooltip( load, resize ) {
  32. // sizes ##
  33. vw = Math.max(document.documentElement.clientWidth || 0, window.innerWidth || 0);
  34. vh = Math.max(document.documentElement.clientHeight || 0, window.innerHeight || 0);
  35. // resize ##
  36. resize = resize || false;
  37. load = load || false;
  38. // update attrs ##
  39. $bs_helper.attr('title', 'Width: '+ vw +'px<br />Height: '+ vh +'px' );
  40. $bs_helper.html( q_bootstrap_breakpoint().name );
  41. if ( load ) {
  42. // trigger tooltip ##
  43. $bs_helper.tooltip('show');
  44. setTimeout(function(){
  45. $bs_helper.tooltip('hide');
  46. }, 3000);
  47. }
  48. // possible delay, for resizing ##
  49. if ( resize ) {
  50. // trigger tooltip ##
  51. $bs_helper.tooltip('show');
  52. // set short timeout to update document dimensions, as they change ##
  53. setInterval(function(){
  54. jQuery('.tooltip-inner').html( 'Width: '+ vw +'px<br />Height: '+ vh +'px' );
  55. }, 30);
  56. // add an event listener to check for an end to the resize action ##
  57. window.addEventListener(
  58. "resize",
  59. q_bootstrap_debounce(
  60. function(e){
  61. // when resizing is done, set a timeout for 3 secs, then close the tooltip ##
  62. setTimeout(function(){
  63. $bs_helper.tooltip('hide');
  64. }, 3000 );
  65. }
  66. ), { passive: true } // Passive Event Listener ##
  67. );
  68. }
  69. }
  70. // resize debouncer, set form eventlistener ##
  71. function q_bootstrap_debounce( func ){
  72. var timer;
  73. return function(event){
  74. if(timer) clearTimeout(timer);
  75. timer = setTimeout(func,100,event);
  76. };
  77. }
  78. /*
  79. https://cdn.jsdelivr.net/npm/bootstrap-detect-breakpoint/src/bootstrap-detect-breakpoint.js
  80. */
  81. function q_bootstrap_breakpoint() {
  82. const breakpointNames = ["xl", "lg", "md", "sm", "xs"]
  83. let breakpointValues = []
  84. for (const breakpointName of breakpointNames) {
  85. breakpointValues[breakpointName] = window.getComputedStyle(document.documentElement).getPropertyValue('--breakpoint-' + breakpointName)
  86. }
  87. let i = breakpointNames.length
  88. for (const breakpointName of breakpointNames) {
  89. i--
  90. if (window.matchMedia("(min-width: " + breakpointValues[breakpointName] + ")").matches) {
  91. return {
  92. name: breakpointName,
  93. height: vh,
  94. width: vw,
  95. index: i
  96. }
  97. }
  98. }
  99. return null
  100. }
  101. </script>
  102. <span
  103. id="bs_helper"
  104. class="nodebug badge badge-warning"
  105. data-toggle="tooltip"
  106. data-placement="top"
  107. data-html="true"
  108. title="Tooltip on top"
  109. style="position: fixed; left: 0; bottom: 0; padding: 20px;"
  110. >~@~
  111. </span>
  112. <div class="example p-5">hello<strong class="ml-1 text-strong">world</strong></div>
  113. </body>
  114. </html>