Strategies for creating a fade-in/fade-out effect that occurs only once

Having an issue with my product images. There are two stacked images and upon hovering, the top one fades out to reveal the bottom image. However, I noticed that if you repeatedly move your mouse over it like twenty times, the animation continues until all cycles are complete. Is there a way to make it stop after just one cycle?

jQuery(function($) {
  $('.product a.product-image .primary-img').hover(function() {
    $(this).fadeTo(300, 0);
  }, function() {
    $(this).fadeTo(300, 1);
  });
});
.product a.product-image {
  position: relative;
  display: block;
}
.product a.product-image .primary-img {
  position: relative;
  z-index: 10;
}
.product a.product-image .secondary-img {
  position: absolute;
  left: 0;
  top: 0;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="product">
  <a class="product-image" href="#">
    <img class="primary-img" src="http://d17ol771963kd3.cloudfront.net/148828/ma/r8-xsWDWKCM.jpg" alt="">
    <img class="secondary-img" src="http://d17ol771963kd3.cloudfront.net/148821/ma/f2xjw32B2U4.jpg" alt="">
  </a>
</div>

Answer №1

For a solution, consider implementing the following code snippet:

jQuery(function($) {
  $('.product a.product-image .primary-img').hover(function() {
    $(this).stop(true).fadeTo(300,0);
  }, function() {
    $(this).fadeTo(300,1);
  });
});

The true is utilized as a flag for clearing the queue.

.stop( [clearQueue ] [, jumpToEnd ] )

https://api.jquery.com/stop/

Answer №2

One way to achieve image opacity toggle on hover is by using CSS

.product a.product-image {
  position: relative;
  display: inline-block;
}

a.product-image:hover .primary-img {
  opacity:0;
}

a.product-image:hover .secondary-img {
  opacity:1;
}

a.product-image .secondary-img {
position: absolute;
  left: 0;top: 0;
  opacity:0;
}

.primary-img, .secondary-img{
  transition:opacity 300ms;
}
<div class="product">
<a class="product-image" href="#">
<img class="primary-img" src="http://d17ol771963kd3.cloudfront.net/148828/ma/r8-xsWDWKCM.jpg" alt="">
<img class="secondary-img" src="http://d17ol771963kd3.cloudfront.net/148821/ma/f2xjw32B2U4.jpg" alt="">
</a>
</div>

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

Utilizing SweetAlert to Show Notifications Following Data Insertion in MySQL with PHP

Combining HTML, PHP, and AJAX to insert data into a MySQL database has proved successful. However, I am facing difficulties in displaying success or error messages within the AJAX success function using SweetAlert. Despite the data being successfully inser ...

I am having an issue with my DIV not functioning correctly in conjunction with my JS animation. What modifications should I make to

I have a simple JS animation script that fetches data from the <div class="quiz"> Some content </div> for animation. When I include this script in my HTML, the animation and other functions such as previous and next work properly. See ...

How can I filter out strings and only keep the numbers in an array using JavaScript?

After searching through numerous similar questions, I have been unable to find a solution to this particular challenge. I am in need of a function that can remove all non-number items from an array while also modifying the existing array rather than creati ...

"Utilizing jQuery to target and manipulate the outer HTML content

Picture this scenario: <div id="xxx"><p>Hello World</p></div> When the .html function is called like this: $("#xxx").html(); The output will be: <p>Hello World</p> However, I actually need to retrieve: <div id= ...

Removing a background by linking a CSS file

Issue Resolved: I have found the solution. The problem was that the css document still contained html tags. Thank you to everyone who provided assistance. While designing a website using css, I encountered a situation where the site worked perfectly when ...

Modifying the href in Django according to the current page URL: a step-by-step guide

Like the title suggests, I'm curious about the proper way to dynamically change a link's href based on the current URL that the user is visiting. I attempted a solution, but it proved unsuccessful. {% if url == '' %} href='/cooki ...

Can the :after pseudo-element be modified when hovering, except when hovering over the after element itself?

Currently, my CSS code looks like this: class-name:hover:after {} Although it works well, the hover effect triggers even when hovering over the after portion of the element. Is there a way to modify the code so that the :hover only applies when hovering ...

Contrast between the structure of asp.net button and hyperlinks

I want to transfer the styles of a hyperlink in regular html to an Asp.net button. However, I encountered a strange background issue that is disrupting the appearance of the buttons. Hyperlink: Asp.net Button How can I remove the outline that appears o ...

What is the CSS technique for displaying text overflow after displaying only two lines within a div?

Currently working on a webpage design where I am looking to display text overflow using ellipsis within a div. However, my requirement is to display two lines of data in the div before handling the text overflow. I prefer not setting a fixed height for t ...

The method of AJAX Pattern for users to make interactive decisions

Currently, I am exploring different strategies to create interactive user decisions. My project involves a rather extensive form that users need to fill out. Upon submission, an AJAX-Post-Request is transmitted to the server. While some fault checks can be ...

Ways to retrieve json_decode() information following ajax transmission

Currently experimenting with an ajax script to enhance my skills in ajax. I am using the serializeArray() method to submit a form via ajax, however, although I can successfully pass the data, I am unable to perform any operations on it. var submit_post = ...

Strange CSS/browser storage glitch

UPDATE: JUST REALIZED THIS ISSUE IS ONLY OCCURRING ON FIREFOX, NOT CHROME ANOTHER UPDATE: Oddly enough, this problem only occurs locally. When I push it to GitHub, everything works fine. So strange. I suppose that means it's not a major issue. On my ...

Issue with PHP/MySQL registration page failing to save user information to database

As I work on developing my website that includes user registration and login functionality, I encountered an issue where the registration page was not loading properly. Initially, the registration process worked perfectly, but the next day, upon trying to ...

JQuery Ajax fails to retrieve variables from an external PHP file

I have a code snippet that retrieves data from a form and sends it to form.php for processing. The form.php file then echoes out the variables (Fullname: $fullname Email: $email Link: $link Instructions: $instr) which are expected to be displayed in #succe ...

What happens in jQuery when no element is being referenced?

In my script, a unique DIV is displayed when hovering over a specific element, and the others are hidden. It's a simple concept. When the page loads, I set a div as the "intro" shown div... My goal is to display the intro div if none of the other 5 ...

place a stationary object within a confined space

I have a specific element called #listMDMap that needs to be positioned within the row div. <div class="col-md-6"> <div class="row"> <div id="listMDMap" [style.position]= ...

Which types of responses does jQuery.ajax define as "successful"?

My jQuery AJAX post request seems to be triggering the error callback instead of success. I'm wondering if the unexpected 302 status code it is receiving could be the cause. After checking out the documentation, a question still lingers: How does jQu ...

Uh-oh, Ajax encountered a 500 Internal Server Error!

Utilizing ajax to fetch events from my database has hit a snag. Instead of displaying the results, there is nothing visible on the screen, and the console is showing this error message: POST http://www.example.com/system/live_filter.php 500 (Internal Se ...

Can you help me figure out what is causing an issue in my code? Any tips on removing a collection from MongoDB

I'm currently facing an issue with deleting a collection from MongoDB using the Postman API. The update function in my code is working perfectly fine, but for some reason, the delete function is not working as expected. It keeps displaying an internal ...

The website functions properly in Chrome, but encounters issues in IE

Currently working on validating some JavaScript code. Chrome seems to be handling it well, but as expected, IE is causing some issues. Take a look at the code below: function validateData(a,id){ var inputs = document.getElementsByName('attname[] ...