Resizing an image based on the coordinates of a click position by utilizing jQuery

I'm new to experimenting with code, and I've been playing around with trying to shrink an image to nothing at a central point. I've found some success using a mix of the code snippet below and relative positioning in my CSS.

$(function() {
            $('#imageID').on('click', function() {
                $(this).animate({
                    width: 0,
                    height: 0,
                    top: '185px',
                    left: '-2px'
                }, 200);
       
            });
          });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

However, my goal is to modify this so that the image shrinks centered on the location where the click occurred. I've attempted absolute positioning and setting the top/left values to pageX/pageY (even assigning them to variables) but haven't had any luck.

If anyone has insight into how or if this can be achieved, I would greatly appreciate it. Thanks.

Answer №1

Welcome to our community at Stack Overflow!

Have you considered utilizing CSS animations here?

Opt for using transform over width and height in animations for improved performance.


CODE SNIPPET:

$(function() {
  $('#imageID').on('click', function() {
    $(this).addClass("shrink");
  });
});
.shrink {
  animation: shrink 300ms linear forwards;
}
@keyframes shrink {
  to {
    transform: scale(0);
  }
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<img id="imageID" src="http://fillmurray.com/300/300">

For further details on CSS properties, explore transform and animation.

Answer №2

$(document).ready(function() {
            $('#imageID').on('click', function() {
                $(this).animate({
                    width: 0,
                    height: 0,
                    top: '185px',
                    left: '-2px'
                }, 200);
       
            });
          });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

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

HTML is unable to recognize the CSS file. Maven and Spring are the solutions

I'm having some trouble connecting my CSS to my HTML. This is the structure of my folders: Here is my HTML code: I've tried several methods to make it work. Interestingly, everything works fine on index.html. I even tried opening the HTML fi ...

Undefined value is encountered when passing props through the Context API in a REACT application

Exploring My Context API Provider File (Exp file) import react form 'react'; import {createContext} from "react"; export const ContextforFile = createContext(); export function ContextData(props){ let rdata=props.data return( &l ...

Setting up the data for the AjaxAppender Request Log4Javascript

I am completely new to Log4Javascript and the concept of logging, so please bear with me. My current task involves sending logs to a CouchDB server using a POST request. However, I keep encountering an error from the server: {"error":"bad_request","reaso ...

There was an unexpected error in angular.js while trying to work on a calendar widget based on angular-ui-calendar. The error message indicated that the argument 'fn' was expected to be a function but instead received Moment

I came across a similar topic earlier on Stack Overflow, but unfortunately, the issue wasn't resolved. So, I've decided to revisit this question and address it myself this time. In my app.js file, which is where my main module for the app is ini ...

Struggling to make `align-items="baseline"` function properly

I'm encountering an issue with alignment in my sandbox project. Specifically, I want the bottom of texts with different font sizes to be on the same y axis but I can't seem to figure out how to make it happen. Check out this picture to see exact ...

What could be causing the issue with my css `content:` not functioning across different browsers, and how can I enhance cross-browser compatibility in all cases

Working on my first HTML/CSS project, I encountered a browser compatibility challenge. The code functions well in Firefox, IE, and Edge but fails in Chrome. Specifically, I am trying to make a button display alternating up and down arrows when clicked. Whi ...

Various tasks to be executed on a shared element

Struggling with implementing actions on a grid component without using a router in Next.js. Here is the current code snippet: const handleActions = (btnPress, row) => { if (btnPress === "Add") {/* implementation */} else if (btnPr ...

Generating a downloadable picture with jQuery freeze frame

If you're looking to add animation to a gif upon mouseover, the Freezeframe plugin created by Chris Antonellis is the perfect solution. Simply use the command below: < img src="image.gif" freezeframe /> For a live example, visit his website he ...

Upload the PDF file along with other form data in a React application

I'm currently facing an issue with using formData for submitting a pdf file const [file,setFile] = useState() const [isFilePicked, setIsFilePicked] = useState(false); I have an input of type file <input accept=".pdf" type="file&quo ...

Techniques for dynamically counting rows in a table using JavaScript

I'm working on a system to create and delete rows, and I want each row to have a unique row number displayed under "Num." However, I'm having trouble implementing this feature. EDIT I found a jQuery snippet that counts the first row but not t ...

Tips for aligning bootstrap labels in one line while allowing horizontal scrolling within a column in Bootstrap

Check out this code snippet here. <div class="row"> <div class="col-xs-6"> <div class="input-group"> <div type="text" class="form-control"> <span class="label label-default">Default</span> &l ...

Ways to retrieve the previously selected option in a dropdown list

I am working with a form that includes a dropdown list featuring options such as apple, gova, lemon, and more. Each option has an associated counter - for example, apple=5, gova=3, lemon=6. If I initially select apple from the dropdown, the count for apple ...

What is the process for creating a rollover effect with png images and jquery?

I managed to reverse engineer a script that I came across online and it seems to be functioning well. The only issue I'm facing is that the hover image appears with full visibility before any hover action is taken place. Here's the script snippet ...

Tips for populating countryList data in Form.Select component within a React.js application

I have a data file that contains a list of all countries, and I need to display these countries in a select input field, similar to what you see on popular websites when a user logs in and edits their profile information like name, address, and country. H ...

Make a copy of an array and modify the original in a different way

Apologies for my poor English, I will do my best to be clear. :) I am working with a 3-dimensional array which is basically an array of 2-dimensional arrays. My task is to take one of these 2-dimensional arrays and rotate it 90° counterclockwise. Here is ...

Ensure the div element remains fixed at the top of the page

I created a script to identify when I reach the navigation bar div element and then change its CSS to have a fixed position at the top. However, I am encountering an issue where instead of staying fixed, it jumps back to the beginning of the screen and fli ...

Click on the table row image using jQuery when the checkbox is selected

I am facing a challenge with my ASP Gridview. In one column, there is an img tag, and in the next column, there is a checkbox. My goal is to trigger the click event that I have assigned to the img when the user checks the checkbox, but only if the src of t ...

Touchwipe incorporation - single-page website script

Today has been dedicated to troubleshooting and searching for a solution regarding the integration of TouchWipe () on a custom one-page-site script based on Parallax that I found perfect for my latest project (). The script itself performs beautifully wit ...

Progressive zoom effect applied to an image in a JavaScript slideshow

Can someone assist me in replicating the zooming effect demonstrated in this code snippet: `http://codepen.io/docode/pen/EjyVQY`. I am specifically focusing on the zooming feature of this slideshow. How can I replicate this zooming effect in JS rather t ...

Applying custom styles to the initial 5 elements post clicking the button with Jquery

I have a set of HTML codes containing multiple buttons. <div class="main"> <div class="button hide">1</div> <div class="button hide">2</div> <div class="button hide">3</div> <div class="button h ...