Advertisement
Guest User

Untitled

a guest
Sep 21st, 2017
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.44 KB | None | 0 0
  1. // for each div on page with a background image
  2. // show a label in its top right corner with the max height and width values
  3. // When page is resized, calculate
  4.  
  5. // create size display element
  6. let sizeDisplayStyles = "style='position: absolute;top: 0;right: 0;padding: 10px;background: navajowhite;'";
  7. let displayEl = `<span class='sizeDisplay' ${sizeDisplayStyles} data-w="0" data-h="0" >waiting for page resize...</span>`
  8.  
  9.  
  10. function magic () {
  11. let els = document.querySelectorAll("*")
  12.  
  13. for (var i = 0, len = els.length; i < len; i++) {
  14. el = els[i];
  15. if (el.currentStyle) {
  16. if( el.currentStyle['backgroundImage'] !== 'none' ) {
  17. // console.log("bg-found:", el)
  18. jQuery(el).prepend(displayEl)
  19. }
  20.  
  21. }
  22. else if (window.getComputedStyle) {
  23. if( document.defaultView.getComputedStyle(el, null).getPropertyValue('background-image') !== 'none' ) {
  24. // console.log("bg-found:", el)
  25. jQuery(el).prepend(displayEl)
  26. }
  27.  
  28. }
  29. }
  30. }
  31.  
  32. magic()
  33.  
  34.  
  35. // do stuff on window resize
  36. window.addEventListener('resize', function(){
  37.  
  38. console.log('page resized')
  39.  
  40. // loop over all .sizeDisplay elements
  41. // get the width and height of its parent element
  42. // save them to data attributes and update the text within span
  43. // we want the max width and height (add logic to compare)
  44.  
  45. jQuery('.sizeDisplay').each(function(i){
  46.  
  47. // console.log('loop:', jQuery(this));
  48.  
  49. // Get parent el and width, height
  50. let parent = jQuery(this).parent();
  51. console.log("parent", parent);
  52. let pWidth = jQuery(parent).outerWidth();
  53. let pHeight = jQuery(parent).outerHeight();
  54. console.log("parent dimensions:", pWidth, pHeight)
  55.  
  56.  
  57. // Get current width, height stored in data attributes
  58. let curWidth = jQuery(this).attr('data-w');
  59. let curHeight = jQuery(this).attr('data-h');
  60.  
  61. let toUpdate = false;
  62.  
  63. if(pWidth > curWidth){
  64. jQuery(this).attr('data-w', pWidth);
  65. toUpdate = true;
  66. }
  67.  
  68. if(pHeight > curHeight){
  69. jQuery(this).attr('data-h', pHeight)
  70. toUpdate = true
  71. }
  72.  
  73. if(toUpdate) updateText()
  74.  
  75. })
  76.  
  77.  
  78. }, true);
  79.  
  80.  
  81. function updateText(){
  82. jQuery('.sizeDisplay').each(function(i){
  83. // get width and height from data attributes
  84. // set text approppriately
  85. jQuery(this).text("width: " + jQuery(this).attr('data-w') + ", height: " + jQuery(this).attr('data-h'))
  86. })
  87. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement