Utilizing jQuery's scaling function allows for exponential growth in size

Currently, I have a function implemented that scales up pictures on mouse-enter and returns them to normal size on mouse-out. However, there is an issue where if I quickly move the mouse out and then back in before the picture has returned to its original size, the image will continue to scale up (x2.7) from whatever size it was at the moment of mouse-enter, resulting in a huge image that does not return to its original size. How can I prevent the function from running until the image has fully returned to its original size? Check out vgtesting.co.nf (under the dog walking tab) to see what I am attempting to accomplish.

//to scale up on hover
$('#videoandmapwrap').on({
    mouseenter: function () {
        current_h = $(this, 'img')[0].height;
        current_w = $(this, 'img')[0].width;
        $(this).stop(true, false).animate({
            width: (current_w * 2.7),
            height: (current_h * 2.7)
        }, 900);
    },
    mouseleave: function () {
        $(this).stop(true, false).animate({
            width: current_w + 'px',
            height: current_h + 'px'
        }, 400);
    }
}, 'img');

Answer №1

Enjoyed reading your comment...

Your code appears to be correct, but there is one issue with the two variables. It seems that they have not been defined, which can restrict their scope to the mouseenter function only. To ensure they are accessible globally, try defining the value globally.

var current_h,current_w; //Simply add this line.
$('#videoandmapwrap').on({
    mouseenter: function () {
       //... your code
    }
}, 'img');

For example, you can refer to this JSFiddle link

Answer №2

The issue you are facing is a race condition caused by multiple event handlers conflicting with each other. A possible solution to this problem would be to save the initial image size in the DOM during the first mouseenter event, and consistently use this stored size for scaling instead of relying on the current size of the image.

Answer №3

To resize an image, you can use a flag method. Set the flag to true when the mouse enters the area of the picture and set it to false when the mouse leaves. Then adjust the size of the picture based on the flag status. Hopefully, this explanation is helpful.

var resizingFlag = true;
// inside the mouseenter function
if(!resizingFlag) return;
resizingFlag = !resizingFlag;

// then in the mouseleave function
resizingFlag = !resizingFlag;

Hopefully, this solution will suffice as an answer.

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 Definitions for File Upload in Typescript

I'm currently working with TypeScript and facing an issue with a file upload form. However, when I try to access the files from the input element in my TypeScript code, an error is generated. $('body').on('change', '#upload_b ...

JavaScript - filter out values not included in specified list of attributes

I am seeking a technique that, when provided with a list of attributes, retains only the values associated with keys present in the list. For instance: attrs = ['a', 'b', 'c'] obj = {'a': 1, 'b': 2, &apos ...

React-Redux error: Unable to call addItem function

Recently, I started learning about redux by following an E-Commerce site tutorial that uses React and Redux. In the tutorial, there is a CollectionItem Component with a button that triggers an addItem function to add the selected item to the shopping cart. ...

What could be causing setTimeout to trigger ahead of schedule?

I am struggling with a piece of node.js code like this: var start = Date.now(); setTimeout(function() { console.log(Date.now() - start); for (var i = 0; i < 100000; i++) { } }, 1000); setTimeout(function() { console.log(Date.now() - s ...

Creating beautifully centered rows of three images dynamically

Seeking a way to dynamically generate centered rows of three images based on the quantity being loaded. The goal is to have one row for three images, two rows for four images, and four rows for ten images. Both the images and their container div are fixed ...

Can a layer be sliced to create a text-shaped cutout?

I want to achieve a cool text effect where the background is visible through the letters. I have explored options with background images and colors, but I haven't found any examples where the underlying layer is revealed. Is this even possible? Imag ...

Storing JSON content in a MySQL database with PHP

I am developing a web application using Leap Motion technology with PHP and JavaScript. This app focuses on gesture recognition, where the values captured by Leap Motion need to be stored in a database. When a recognized gesture is matched, its correspondi ...

Exporting variables in node.js allows you to share data between different

Hi, I'm currently working with a file that looks like this: module.exports = { some variables that are being exported here, execute(message) { } } I want to export the message parameter to another file. I've attempted using var msg ...

Should I increase the number of followers, or opt for an aggregated method to monitor them?

My system loads products using an infinite scroll, displaying 12 at a time. Sometimes, I may want to sort these products based on the number of followers they have. Here's how I keep track of the followers for each product: The follows are stored i ...

The plugin that simplifies adding and removing form fields in jQuery

I am in search of a quality plugin that can easily add or remove a set of form fields on a webpage. I came across this one , which seems to fit my requirements. However, I am exploring other options to see if there is an even better plugin available. Ther ...

Is it possible to customize the formatting of the information displayed within a details tag when it is nested under a summary

Is there a way to format the details information so that it aligns underneath the summary tag? Currently, both lines of text are aligned to the left. <details> <summary> Summary 1 </summary> Details 1 </details> ...

Trouble with modifying style in Google Chrome and Internet Explorer prior to AJAX request

When utilizing AJAX, I have a common practice of setting up a loading indicator before each request to inform the user about the short wait. Usually, this is achieved by adding an animated loading gif. Interestingly, when performing this action in Firefox, ...

What are the benefits of using one state in React with useState compared to having multiple states?

Is it more beneficial to optimize and enhance code readability in React using Hooks and Functional components by utilizing a single setState hook or having multiple hooks per component? To further elaborate, let's consider the following: When workin ...

Use jQuery eq() to populate a group of text fields with data stored in localStorage

In my project, I have a series of text areas that are configured to save their content in localStorage for persistence. As I attempt to retrieve and reassign these values when the browser reloads, I encounter an issue with the following code snippet. When ...

JS animation triumphant

I have developed an app that utilizes a checkbox to control the appearance of an overlay on a screen. To ensure smooth transitions, I have incorporated animations into the process. #overlay{ position:absolute; height: 100vh; width: 100vw; ...

Is it possible to use coding to determine if a user has posted a link on Facebook?

For tracking Facebook shares on my site, I am currently utilizing jQuery to register each click on the share link. However, I am seeking a more precise method. Rather than just tracking clicks, I want to track the actual "shares". Is there a way to recei ...

Bringing in More Blog Posts with VueJS

Exploring the Wordpress API and devising a fresh blog system. As a newbie to VueJS, I'm intrigued by how this is handled. The initial blog posts load as follows: let blogApiURL = 'https://element5.wpengine.com/wp-json/wp/v2/posts?_embed&p ...

Is there a way to determine if a Dojo dialog has been successfully loaded on the page?

I have a function that needs to close a Dojo dialog if it is currently open. How can I determine if a dojo dialog is active? Should I rely on pure JavaScript and check for its existence by ID? if (dijit.byId("blah") !== undefined) { destroyRecursive ...

How to Modify a File Inside a Compressed File Using JSZip

Is it possible to modify a file within a zipped file using JSZip? I have searched for answers and reviewed the API, but I am unable to find a solution. Any assistance on this matter would be highly appreciated. Thank you in advance! ...

Issue encountered: An unexpected token = error appeared after updating from RN version 0.64.2 to 0.65.1

After upgrading from version 64.1 to 65.1 using the upgrade helper, I encountered an error that has been difficult to resolve. Even after updating node/npm to version 14.17 and trying various other solutions, I have not been able to fix this issue during ...