Placing image at different locations

When a user clicks, I need this image to smoothly transition to the click location. Using only Javascript results in the image appearing at the new location without any transition effects.

<body>
    <img id="player" src="images/man.png"></img>

<script>
    var x_click;
    var y_click;
    var player = document.getElementById("player");
    document.addEventListener("click", move_player)

    function move_player(e) {
        x_click = e.clientX;
        y_click = e.clientY;

        player.style.left = x_click - player.clientWidth/2 + "px";
        player.style.top = y_click - player.clientHeight/2 + "px";  
    }


</script>
</body>

To add a transition effect, I included the following CSS:

position: relative;
transition: left 0.5s linear, top 0.5s linear;

The issue is that the speed of movement remains constant. Whether moving 300px or 1000px, it consistently takes 0.5 seconds. My goal is for the speed to vary based on distance: covering 1000px should take significantly longer than 300px. How can I accomplish this using pure Javascript, without relying on external libraries like JQuery?

Answer №1

Give this a try:

document.getElementById('your_id').style.animationDuration="1s";

To ensure compatibility across different browsers, consider using prefixes like this:

document.getElementById('your_id').style.webkitTransitionDuration="1s";

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

How come Angular doesn't properly handle array element modifications through filters?

Despite my efforts to debug, I am still unable to pinpoint the mistake I am making in Angular.js. I have been closely following the basic Angular tutorial at https://docs.angularjs.org/tutorial/step_03, where all examples run smoothly. However, I decided ...

Eliminate entry upon checkbox completion from task schedule

Currently working on developing my own to-do list web application. I have set everything up except for one thing. I am trying to figure out how to remove a checkbox from the array when it is checked. Can someone please assist me in achieving this using DOM ...

Standardize API data for utilization in Redux

I have an API that provides the following data: const data = { id: 1, name: 'myboard', columns: [ { id: 1, name: 'col1', cards: [ { id: 1, name: 'card1' }, { id: 2, name: 'card ...

What are the ways to prolong or pause the JSON transformation process in NodeJS?

I'm currently extracting keywords using ExpressJS: /* * GET keywords. */ router.get('/keywords', function(req, res) { // Ensure user is logged in if (req.user) { var db = req.db; var user = req.user; db.col ...

Revealing and concealing adjacent elements within a specified class

In an attempt to create a carousel that functions by hiding and showing images when the next and previous buttons are clicked, I have organized my images in a table and assigned the li elements a class of 'li'. There are four images in total, wit ...

unable to locate the allong.es variadic function

Encountering an Error node.js:201 throw e; // process.nextTick error, or 'error' event on first tick ^ TypeError: undefined is not a function at /home/ubuntu/nodejs/test.js:4:10 at factorial (/home/ubuntu/nodejs/test.js:17: ...

Error: JSON response is returning as undefined

Whenever I attempt to display the height and weight data from an API (), I encounter an issue: Error: TypeError: Cannot read properties of undefined (reading 'metric') If I remove the "items.height.metric", everything runs smoothly, however, I ...

How to select the first column in a jQuery Datatable and turn it into checkboxes

I'm faced with a situation where I need to incorporate a checkbox column in a table, with the checkboxes appearing as Checked or Unchecked based on the values in the first column and its subsequent rows. The challenge lies in dealing with dynamic data ...

Error encountered during Ajax request - two files being transmitted instead of one

Can someone assist me with a basic ajax call for a login button? I need help with the form submission and sending the request to a php file to handle the login action. However, I am encountering an issue where two files are being sent instead of one when ...

jquery ajax request not functioning properly on Firefox browser

This query is following up on this inquiry and this one. I am experiencing difficulties sending form field values through the jquery ajax API. Here is the code snippet: index.html <!DOCTYPE html> <html> <head> <meta content="tex ...

The unzipper will disregard any empty directories within the file

I am a Japanese Web Developer. Unfortunately, I struggle with English. https://www.npmjs.com/package/unzipper This library is currently in use by me. Below you will find my code snippet: // unzip module import fs from 'fs-extra' import unzip ...

Opacity problem when hovering on Chrome

Work in progress: Hello everyone, I'm facing an issue with a hover effect that only seems to be occurring in Chrome. When hovering over a tile, the background color changes, the image opacity decreases, and an icon appears over the image. In Chro ...

Guide on transferring files between Node.js/Express servers from receiving files at Server A to sending files to Server B

Currently, I'm tackling node.js express servers and I've hit a roadblock! Despite my efforts to scour the documentation and other resources, I can't seem to find the right solution. Here's what I need to accomplish: Receive 2-3 PDF ...

javascript problem with class method for loading json file

I've encountered an issue with my class setup below. Despite most things working, the console keeps throwing an error when json.onload is triggered. The error message reads "Uncaught TypeError: Cannot read property of 'push' of undefined". ...

Tips for building a JavaScript promise chain using an array function

I encountered a strange problem while working on a JS promise chain. When using an arrow function with parentheses in the promise, I am not getting the expected value. Instead, it gives me an 'undefined' value in the second 'then'. Her ...

Utilizing getter and setter functions within a setter with a type guard

I need to implement a getter and setter in my class. The setter should accept a querySelector, while the getter is expected to return a new type called pageSections. The challenge I'm facing is that both the getter and setter must have the same argum ...

CSS parsing through regular expressions

I trust this message finds you in good health. I understand that a one-size-fits-all regular expression for CSS may not work in every scenario. However, I am currently analyzing a specific CSS segment and can customize the expression to meet my requiremen ...

Visual Studio debugging: MVC CSS not appearing as expected

There seems to be an issue with the CSS not rendering correctly in my MVC project when I view it on [https://localhost/MyApp]. The buttons and background image are not appearing as they should. It initially worked fine, but then suddenly stopped. I suspect ...

When an HTML input button within a table fails to trigger any jQuery function upon being clicked

I currently have a table set up with multiple rows and columns. <table style="width: 100%;" class='table_result'> <tr><td>Text</td><td>Text</td><td>Text</td><td>Text</td> ...

Warning: Avoid using arrow functions for JSX props in the code-eslint alert

Looking for guidance on how to pass a function as a prop from one component to another without triggering an eslint error: Example Code: <Parent> const doSmthHandler = useCallback((id: number)=> { //do some stuff },[]) <ComponentB> ...