JQuery's animations and their influence on element positioning

My thumbnail animation increases in size on hover, but it is affecting the position of the other images.

The issue is that when hovering over an image, the others move down and to the right to make space for the animated one. I want the other images to stay in place while one is being animated.

$num and the for loop are used to pull images from a MySQL table, please ignore

<html>
<?php
for($i; $i < $num; $i++){
echo "<img class='img' id='img" . $i . "' />";
}
?>

<script>
$('.img').hover(
        function(){
            $(this).animate({
                width: "+=14",
                height: "+=14",
                left: "-7",
                bottom: "-7"
            });
        }, function(){
            $(this).animate({
                width: "-=14",
                height: "-=14",
                left: "0",
                bottom: "0"
            });
        }
    );
</script>
<css>
.img{
    padding: 10px;
    position: relative;
    cursor: pointer;
}
</html

Answer №1

If your design lacks padding, SpaceDog's suggested solution may not be effective. In such cases, the following approach could yield better results:

$(function( ){
    $(window).ready(function( ){
        $('.img').each(function( ){
            $(this).css({'top': $(this).offset( ).top, 'left': $(this).offset( ).left});
        });
        $('.img').css({'position': 'absolute', 'z-index': 1});
    });

    $('.img').hover(
        function(){
            $(this).css('z-index', 10);
            $(this).animate({
                width: "+=14",
                height: "+=14",
                left: "-=7",
                top: "+=7"
            });
        }, function(){
            $(this).css('z-index', 1);
            $(this).animate({
                width: "-=14",
                height: "-=14",
                left: "+=7",
                top: "-=7"
            });
        }
    );
});

You can view a demonstration on JSFiddle

This script essentially recalculates the position of each image and adjusts it to absolute positioning without disturbing neighboring elements. It then animates the images in a manner similar to your original intent.

While SpaceDog's recommendation is suitable with sufficient padding, this alternative method offers improved performance for scenarios without ample padding.

Answer №2

To achieve the desired effect, you can modify the padding within the animation instead of adjusting the left/bottom position. Take a look at this code snippet:

$('.img').hover(
        function(){
            $(this).animate({
                width: "+=14",
                height: "+=14",
                padding: "-=7"
            });
        }, function(){
            $(this).animate({
                width: "-=14",
                height: "-=14",
                padding: "+=7"
            });
        }
    );

Check out this live demo for reference.

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

To see website changes, refreshing with Ctrl + F5 is necessary in the browser

I have a simple website that uses only HTML and CSS. Whenever I upload new files to the site, they do not appear in the browser until I perform a hard refresh by pressing CTRL + F5. Does anyone have any suggestions on how to fix this issue? ...

How can I efficiently create an editForm in Angular?

Within my database, there are numerous users, each with their own collection of recipes. Each recipe contains various properties and a list of ingredients. Take a look at the screenshot below: Recipe with all properties When a user goes to edit a recipe ...

Is there a way to access the scrolling element on the current webpage?

When the route changes, I need to locate the element with scrolling functionality on the new page and scroll it to the top using window.scrollTo(0,0). How can I achieve this? Here is my current code snippet: if (process.client) { router.afterEach((to, ...

Chip input component in React

I've recently upgraded to React 17 and encountered an issue with chip input fields like material-ui-chip-input not working properly. I've tried various npm packages, but none of them seem to be compatible with React version 17. Does anyone know ...

Can you outline the distinctions between React Native and React?

Recently delving into the world of React sparked my curiosity, leading me to wonder about the distinctions between React and React Native. Despite scouring Google for answers, I came up short on finding a comprehensive explanation. Both React and React N ...

Looking to display a page header alongside an image on the same page

I'm currently learning React.js and working on my very first app. As someone new to frontend development, I am aiming to have a header design similar to that of popular websites like StackOverflow or YouTube, where an image or icon is positioned just ...

Retrieve a specific value from an array within Firestore

I am facing an issue where I can only retrieve the values I need from the array by adding a specific string like "اقلام" or "سبورة". However, I want the value to be passed as a prop from another component or screen. Is there a way to resolve this ...

Issue with starting tubular jquery video on Safari 7 (mac)

I'm currently utilizing the tubular plugin () to generate a subtle HTML video background. Everything is functioning smoothly in Chrome, however, when attempting to run it on Safari 7 for Mac, the video fails to start. After some investigation, I&apos ...

What is the best way to flip the direction of the text input for a Calculator?

I am currently working on creating a basic calculator from scratch using HTML, CSS, and JavaScript. I have been trying to figure out how to align the numbers to the right side of the input element. Can anyone provide me with guidance on how to achieve thi ...

Challenges Encountered When Inputting Year in ReactJS Date Picker Component

I've encountered a problem with a date input component in my ReactJS project and need some assistance with resolving two issues: Problem 1: Year Input Length The first issue is that the year input field allows six digits, but I want to restrict it to ...

Using the goBack function in React Router does not add the previous location to the stack

In React Router v4, I have a list page and a details page in my web application. I want to implement a 'Save and close' button on the details page that redirects the user back to the list page when clicked. However, I noticed that after the user ...

Transmitting several images via the HTML INPUT tag through AJAX to a Laravel Controller, in addition to sending other data not contained within the

$('#update-post').on('click', function(event){ $.ajax({ method: 'POST', url: editPostUrl, data: { content: $('#post-content').val(), deleteImages: deleteImages, postId: postID, ...

What is the best way to transfer static assets from an npm package to a nuxt project?

Has anyone successfully sent assets from an npm package to a nuxt application before? I have a vue component within an npm package with the following structure: -src/ -assets/ -noun-filter.svg In this npm package, the vector image is included in the v ...

Managing additional components in request JSON when communicating with DialogFlow, previously known as Api.ai

My current challenge involves enhancing the information sent in a JSON request from my application to DialogFlow. While I am familiar with triggering events to send data calling an intent through the method described in Sending Parameters in a Query Reques ...

Can a secondary non-static, privileged function be implemented in a jQuery Plugin?

Most of the jQuery tutorials I've encountered tend to focus on using a single main public function for their selection plugins. By 'selection' plugin, I am referring to one that is more complex than just a static function added to jQuery. F ...

Utilizing jQuery in non-React JavaScript with Rails- A Guide

I've recently integrated The react_on_rails (NOT react-rails) Gem into my Rails 4.2 project. I am looking to add new React components to an application that already contains a substantial amount of JavaScript code. After running the "hello world" ge ...

What steps can I take to ensure the proper formatting of the `select` input on Mac OS?

How can I maintain consistent formatting for my form across different operating systems? Currently, the form looks good on Windows, but I want to ensure that the select input appears consistently on Mac OS, iOS, and Android devices as well. The image bel ...

Looking to place a global filter outside the primeNG table component?

I am currently utilizing primeNG in my project and I have a need to incorporate a global filter. The challenge I am facing is that I must add this filter in a different component. These two components are deeply nested within other components. My approach ...

"Troubleshooting: Fixing the issue with jQuery datepicker not

After implementing the jQuery Datepicker on a field, I noticed that even though assigning a value to the field shows in Chrome's developer tools... https://i.sstatic.net/We7Ua.png Unfortunately, the assigned value does not show on the front end. I ...

Column content merging in BS layout

Currently, I am exploring how to implement a two-column layout with the left column serving as a scrollable navbar. At this early stage of development, please excuse any imperfections in the code! One issue that arose involved using the .sidebar class wit ...