Jquery enables smooth image transitions when changing classes

I am seeking assistance with implementing a simple transition effect on hover/out for a div box to switch between the first image .image-main and the second one image-hover. The current setup works properly, but I am unsure how to incorporate a mouseOn/mouseOut effect (a basic opacity fade out will suffice).

I prefer using JS for this task as I intend to include additional actions. The CSS hover effect interfered with the activity of .img-fluid.

HTML:

<div class="col-6">

  <div class="product-box">
    <div class="product-box__image">
      <img class="img-fluid image-main" src="https://dummyimage.com/400x200/000/fff">
      <img class="img-fluid image-hover" src="https://dummyimage.com/400x200/00cc00/fff">
    </div>
    <div class="product-box__content">
      <p class="text-center p-3">
        hello description<br>on hover - change image too
      </p>
    </div>
  </div>

</div>

JS:

$('.product-box').hover(
  function() {
    var main = $(this).find('.image-main');
    var second = $(this).find('.image-hover');
    main.removeClass('image-main');
    main.addClass('image-hover');
    second.addClass('image-main');
    second.removeClass('image-hover');

  },
  function() {
    var second = $(this).find('.image-main');
    var main = $(this).find('.image-hover');
    second.removeClass('image-main');
    second.addClass('image-hover');
    main.addClass('image-main');
    main.removeClass('image-hover');
  }
);

CSS:

    .product-box {
      border: 1px solid gray;
    }

    .image-hover {
      display: none;
    }

https://jsfiddle.net/qsyr1te7/2/

Answer №1

To achieve the desired effect, start by setting the initial image to full opacity with a smooth transition effect.

.main-image {
  transition: opacity .4s;
  opacity: 1;
}

Next, adjust the opacity of the .hovering class to 0.

.hovering {
  opacity: 0;
}

Finally, add or remove the hovering class to the main image using jQuery.

$('.product-container').hover(
  function() {
    var img = $(this).find('.main-image');
    img.addClass('hovering');
  },
  function() {
    var img = $(this).find('.image-hover');
    img.removeClass('hovering');
  }
);

I hope this solution meets your needs. Enjoy!

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

What is the best way to synchronize MultiView and TreeView in ASP.NET web forms?

I'm looking to set up a tree view on the left side of my screen, along with a multiview showing content based on the selected tree item on the right. However, I'm having trouble aligning the multiview properly next to the tree view - it seems to ...

issues with the functionality of bootstrap modal

I'm currently working on a project where I need to set up a modal popup using bootstrap. The website I'm working on is organized by departments, so the only part of the code that I have control over is the main body of the site. I have included t ...

Exploring the World of Image Galleries: Preloading Compared to AJAX

Currently, I'm facing a dilemma regarding the creation of an image gallery. Should I preload all the images when the page loads, or should I use ajax to fetch new images before displaying them in the gallery? In the example below, I'm looking at ...

"Any tips on maintaining the blue color of my dropdown link when it is selected as active

I'm struggling to keep the bootstrap accordion dropdown link blue when it's active. I've tried different methods but none seem to work. What am I missing? Here's my code: <div class="visible-sm visible-xs" id="accordion" role="t ...

Guide on retrieving a file through an HTTP request with restricted cross-origin access restrictions

Having some issues with downloading files from specific links, such as . My goal is to automate the download process for these redirected files. The challenge I'm facing is that trying to access the link directly through a web browser just gives me a ...

Incorrect display of French characters in emails

Issues arise when sending emails with French characters, as they are not displaying correctly in the mail body. I made sure to include the following code in my HTML page: <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> Despit ...

What sets apart the two syntaxes for JavaScript closures?

Similar Query: Is there a distinction between (function() {…}()); and (function() {…})();? I've come across a way to prevent variables from becoming global by using the following syntax: (function ($, undefined) { })(jQuery); Rec ...

Using Ajax to trigger JSON-formatted HTML output

Issue with Populating Modal Window I am encountering difficulties while trying to populate a modal window dynamically with extended information retrieved from a mySQL database. Despite attempting various methods such as nested arrays, while loops, and for ...

What is the process for assigning a value to the body in a div element?

Within a div, there is a body element structured like this: <div id = "TextBox1"> <iframe id = "TextBox1_1"> #document <html> <head></head> <body></body> </html> </iframe> </div> I have attempte ...

Ways to align div elements

I am currently in the process of developing my own custom animation player. Utilizing Three.js for object rendering has been successful so far. However, the challenge lies in incorporating control options at the bottom of the player interface (such as play ...

Storing user data on the client side of a website is most effectively accomplished by

I am looking to incorporate the following features: I would like to personalize the user experience by greeting them with their login name on the webpage (e.g. 'Hello, Fred!'). I also want to display or hide certain content based on the user&apo ...

The implementation of display flex is causing issues with the material-ui grid layout

Struggling to implement the grid system from material-ui due to an issue where grid items do not resize when resizing the browser window. The culprit seems to be a parent div with the style property "display": "flex". The dilemma arises from using a mater ...

Generate a container element that occupies the remaining space on the screen under a header of adjustable height

I'm trying to create a layout where the header height can vary, but the footer has a fixed height. I want the left nav and content sections to fill the screen height, with the left nav having a fixed width and the content taking up the remainder. Is ...

Use jQuery.ajax() to submit data in separate rows of a table

At the moment, I have no issues submitting my form as long as it contains only one row in the table. However, when I add a second row, an error occurs and the submission fails. My goal is to be able to post the data from each table row separately with just ...

Angular: The Ultimate Guide to Reloading a Specific Section of HTML (Form/Div/Table)

On my create operation page, I have a form with two fields. When I reload the page using window.reload in code, I can see updates in the form. However, I want to trigger a refresh on the form by clicking a button. I need help writing a function that can r ...

The output for each function is consistently the same for every record

Implementing a foreach loop to send data and display it in the success function of an AJAX call is functioning smoothly. Recently, I made a modification to the code by introducing a new data-attribute which contains the $creator variable. Here's how ...

Counting with Jquery and managing variables inside square brackets

When a form containing a hidden field is loaded, <input type = "hidden" class = "counter" value="6"> I utilize Jquery in my code: var counter = $(".counter:last").val() After that, a row with a text field is added when a click event occurs: .app ...

Avoiding the appearance of a scrollbar when content extends beyond its containing block

I am struggling with writing markup and CSS to prevent a background image from creating a scrollbar unless the viewport is narrower than the inner content wrapper: The solution provided in this post didn't work for me: Absolutely positioned div on ri ...

Dealing with full-width elements on mobile devices

I've got a pretty basic question here but can't seem to find a straightforward answer. I'm using Bootstrap to style a website and struggling with making it mobile-responsive. Following the given answer, I used this code to show a button ne ...

What could be causing the issue with my code where the canvas is not showing boxes beyond a y-coordinate of 8 along the x-axis?

I've been working on creating a 64 square checkerboard using the html canvas element in javascript, but I'm encountering an issue. The boxes aren't rendering properly after the first 8 squares on the y-axis, and I can't figure out where ...