Programmatically calculate element height in vuejs 3

I have made myself a nice CarouselComponent based on bootstraps carousel, which can take children as slots and automatically creates slides for each of the children:

App.vue:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code><CarouselComponent identifier="TestCarousel">
<CarouselItemComponent someParameter="ImAParameter">
Items can also take content in their slots.
</CarouselItemComponent>
</CarouselComponent>
</code>
<code><CarouselComponent identifier="TestCarousel"> <CarouselItemComponent someParameter="ImAParameter"> Items can also take content in their slots. </CarouselItemComponent> </CarouselComponent> </code>
<CarouselComponent identifier="TestCarousel">
    <CarouselItemComponent someParameter="ImAParameter">
        Items can also take content in their slots.
    </CarouselItemComponent>
</CarouselComponent>

The component can take some additional parameters (e.g. enable/disable autoplay) but the core functionality just comes from finding the slot items and iterating bootstraps carousel items over them:

CarouselComponent.vue:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code><script setup>
import { useSlots, computed } from 'vue'
defineProps({
identifier: {
type: String,
required: true,
},
// other properties
})
var slots = useSlots()
const childComponents = computed(() => {
return slots.default()
})
</script>
<template>
<!-- standard boostrap carousel html -->
<div
class="carousel-item"
:class="{ active: index === 0}"
v-for="(child, index) in childComponents"
:key="index"
>
<component :is="child" />
</div>
<!-- standard boostrap carousel html -->
</template>
</code>
<code><script setup> import { useSlots, computed } from 'vue' defineProps({ identifier: { type: String, required: true, }, // other properties }) var slots = useSlots() const childComponents = computed(() => { return slots.default() }) </script> <template> <!-- standard boostrap carousel html --> <div class="carousel-item" :class="{ active: index === 0}" v-for="(child, index) in childComponents" :key="index" > <component :is="child" /> </div> <!-- standard boostrap carousel html --> </template> </code>
<script setup>
import { useSlots, computed } from 'vue'
defineProps({
  identifier: {
    type: String,
    required: true,
  },
  // other properties
})

var slots = useSlots()
const childComponents = computed(() => {
  return slots.default()
})
</script>

<template>
    <!-- standard boostrap carousel html -->
      <div
        class="carousel-item"
        :class="{ active: index === 0}"
        v-for="(child, index) in childComponents"
        :key="index"
      >
        <component :is="child" />
      </div>
    <!-- standard boostrap carousel html -->
</template>

This works fine, but I want to add a feature and have not found a good way to do it.
The problem with this setup is, that I cannot know the actual height of the different slides beforehand (and enforcing fixed heights resulted in unwanted look and feel). This results the content below the carousel jumping around due to the height of the carousel being changed for each slide change

So, what I would like to do is to find out the height of the tallest slide and set the height of the carousel based on that. The question is, how can I do that without the user noticing?
I think I have to actually render the slides to find the height, but can I do that in the background somehow? Or is there another (better) approach to this problem altogether?

There are several “classical” solutions to your problem:

  1. Probably the most popular is to contain the carousel in a section with fixed height, making sure its height is higher than the max height of the carousel (typically 100vh – menu height) = a screen height. You might want to center the carousel vertically in the section. This way, the carousel doesn’t influence the vertical offset of the rest of the page
  2. Another solution is to only update the carousel’s height upwards:
    • contain the carousel in a wrapper with overflow-y: hidden; max-height: 0; min-height: 0.
    • watch the carousel’s height (with a resize observer) and every time it changes, update the wrapper’s min and max height to the max between it’s current height and the carousel’s height. This way, the content below the carousel will only move downwards and only on the first pass through the carousel slides. After that, it will already be at its max value, so it won’t move anymore. To avoid jumps in height, you should animate the transitions of min-height and max-height on the wrapper.
  3. And third solution is to render all slides inside a hidden element having the same width as the carousel, calculate their heights and set it as the carousel’s height. Performance wise, this is the most costly. Also, you’ll need to do it on each window resize, preferably debounced. Although technically the most precise, I advise avoiding it, for performance reasons.

3

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật