Fixing the Jquery animation glitch triggered by mouseover and mouseout

In my project, I have implemented a small mouseover and mouseout functionality. The challenge I am facing is that I need to keep the mouseout function using animate() instead of css() for specific reasons.

The issue arises when I quickly do a mouseover followed by a mouseout while the opacity animation from 1 to 0 is still in progress, which is commonly done during testing purposes.

I attempted to use setTimeOut as well to ensure that the opacity is set to zero after a certain period of time.

However, both the animate and setTimeOut methods are causing a problem where after triggering the mouseover function and updating the opacity to 1, the ongoing animations reset the opacity back to zero because they are still active. JSFIDDLE Jquery Code:

$("#dp-ashish").on("mouseover",function(){
    $("#dp-ashish").css("opacity","1");
});
$("#dp-ashish").on("mouseout",function(){
    $("#dp-ashish").animate({"opacity":"0"},1000);          
});

Answer №1

If you're looking to achieve a smooth transition effect, you may want to consider using either .stop(true, true) or .finish() (the latter exclusively compatible with jQuery v1.9 and above):

$("#dp-ashish").on("mouseover",function(){
    $("#dp-ashish").stop(true,true).animate({opacity:1},1000);
});
$("#dp-ashish").on("mouseout",function(){
    $("#dp-ashish").stop(true,true).animate({opacity:0},1000);          
});

or:

$("#dp-ashish").on("mouseover",function(){
    $("#dp-ashish").finish().animate({opacity:1},1000);
});
$("#dp-ashish").on("mouseout",function(){
    $("#dp-ashish").finish().animate({opacity:0},1000);          
});

Just a reminder that the opacity property does not require quotation marks for its numerical integer value. It's a valid property in CSS and should be used without being parsed as a string.

On another note, you can utilize CSS transitions by toggling a specific class to handle opacity changes smoothly :)

Explore this example on JSFiddle for additional insights

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 jQuery AJAX with a plethora of dynamically created div elements

Within my JSP file, I have a series of divs that are created dynamically based on a database value. These divs have ids such as event1, event2, and so on. Let's focus on the div with the id event1. Inside this div, there is a button with the id foll1 ...

Dotted JavaScript property within an object

Having trouble sorting an array of objects using the lodash orderBy function because it doesn't work when the iteratees contain a dot in the middle. For a better understanding of the issue, check out this plunker. plunker ar users = [ { 'us ...

The equivalent of the $.curCSS method in jQuery version 1.10 is as follows:

While working with a library called mcdropdown from http://www.givainc.com/labs/, I encountered an issue with the $.curCSS method. The latest version of jQuery no longer supports this method and suggests using $().css instead. Following the documentation, ...

My goal is to create text that is opaque against a backdrop of transparency

Is it feasible using CSS to create text with a transparent background while keeping the text opaque? I attempted the following method: p { position: absolute; background-color: blue; filter: alpha(opacity=60); opacity: 0.6; } span { color: b ...

Improving conditional rendering in Mui <Cards> component by fixing state behavior

I have a situation where I want to display a Floating Action Button inside each Mui card when hovered over. However, I'm running into an issue with the hover state affecting all cards instead of just the one being interacted with. How can I ensure tha ...

During the serialization process of a System.Reflection object, a circular reference was identified

One of my asp.net MVC 3 controller action methods looks like this: public JsonResult GetRecordingRates(int Id) { List<DefaultRateChart> defaultRateCharts = new List<DefaultRateChart>(); using (IDefaultRateChartManager defau ...

What could be the reason for jQuery not functioning properly as needed?

function toggleDisplayingRooms(nameSelect){ if(nameSelect){ firstroom = document.getElementById("firstroom").value; secondroom = document.getElementById("secondroom").value; thirdroom = ...

Utilizing Angular JS to Manage Controller Events

I am currently working on an application that requires saving a large amount of data in cascade, similar to a typical master-detail view. Within this view, there is a "Save All" Button which saves each row in an iteration. This process triggers jQuery cus ...

Angular js is a powerful framework that allows for the seamless transition of views, except for the home

I am currently using the ng-animate module to implement sliding effects for my app views. Each route has its own unique slide animation. Take a look at my simple code below: HTML: <div ng-view ng-animate class="slide"></div> CSS: /*Animatio ...

The image will come to life with animation as the background position is adjusted using Skrollr

Trying to create a div that switches the background image every X pixels scrolled. Initially experimented with changing the background-image, including using sprites and adjusting background position, but encountered a distracting "flickering" effect. Exa ...

What could be causing the input submit in my form to be unresponsive when clicked?

I'm stumped on what's causing the issue. My sign-up form seems to be malfunctioning - when I click the "Complete Registration" button, nothing happens! The form doesn't even submit. Here is the HTML structure: <!DOCTYPE html> <html ...

"Using conditional statements to check for specific value ranges and properly handling cases where the result is undefined

Currently, I am working on a function that prompts the user to input a number and then displays a calculated score after they click a button. The score is based on the value entered by the user. While constructing this feature, I have pondered whether IF ...

transferring information to d3.js

I have a json object structured in the following way { "tweet":[ {"text": "hello world"}, {"text": "hello world"} ] } When I check the console output of "data" in my code below, it shows me an Object tweet: Array[131]. However, when I l ...

Tips for integrating and utilizing the MSAL (Microsoft Authentication Library for JavaScript) effectively in a TypeScript-based React single page application

Issue I'm encountering difficulties importing the MSAL library into my TypeScript code. I am using the MSAL for JS library with typings in a basic TypeScript/React project created using create-react-app with react-typescript scripts. As someone who i ...

How to eliminate an item from an array using index in JavaScript with jQuery

In order to remove a specific array added in an object by index, I would like the functionality where when a user clicks on a button, it removes the corresponding array. Here's what I have in mind: HTML: <button>1</button> <button>2 ...

The handleClose() function in React is currently writing to the console but failing to close the child element

Currently, I am in the process of creating a small online store for my personal business. Although I have limited experience with React, I believe I have managed to make some progress and might be able to complete something that is at least functional, eve ...

Exploring the possibilities of integrating jQuery into Angular 2 using Javascript

import {Component, ElementRef, OnInit} from 'angular2/core'; declare var jQuery:any; @Component({ selector: 'jquery-integration', templateUrl: './components/jquery-integration/jquery-integration.html' } ...

Retrieve various forms of information using Ajax

Currently, I am working on developing a social networking website similar to Facebook using PHP. On one of the pages titled showdetails.php, I have implemented a search bar that should display a dropdown list of user names from the database as the user typ ...

Sliding Feature

Can someone assist me with implementing a jQuery half slide function from left to right? Please check out the following URL for more information: http://jsfiddle.net/prabunivas/Fy9Hs/2/ <div> <div id="col1" style="float:left"> &l ...

sequencing the compilation of Node.js modules

I am facing an issue with my node application involving multiple interdependent modules exported using module.exports. These modules include mongohelper, transaction, server, conhandlr, and appmin. Compile order- mongohelper transaction server (..the ...