`I am facing issues with class binding in Vue 3 when using SwiperJS`

Currently, I am working with Vue 3 along with swiperjs.

I have encountered a problem related to the v-bind:class behavior in my project.

The issue arises when transitioning to the second slide, where the !h-auto class does not get applied as expected. Strangely, when moving back to the first slide, the class is applied to the second slide instead of being applied while on the second slide. This discrepancy occurs because the second element adjacent to the image in the second slide has more height than the image itself.

The desired outcome is for the !h-auto class to be applied whenever the second element next to the image in the slide is taller than the image. This should align with how the

v-bind:class="{ '!h-auto': isDescriptionTaller(index) }"
logic works.

Check out the code on CodeSandbox

https://i.sstatic.net/1EvwL.gif

I have shared the sandbox link in hopes that someone can help resolve the issue and enhance the code for future reference.

Answer №1

What an interesting problem we have here.

The class is dynamically set based on the result of the isDescriptionTaller(index) function:

<div
  :class="{ '!h-auto': isDescriptionTaller(index) }"
  :id="`image-event-${index}`"
>

This function compares the height of a div with the height of another column, and if the second column is taller, it stretches the column by adding a specific class to it.

The issue arises when the class is active because both columns end up having the same height, causing the condition in isDescriptionTaller(index) to evaluate as false. As a result, the class is removed, making the second column taller again, triggering the class to be added once more. This cycle continues endlessly.

To resolve this, you can modify the condition in isDescriptionTaller(index) to check for greater or equal height instead:

const isDescriptionTaller = (index) => {
  ...
  return description?.clientHeight >= image?.clientHeight ?? false;
};

With this change, the class remains even when the columns share the same height.


You may want to reconsider whether removing the class is necessary at all. If the image is taller, leaving the class should not cause any issues.


If you prefer applying the class only when the image is shorter than the text, consider checking the naturalHeight of the image instead of its client height:

const isDescriptionTaller = (index) => {
  const description = document.querySelector(`#description-event-${index}`);
  const image = document.querySelector(`#image-event-${index} img`); // <--- target the image

  return description?.clientHeight >= image?.naturalHeight ?? false; // <--- use image height
};

Remember that until the image source is loaded, its dimensions will be 0. If you are using the same image on both slides, you may encounter this issue only the first time the app loads. Consider utilizing the onload event or finding a CSS workaround that doesn't rely heavily on JavaScript calculations. It's a separate challenge altogether.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Does Javascript support annotation? If not, what is the best way to easily switch between debug and productive modes in a declarative manner?

I have a question that has been on my mind. I have searched Google but couldn't find any helpful links, so I thought it would be best to seek advice from experts here. My main concern is whether there is a way to create annotations in JavaScript sour ...

What is the reason for Firefox displaying the "excessive recursion" error message?

I have been working on generating an area Google chart using the code below, but I am running into an issue where Firefox is showing me a "too much recursion" error. The chart currently only has one point for testing purposes. Can anyone provide some gui ...

Tips for making sure there is a delay in between each axios call in a React

Currently in the process of developing an application that needs to interact with a RestAPI by sending a specific set of inputs. However, the API has a major flaw when it comes to scalability and tends to respond with code 429 if bombarded with too many re ...

What is the best way to include variable child directives within a parent directive in AngularJS?

Exploring a New Angular Challenge In the current project I am engaged in, I am faced with a unique problem that requires an 'angular way' solution. Despite my best efforts, I seem to be hitting a roadblock every time I attempt to solve it. The ...

Display a bootstrap toast using a JavaScript class method

One way to display a bootstrap toast is by using the following code snippet: let my_toast = new myToast('title', 'hello world'); my_toast.show(); Currently, I am loading the HTML content from an external file named myToast.html: < ...

Is it a mistake to gather errors together and send them to the promise resolve in JavaScript?

When processing a list in a loop that runs asynchronously and returns a promise, exiting processing on exception is not desired. Instead, the errors are aggregated and passed to the resolve callback in an outer finally block. I am curious if this approach ...

Using AngularJS to interact with resources by passing in the scope parameter

My issue is as follows: I am attempting to call a resource with a specific parameter, but I keep encountering the following error: [$resource:badcfg] I have spent the last 3 hours trying to resolve this, but to no avail. When I call the resource like th ...

JavaScript menu that pops up

Hello everyone, I've recently created an HTML5 file that, when clicked on a specific href link, is supposed to display an image in a smooth and elegant manner. However, so far it hasn't been working for me as expected. Instead of smoothly popping ...

Storing data in MongoDB upon the emission of a Socket.io event

In my project, I am using a MEAN stack and Socket.io to retrieve images from the real-time Instagram API. The current setup is functioning well, but now I want to store image data in a MongoDB database to maintain a record of images based on locations, rat ...

How do I access Google Chrome and perform a Google image search within a Node.js/Electron program?

I am currently working on incorporating Google Image search into my Electron-based photo application. My goal is to have users click on a button labeled "Search Google for Image," which will then open up Chrome (if installed) with the local image file in ...

Exploring the Differences: NodeJS and ExpressJS for Server-Side and Client-Side HTML Rendering

Embarking on my journey with Node.js, I find myself grappling with the concept of server-side and client-side HTML pages. My objective is to hone my skills by creating an e-commerce web store. The technology stack that I have set my sights on includes Node ...

Use Angular and JavaScript to fetch HTML from a mySQL database and dynamically render it on a webpage

I'm currently utilizing Angular for the front end and Node for the back end. The data is retrieved from a MySql database where it's manually stored in text format with HTML tags. For example: <ul> <li>item1</li> <li> ...

Invoking WinJS.Binding.List.createFiltered with an asynchronous call to the predicate function

Is there a way to wait for the async operation in this code snippet and use its result as the predicate instead of always returning false? return someList.createFiltered(function(item) { var filter = false; var ...

Is there a way for me to gain access to alter the price function within Magento?

Now, I am faced with the task of customizing the discounted price for my Shopping cart. After some independent research, I discovered that by modifying the view.phtml and item.phtml files, I can properly display the desired price. However, I still feel uns ...

Managing various swipe interactions using HTML and JavaScript/jQuery

I'm in the process of creating a mobile multiplayer game using HTML5 and Javascript. The jQuery Plugin "touchwipe" is utilized to manage swipe events in various divs like this: $('#play1').touchwipe({ wipeLeft: function(){ if ...

Organize an array of objects in JavaScript into a structure with nested children

I am facing a challenge with organizing an array of objects based on parentId and sort values. I need to create a nested array with 'children' and ensure proper sorting. Consider the following data: [{ id: 1, sort: 2, parentId: null ...

Troublesome tab key function in FireFox causing navigation issues

I'm facing an issue with the tab key focus on links in FireFox. Strangely, it's working fine in Chrome but in FireFox, it keeps looping within one element. For better understanding, I have created a demo showcasing this behavior specifically in ...

Enhance user experience with AngularJS Bootstrap autocomplete feature by automatically filling input box when hovering over dropdown options

I am currently implementing the AngularJs Bootstrap typeahead functionality. Typically, when we enter text that matches an option, the dropdown list appears. https://i.stack.imgur.com/395Ec.png What I aim for is to automatically fill the textbox with the ...

Experiencing issues with regex in JavaScript: all text enclosed within <angular> brackets vanishes

I am trying to analyze a formula and present it on the screen. For instance, I want to be able to input <path>T Q, where <path>T remains unchanged, and Q is a variable. It accepts this input, but when displaying it on the screen, only T Q shows ...

Bootstrap modal not displaying in full view

I recently ran into some issues while using a jQuery plugin with my bootstrap modal on my website. After implementing jQuery.noConflict(), I encountered a problem where the program no longer recognized $, forcing me to replace all instances of it with jQue ...