Advertisement
Guest User

Untitled

a guest
Jul 23rd, 2019
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.97 KB | None | 0 0
  1. var biggestHeight = 0;
  2. // Loop through elements children to find & set the biggest height
  3. $(".container *").each(function(){
  4. // If this elements height is bigger than the biggestHeight
  5. if ($(this).height() > biggestHeight ) {
  6. // Set the biggestHeight to this Height
  7. biggestHeight = $(this).height();
  8. }
  9. });
  10.  
  11. // Set the container height
  12. $(".container").height(biggestHeight);
  13.  
  14. <div class="autoheight" style="background: blue">
  15. <div style="position: absolute; width: 33.3%; background: red">
  16. Only
  17. <div style="background: green">
  18. First level elements are measured as expected
  19. </div>
  20. </div>
  21. <div style="float:left; width: 33.3%; background: red">
  22. One Two Three Four Five Six Seven Eight Night Ten
  23. </div>
  24. <div style="float:left; width: 33.3%; background: red">
  25. One Two Three Four Five Six
  26. </div>
  27. </div>
  28.  
  29. <script>
  30. function processAutoheight()
  31. {
  32. var maxHeight = 0;
  33.  
  34. // This will check first level children ONLY as intended.
  35. $(".autoheight > *").each(function(){
  36.  
  37. height = $(this).outerHeight(true); // outerHeight will add padding and margin to height total
  38. if (height > maxHeight ) {
  39. maxHeight = height;
  40. }
  41. });
  42.  
  43. $(".autoheight").height(maxHeight);
  44. }
  45.  
  46. // Recalculate under any condition that the viewport dimension has changed
  47. $(document).ready(function() {
  48.  
  49. $(window).resize(function() { processAutoheight(); });
  50.  
  51. // BOTH were required to work on any device "document" and "window".
  52. // I don't know if newer jQuery versions fixed this issue for any device.
  53. $(document).resize(function() { processAutoheight(); });
  54.  
  55. // First processing when document is ready
  56. processAutoheight();
  57. });
  58. </script>
  59.  
  60. /**
  61. * Sets the height of a container to its maximum height of its children. This
  62. * method is required in case
  63. *
  64. * @param selector jQuery selector
  65. */
  66. function setHeightByChildrenHeight(selector)
  67. {
  68. $(selector).each(function()
  69. {
  70. var height = 0;
  71.  
  72. $(this).children("*").each(function()
  73. {
  74. height = Math.max(height, $(this).height());
  75. });
  76.  
  77. $(this).height(height);
  78. });
  79. };
  80.  
  81. <div class="container">
  82. <div class="faq-cards">
  83. <div class="faq-cards__card">Im A Card</div>
  84. <div class="faq-cards__card">Im A Card</div>
  85. <div class="faq-cards__card">Im A Card</div>
  86. <div class="faq-cards__card">Im A Card</div>
  87. <div class="faq-cards__card">Im A Card</div>
  88. <div class="faq-cards__card">Im A Card</div>
  89. <div class="faq-cards__card">Im A Card</div>
  90. </div>
  91. </div>
  92.  
  93. .container {
  94. height:auto;
  95. width: 1280px;
  96. margin:auto;
  97. background: #CCC;
  98. }
  99. .faq-cards {
  100. display:flex;
  101. flex-wrap:wrap;
  102. justify-content: center;
  103. position:relative;
  104. bottom:40px;
  105. height:auto;
  106. }
  107. .faq-cards__card {
  108. margin:10px;
  109. position:relative;
  110. width:225px;
  111. background: beige;
  112. height: 100px;
  113. padding:10px;
  114. text-align: center;
  115. }
  116.  
  117. $(".container").height($(document).height());
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement