Make the cursor move in sync with an already moving element

I'm trying to create an element with a breathing animation effect that also follows the cursor. However, I'm running into an issue where the code only works if I remove the animation in CSS.

What am I doing wrong?

It seems like your post is mostly code, can you please provide more details?

class FollowCursor {
  constructor(element, window) {
    this.element = element;
    this.w = window;
    this.w.onmousemove = this.move.bind(this);
  }

  move(e) {
    this.element.setAttribute(
      "style",
      `transform: translate(${e.clientX}px, ${e.clientY}px);`
    );
  }
}

const el = document.querySelector(".el")

const cursorFollowing = new FollowCursor(el, window)
 .el {
   position: absolute;
   animation: a-breathing 8s infinite forwards;
   left: 0;
   top: 0;
   width: 30px;
   height: 30px;
   background-color: yellow;
 }

 div {
   position: relative
 }

 @keyframes a-breathing {
   0% {
     transform: scale(1);
   }

   60% {
     transform: scale(1.8);
   }

   100% {
     transform: scale(1);
   }
 }

<div>
<div class="el">
</div>
</div>

Check out the jsfiddle snippet here

Answer №1

If you make this adjustment, I believe the desired outcome will be achieved:

transform: translate(${e.clientX}px, ${e.clientY}px);

Changing it to:

position: absolute; top: ${e.clientY}px; left: ${e.clientX}px

Example: https://jsfiddle.net/cxszqdka/

Answer №2

If you find yourself overwriting the transform attribute, remember that only one can be used per element. To have elements move around and perform other actions like breathing, you need additional div elements. Here's a simple solution to achieve this:

class FollowCursor {
  constructor(element, window) {
    this.element = element;
    this.w = window;
    this.w.onmousemove = this.move.bind(this);
  }

  move(e) {
    this.element.setAttribute(
      "style",
      `transform: translate(${e.clientX}px, ${e.clientY}px);`
    );
  }
}

const el = document.querySelector(".el")

const cursorFollowing = new FollowCursor(el, window)
.el {
   position: absolute;
   left: 0;
   top: 0;
   width: 30px;
   height: 30px;
   background-color: yellow;
 }
.breathing-element {
  animation: a-breathing 8s infinite forwards;
  width: 100%;
  height: 100%;
  background: red;
}
 div {
   position: relative
 }

 @keyframes a-breathing {
   0% {
     transform: scale(1);
   }

   60% {
     transform: scale(1.8);
   }

   100% {
     transform: scale(1);
   }
 }
<div>
<div class="el">
  <div class="breathing-element"></div>
</div>
</div>

To add multiple values for the transform attribute, use this pattern as a guide:

transform: translate | transform | scale | rotate

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 to implement and utilize a history-object interface in React with Typescript?

Can you help me with setting up an interface for a history object in my component? Currently, it is typed as any and I want to type it appropriately. Object: https://i.sstatic.net/Sru8R.png Here's the code snippet: import React, { useState } from &a ...

CSS for a Div with a Subtle Curve at the Bottom

Is there a way to achieve this specific shape using CSS? I attempted to use the following resources, but they didn't provide me with the desired outcome. Curved border with stroke in pure CSS? http://jsfiddle.net/ECHWb/ .banner-img{ height: 661p ...

Maintain the tab order for elements even when they are hidden

Check out this demonstration: http://jsfiddle.net/gLq2b/ <input value="0" /> <input id="test" value="1" /> <input value="2" /> By pressing the TAB key, it will cycle through the inputs in order. If an input is hidden when focused, press ...

Setting up Highcharts version 7 heatmaps with npm in an Angular 6 environment

I am currently having an issue with initializing the heatmap lib in Highcharts 7.0.1 within my Angular 6 app via npm. Despite successfully running basic charts like line, spline, bar, etc., when following the documentation for the heatmap initialization, I ...

Using AngularJS to iterate through JSON data

I received JSON data from my Facebook account and saved it in a file called facebook.json. Next step is to retrieve and process the data from the JSON file in my controller. app.controller('Ctrl', function($scope, $http) { $http({metho ...

Website layout looking unique due to pixel width adaptation from standard width

I'm currently working on a website with full width design. On my computer, the website displays as full width, but on other computers it shows extra space on the sides. How can I ensure that this website is truly full width? .wrapper_boxed { wid ...

How to Extract Minutes in Datatables Timestamps and Apply Custom Styling

Working with Datatables v1.10 Right now, my table is showing a date in the second column in the format 17-04-2019 14:34, which is how it's stored in the database. The filtering and searching functionality are all working as expected. The current HTM ...

Tips for saving the latest counter value in CSS and showcasing it as content in HTML

Here is an example of how my CSS code is structured: .page-number:after { counter-increment: page; } If we were to display 6 pages, it would look something like this: .page-number:after { content: 'Page ' counter(page) ' of'; } ...

Using Angular to create dynamic CSS animations

Hey there! I'm currently working on developing a sleek horizontal menu that may have elements extending beyond the window's x-axis. The navigation through the menu is managed by utilizing arrow keys within an Angular controller. My goal is to in ...

Using jQuery to locate the dimensions of an image and then adjusting the height and width of a div using

I'm currently working on a project where I need jQuery to determine the size of each image (approximately 8-10 images per page) within an Owl Carousel. However, every time I check in the developer tools, all I see is width: 0px, height: 0px Here&apos ...

A different approach to splitting strings

After receiving a GET request, I am presented with a string that has the following format: <?xml version="1.0" encoding="utf-8"?> <string xmlns="http://myUrl.com">you said:This is a test --&gt;SHA1:7d9d2886509cfd1dfd3c693fff43b82561ec00a18 ...

Executing mathematical calculations within a JavaScript object

Can an equation be executed inside an object? For instance: var taxes = { gst: '0.10', }; var prices = { first_key: '437.95', total_key: parseFloat(prices.first_key * taxes.gst).toFixed(2), ....... }, }; Or do I n ...

How can I make Backbone.js execute a function following the creation of a Collection?

It seems like I may not be grasping the full picture here, but let me lay out my current understanding: I have a Model that holds 'all' the data (JSON retrieved from a single URL). This model contains one or more Collections which are populated ...

What is the appropriate HTTP status code for "item not found" when using RESTful ajax?

Imagine having a single-page web application featuring a list of items. Users can click on an item to view more information about it. The details of each item are loaded asynchronously via an AJAX GET request to the URL http://www.example.com/item/[id], an ...

Implementing the DRY (Don't Repeat Yourself) principle across a group of related

I'm struggling with applying the DRY principle. Can someone assist me in creating a single function that can achieve the same results as these 4 functions? I also need guidance on how to call this function for each .symbol. function changeGradient(in ...

What is the method used by Disqus to adjust the size of its iframe according to the content it contains?

Check out this interesting example article: If you scroll down to the comments section, you'll notice that Disqus inserts an iframe onto the page, and then loads the comments inside the iframe. The intriguing part is that the iframe automatically ad ...

How to Redirect a Webpage to the Same Tab in ASP.NET

I am currently using an asp.net hyperlink control to direct users to a web URL when the hyperlink is clicked. My goal is for the user to open a new tab, rather than a new window, when they click the hyperlink. If the user clicks the link again, I want th ...

Insert a percentage sign into a right-aligned text box

I am trying to figure out how to permanently display a % symbol at the far right side of a textbox that shows a percentage. Can anyone help me with this? ...

Dynamic parameters can be passed in Rails using the link_to method

Feeling a bit stuck, maybe this is just a simple question. I have a sort button that I need to use to organize my list of questions. To do this, I create a link_to like this: <%= link_to xx_path(:is_sort => true, :remote=> true , :method=> :p ...

What impact does utilizing CSS 3D Transforms have on search engine optimization (SEO)?

Google emphasizes the importance of not hiding content, as it may not be counted by search engines. However, Google Cache has shown some discrepancies in understanding certain elements correctly. It's possible that what Google perceives as hidden migh ...