Tips for implementing fluid transitions between mouse X and Y coordinates using JavaScript

I recently developed a function that enables a DOM element to follow the mouse cursor. You can check out the code here.

Currently, I am looking for suggestions on how to add a nice animation to this feature. Ideally, I want to incorporate a slight delay with smooth easing transitions. I attempted to use transition: all 0.1s ease; but it didn't provide the level of smoothness I desire. I've heard about something called cubic, but I'm unsure if that's what I need.

I believe there should be a function that eases the mouse X and Y coordinates with a gentle delay for added smoothness.

The part of the code responsible for handling mouse movement is:

function mouseMove(event) {
  var target = $(this);
  var dot = target.find('.pointer');

  var height = dot.height();
  var width = dot.width();

  var offset = target.offset();
  var w = target.width();
  var h = target.height();
  var top = offset.top;
  var left = offset.left;

  var mX = (event.clientX - left) - width / 2 - 15; // 15 = padding
  var mY = (event.clientY - top) - height / 2;
    
  $(dot).css('-webkit-transform', 'translate3d(' + mX + 'px , ' + mY + 'px, 0) scale(1, 1)');

};

Pointer CSS:

.pointer {  
  position: absolute;
  z-index: 1;
  width: 25%;
  position: relative;
  border-radius: 50%;
  box-shadow: 0 5px 3px rgba(0, 0, 0, .1);
      transform: translateZ(0) scale(0);
      -webkit-transform: translateZ(0) scale(0);
}

.pointer:before {
  content: "";
  display: block;
  padding-top: 100%;
}

I would greatly appreciate any advice, resources, or snippets of code that could help enhance this functionality.

Additionally, I am looking to smoothly animate the scale property of the pointer class. Ideally, I want the pointer to scale up to 1 when the mouse enters the thumbnail area and scale back down to zero when it exits.

Answer №1

Here is a summary of my process:-)

*{  -webkit-transition:1s;
transition:1s;
-webkit-transition-timing-function: cubic-bezier(0.42,0,0.58,1);
transition-timing-function: cubic-bezier(0.42,0,0.58,1);}

Cheers

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

What is the best way to add spacing between the fields within a Bootstrap 4 form row that spans across 2 rows when viewed on small displays?

Trying to create a bootstrap 4 form with fields in one row for larger screens and two rows for smaller screens. The attempt was made using the following code: <div class="form-group row"> <label class="col-sm-2 col-form-label">File:</lab ...

What could be causing the issue: Unable to locate or read the file: ./styles-variables?

I'm currently following a tutorial on how to create responsive layouts with Bootstrap 4 and Angular 6. You can find the tutorial here. I've reached a point where I need to import styles-variables.scss in my styles file, but I keep encountering t ...

How can I retrieve the array data that was sent as a Promise?

I have a database backend connected to mongoDB using mongoose. There is a controller that sends user data in a specific format: const db = require("../../auth/models"); const User = db.user const addProduct = (req, res) => { User.findOne({ ...

Trouble with the JQuery event listener onchange()

On my webpage, I've set up a drop-down list and a text-box in the following HTML code: <table> <tr> <td>Group Name: </td> <td><%= Html.Dr ...

Tips for launching different web browsers via hyperlinks?

My app has a link that I want mobile users from apps like LinkedIn to open in a browser such as Safari. I attempted this: <a href="safari-https://meed.audiencevideo.com">May open on Safari</a>' However, when I click the link, it opens i ...

Segment will expand completely upon inserting a new division

Lately, I've been trying to simplify things by consolidating my JavaScript functions into one file instead of using multiple separate files. The idea was to merge all the functions together and wrap each one in its own function so that they can be eas ...

Displaying and concealing table rows based on selected items

After spending a whole day trying to get this HTML/java script to work properly, I came across some code online that I used here. My goal is to have the "Colors*" row not displayed when the page loads, but to show the color options when a shirt size is sel ...

d3: It appears that my routes are replicating themselves, and I am unable to ascertain the cause

I've been diving deep into D3, studying the works of Mike Bostock and other experts in the field. I'm also going through Scott Murray's book on Interactive Data Visualization specifically focusing on D3. At the moment, my project involves c ...

The removal of classList.remove() only eliminates the class itself, not its contents

My goal is to add one class and remove another class when the start quiz button is clicked. While the 'info_box' class is successfully added, the 'start_btn' class does not get removed; it just changes position (from flex to no flex). T ...

Is it possible to incorporate conditionals within a jade template using JavaScript?

I've been working on a Jade template that includes some logic, but I seem to be encountering an issue. Here's the code snippet: #container -for(var col=0; col < 2 ;col++){ - if(col % 4 == 0){ .movie_row - } ...

Load content from a remote page using AJAX into a JavaScript variable

Looking to retrieve a short string from a server without direct access to the data in XML or JSON format. Utilizing either .load or .ajax for this purpose, with the intention of parsing the data into a JavaScript array. The target page contains only text c ...

What is the best way to resume a paused animation using JavaScript?

My first game involves triggering an animation that transitions the game screen to greyscale when the character dies. Despite my efforts, I have been unable to successfully trigger this animation using document.getElementById("object").animationP ...

What changes can I make to this jquery code to utilize a background image instead of a background color?

Can someone help me modify this snippet to set a background-image instead of changing the background color? <script type="text/javascript"> $(document).ready(function(){ $("button").click(function(){ $("#button_layer").hide(); $("#im ...

How can I ensure that Redux-saga waits for API calls to resolve instead of returning promises continuously? Is there a way to make "yield call" wait for API calls to complete?

Where I'm initiating the API request: function fetchCharacter(value){ return axios.get(`https://www.breakingbadapi.com/api/characters?name=${value}`) .then(res=>{ console.log(res.data) }) .cat ...

Using jQuery to gently fade out text at the top and bottom of the page as you scroll

I am seeking a way to fade out the content of my page with an opacity/rgba color effect as it approaches a specific distance from both the top and bottom of the viewport. This is the desired outcome I want: In the example above, there is a gradient posit ...

Issues with Jquery Ajax POST request not resolving

Can you explain why the success code is not being executed in this request? $(document).ready(function(){ var post_data = []; $('.trade_window').load('signals.php?action=init'); setInterval(function(){ ...

Add a custom filter to the active route for a stylish look

I am trying to dynamically change the color of elements in my navbar by creating a filter in my typescript code. I have a string with values like 'greyscale(73%) saturate(1400%)'. How can I apply this string to the fa-icon's filter property ...

Ways to display the modal once the user initiates the action

Is there a way to delay loading my modal HTML codes until after the user clicks a button, rather than having them load automatically with the template? HTML <!-- Template Codes--> <button data-toggle="modal" data-target="#modal-content" type="bu ...

What is the best way to retrieve data from multi-dimensional JSON structures?

Looking to extract specific values from my JSON file. console.log( objects.assignments.header.report_type ); I need to display HOMEWORK Javascript $.ajax({ url: "/BIM/rest/report/assignment", type: "POST", dataTyp ...

Generate an array in JavaScript using the values from input fields

Is there a way to create a JavaScript array that stores the values of all input fields with the class 'agency_field', when there are x number of these fields in the form? I attempted to achieve this using jQuery, but encountered a syntax error. ...