Attach a button and arrow to the cursor while it is hovering

Can anyone help me figure out how to make a button magnetize the content (arrow) along with it when hovered over by the cursor? I found an example of what I want on this website , but I am struggling to implement it in my own code.

I've searched through forums and tried different solutions, but nothing seems to work. Any assistance would be greatly appreciated!

function btnRun() {

  let offset = 70,
    cur = false;

  document.body.addEventListener('mouseenter', function(e) {
    if (e.target.classList.contains('btn') && cur === false) {
      cur = {
        e: e.target,
        x: e.target.getBoundingClientRect().left,
        y: e.target.getBoundingClientRect().top
      };
    }
  }, true);

  document.addEventListener('mousemove', function(e) {
    if (cur !== false) {
      let x = (e.clientX - cur.x) - (cur.e.getBoundingClientRect().width / 2),
        y = (e.clientY - cur.y) - (cur.e.getBoundingClientRect().height / 2);
      cur.e.style.transform = `translate(${x}px,${y}px)`;

      if (Math.abs(x) >= offset || Math.abs(y) >= offset) {
        cur.e.style.transform = 'translate(0,0)';
        cur = false;
      }
    }
  });

}

btnRun();
.btn {
  position: relative;
  width: 69px;
  height: 69px;
  border-radius: 100%;
  border: 0px;
  background: #FF625B;
  color: #fff;
  font-size: 29px;
  font-weight: 300;
  text-align: center;
  text-decoration: none;
  padding-top: 15px;
  transition: transform .2s linear;
  margin-top: 20px;
}

.intro {
  position: relative;
  display: flex;
  width: 100%;
  height: auto;
  background: #447EF0;
}

.intro__inner {
  position: relative;
  display: flex;
  width: 100%;
  height: auto;
}

.intro__content {
  width: 50%;
}

/* More CSS styles here... */

.intro__info h2:last-child {
  padding-right: 0px;
}

.intro__h2__one {
  order: 1;
}
<div class="intro__block__btn">
  <p class="intro__text__btn">Sign Up</p>
  <a href="#form" class="btn btn-intro">
    <span>--></span>
  </a>
</div>

Answer №1

The code is functioning perfectly on CodeSandBox as well as my Edge browser. (insert code snippet here)

For reference, you can check it out at https://codesandbox.io/s/keen-jennings-kl3d5

If for some reason it doesn't work on your current browser, feel free to try running it on a different browser or platform.

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

Interactive menu backdrop determined by div element

I am working on a website with a menu structured like the following: Home -- Work -- Pricing -- Contact Us -- About This website uses a one-page layout and has enabled Scrollspy on Bootstrap. I need to make the active menu background dynamic depending o ...

Avoid showing an image when it is outside the div container

Within a single div, I have an assortment of images that are dynamically repositioned using JQuery. $("#"+carElem).css({"left": pos.left+50 +"px"}); I am currently utilizing absolute positioning (although relative positioning yields the same outcome). Is ...

Error in AngularJS and TypeScript: Property 'module' is undefined and cannot be read

I'm attempting to incorporate TypeScript into my AngularJS 1.x application. Currently, my application utilizes Webpack as a module loader. I configured it to handle the TypeScript compilation by renaming all *.js source files to *.ts, and managed to ...

encounter an auth/argument issue while using next-firebase-auth

