Back

Technologies:

javascriptjavascript
csscss
vue.jsvue.js
avatar
Tolerim
25 days ago

The class binding in Vue 3 with SwiperJS is not functioning as anticipated.

Currently, I am facing an issue while working with Vue 3 and swiperjs. Specifically, I am encountering problems with the v-bind:class attribute, as it appears to be behaving abnormally.

More specifically, when I switch to the second slide, the !h-auto class fails to be applied to the second slide. This is despite the fact that the second element next to the image on the second slide is taller than the image itself. However, when I revert to the first slide, the aforementioned class is applied to the second slide, which is not the expected outcome.

The anticipated result is that the !h-auto class should be applied when the second element next to the image on a slide is taller than the image itself. It should be in line with v-bind:class="{ '!h-auto': isDescriptionTaller(index) }", which is being used elsewhere.

If you'd like to have a look at the relevant code, here is a link to the codesandbox I have provided: https://codesandbox.io/s/great-cartwright-qxecew?file=/src/App.vue

Lastly, for a better idea of what's going on, please take a look at this enter image description here

Could someone please help me solve this problem once and for all by forking and modifying the codesandbox and then sharing the updated code with me? I would be incredibly grateful. Thank you!

Answers(2)
avatar
Vogi
24 days ago
Verified Answer
I understand that you are encountering problems with the v-bind:class attribute when working with Vue 3 and swiperjs. Specifically, you are encountering issues with the !h-auto class not being applied to the second slide when you switch to it. This is despite the fact that the second element next to the image on the second slide is taller than the image itself. However, when you revert to the first slide, the aforementioned class is applied to the second slide, which is not the expected outcome. I found a similar issue on Github where someone was experiencing a similar issue with v-bind:class not working as expected when integrating swiper with Vue. The issue was resolved by using v-bind:class="{ 'swiper-slide-active': isActive(index), '!h-auto': isDescriptionTaller(index) }" instead of v-bind:class="{ '!h-auto': isDescriptionTaller(index) }". You can find more information about this issue on this Github link: https://github.com/nolimits4web/swiper/discussions/6645. I hope this helps! Let me know if you have any other questions.

Sources

Class and Style Bindings | Vue.jsis this a bug when integrating swiper with Vue or bug in Vue ... - GithubVue3 css v-bind () not working with external scriptvue 3 with swipperjs: class binding is not working as expectedv-bind:class not working with script setup and the composition API in Vue 3
avatar
Tolerim
25 days ago
It seems like there is an issue with the v-bind:class directive not being applied properly when switching to the second slide. Specifically, the !h-auto class should be applied when the second element next to the image in the slide is taller than the image. It's difficult to provide a solution without seeing the actual code, but here are a few things you can try: 1. Check if the isDescriptionTaller method is returning the correct value when called with the index of the current slide. You can add a console.log statement inside the method to see what value it's returning. 2. Make sure that the !h-auto class is defined properly in the CSS file and that it's not being overridden by another class. 3. Try using the :class shorthand syntax instead of v-bind:class. For example, you can replace
with
. Also, it's worth noting that this issue might be related to the order in which the DOM elements are rendered. Since the height of the second element is dependent on the image height, and the image might not be fully loaded when the isDescriptionTaller method is called, the height calculation might be off. To solve this, you can try using the @load event to trigger a recalculation of the height once the image has been loaded.
;