Issues: Encountered an error while attempting to log in using Firebase Authentication. No errors occur when using the Firebase Auth emulator, but encountered errors without it. Received a 500 response from login API endpoint: {"error":"Unex ...

The Modal Component remains unchanged despite being connected to useContext for data, as the Array is being modified through the useReducer Hook

In search of an elegant solution, the title speaks for itself - I am on a mission to create a practice food order App (consider me a novice in this field). This App boasts a Cart feature where users can add items for ordering. The nav-bar conveniently disp ...

CSS animations failing to transition properly in Firefox

It seems that despite following all the necessary steps in my CSS, I am facing an issue with a transition not working properly in Firefox. The bounce-in effect I have implemented is failing to function as expected in this browser, even though Firefox does ...

Click on a plane within a three.js environment to determine the X Y position within it

When using a raycaster, I can successfully obtain the current object (in this case, a plane) under the mouse. However, I am seeking a more precise X and Y value for the mouse position INSIDE the plane. var vector = new THREE.Vector3( ( event.clientX / win ...

What is the process for monitoring a property in Angular?

Seeking clarification on Angular - is it possible to detect property value changes within the same class? import { Component } from '@angular/core'; @Component({ selector: 'my-app', templateUrl: './app.component.html', ...

What are the solutions for resolving problems with the display and functionality of a multi-page form in Internet Explorer?

I am currently working on a form and you can view it at this link: http://jsfiddle.net/pPWr3/14/ While the form displays correctly in Chrome and Firefox, I am experiencing display and functionality issues when using Internet Explorer 9. The problems inclu ...

Using Ajax to update a webpage by invoking a PDO function within a PHP class

Looking for assistance with filtering a list of files using a dropdown box and categories. I have a PHP class ready to handle the SQL query and results, but I'm eager to implement this through AJAX to avoid page refresh. Feeling stuck and seeking gui ...

After refreshing the page, vuex is encountering null values when using firebase.auth().onAuthStateChanged

Encountering an issue with Vuex and Firebase Authentication integration. When reloading the page, there is a delay in response from firebase.auth().onAuthStateChanged. I require an immediate response upon page reload without using router guards as seen in ...

What is causing the error when trying to parse a JSON with multiple properties?

Snippet: let data = JSON.parse('{"name":"dibya","company":"wipro"}'); Error Message : An error occurred while trying to parse the JSON data. The console displays "Uncaught SyntaxError: Unexpected end of JSON input" at line 1, character 6. ...

Building a bespoke search input for the DataTables JQuery extension

As the title indicates, I am in the process of developing a custom search box for the DataTables JQuery plugin. The default options do not suit my needs, and configuring the DOM is also not ideal as I aim to integrate it within a table row for display purp ...

Subtract and include characters in a string

Can you help me swap out \n for a new line? Below is the content retrieved from the database: <!DOCTYPE html>\n<html>\n<body>\n<div>\n<label class=\"s20 c03\">Label</label>\n< ...

What causes the 'then' method of my angular service to return a resolved promise instead of the expected value?

I am perplexed as to why the "result" in this code snippet is a resolved promise instead of the actual value: searchService.getLink(vm.queryObject).then(function (result) { console.log(result); }); The implementation for the getLink() function is pro ...

Generate a fresh object if the values within the TypeScript object are identical

How can I filter an object to return a new object containing elements with the same values? For example: allValues = {"id1": 3, "id2": 4, "id3": 3} The desired output is: filteredValues = {"id1": 3, "id3": 3} This is because the keys "id1" and "id3" hav ...

What is the best way to transmit a 500x500 2D Integer Array using Websockets?

I'm encountering an issue where I believe it may be too time-consuming to JSON.stringify and send data to each individual user. For example, if 4 people connect at the same time, the server will become stalled during array parsing, resulting in signif ...

Tips for customizing standard data types in TypeScript

Currently facing a challenge where I need to update a global type. Specifically, I am looking to modify the signature of the Element.prototype.animate function to make it optional. This is the approach I attempted: declare global { interface Element { ...

Issue with passing values from JavaScript to PHP not working as expected

For some reason, I just can't seem to get this line of code to work. It's like my brain is not cooperating: var all = color.val('all'); $('#cssColor" + <?php echo $page ?> + "', parent.document).attr("background-color", ...

Is it advisable to approve automatic pull requests initiated by dependabot for updating the yarn.lock file?

I've recently received some pull requests from "dependabot" in a JavaScript library I am working on, like the one found here. While I appreciate the effort to update dependencies to newer versions, it seems strange that each PR only updates the versi ...