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

Problems encountered when attempting to create a link between two nodes on a force-directed graph using a mouse click

I'm currently working on creating an interactive graph where users can click on two nodes to establish a link between them (which can be removed later). My approach was inspired by Mike Bostock's example at: https://bl.ocks.org/mbostock/1095795 ...

"Utilizing jQuery's getJSON method in a session to fetch data

I have a code snippet that fetches the current number of likes on Facebook like this: $(document).ready(function() { $.getJSON('https://graph.facebook.com/<username>?callback=?', function(data) { var fb_count = data['likes ...

Top-notch tools and guides for front-end web development

As I prepare to transition to a new role focused on front-end web development, specifically CSS and jQuery, I am seeking recommendations for valuable resources to enhance my skills in these areas. Whether it be books, websites, blogs, or any other medium, ...

I'm having trouble getting my .click method to work with the <div id=menuButton>. Can anyone help me figure out why this is happening?

Here is the HTML code I created for a dropdown menu. Initially, in the CSS file, the menu is set to display: none; <!doctype html> <html> <head> <title>DropDown Menu</title> <link rel="stylesheet" href="normalize ...

Receiving and transmitting messages repeatedly in Discord.JS

Currently, I am developing a simple bot. Interestingly, the bot seems to be responding multiple times to a single command. Here is the code snippet: const Discord = require('discord.js'); var bot = new Discord.Client(); const PREFIX = "+"; var ...

At what point is the args variable required by Dojo?

There comes a point when passing the args variable to an anonymous function in Dojo becomes necessary, even though the function itself may not visibly need it. This can lead to confusion as there is no clear indication on the Dojo help page regarding whe ...

Styling with CSS using the em unit to make an image half the size of its parent div element

I'm struggling with resizing an image within a div using the em size unit. I want the image to be half the size of its parent div, so I set both the width and height CSS properties of the image to 0.5em. However, when looking at the code below, you c ...

The server returned a response that was void of any content

I have encountered an issue while trying to retrieve data from my server. The request works perfectly fine when tested with Postman, returning the expected data. However, upon implementing the request in my application, I receive an empty object with prope ...

Tips for Disabling ML5 Posenet

Looking to halt Posenet after completing app task private sketch(p: any) { p.setup = () => { this.poseNet = ml5.poseNet(p.createCapture(p.VIDEO), { outputStride: 8 }); this.poseNet.on(&apos ...

Using Jquery to delete the parent element containing text that does not match

I need to search for text in a table cell that matches the text in an h1 heading and then eliminate all other table rows containing text that does not match. The code snippet provided only works if there is one .tablerow with a .tablecell, so I am looking ...

Evaluating TypeError in CoffeeScript using Jasmine with Backbone.js

Currently, I am following the PeepCode video tutorial on Backbone.js, but I am rewriting all the code in CoffeeScript instead of plain JavaScript. Everything is going well so far, except when I attempt to run Jasmine tests on the code, I encounter some Ty ...

The scrollbar is shifting the page's content towards the left

As a first-time user of Bootstrap, I have encountered an issue while building my website that I cannot seem to solve. Whenever I add large content that requires a scrollbar in the browser, the entire page shifts to the left. In simpler terms, when a scrol ...

Node.JS Logic for Scraping and Extracting Time from Text

Currently, I am working on developing a web scraper to gather information about local events from various sites. One of my challenges is extracting event times as they are inputted in different formats by different sources. I'm seeking advice on how t ...

Displaying object properties within another object obtained from an API request in a React component

Dealing with API data can be tricky, especially when there are nested objects involved. I've encountered an error message stating 'undefined is not an object (evaluating 'coin.description.en')', as the description property of the c ...

Library for Nodejs that specializes in generating and converting PDF/A files

Is there a library available that can convert/create a PDF/A file? I've been searching for solutions but the existing answers suggest using an external service or provide no response at all. I heard about libraries in other languages like ghostscriptP ...

I am looking to incorporate an OVG video onto my website

I have a video in OVG format that I want to add to my website. Since I am not very familiar with this file type, I'm concerned about how it will perform across different web browsers. I know that Firefox can play the file, but I'm unsure about In ...

Having trouble rendering arrays and objects in Node, Express, and Jade?

//What is currently being passed to the Jade template exports.displayList = function(req, res){ res.render('report', {title: 'Custom Reports', rpts:[{uri:'/reports/allocation', title:'Allocation Report' ...

Issue encountered while utilizing combineReducers: "Error: The assetsReducer returned an undefined value during initialization."

Issue: The "assetsReducer" has returned an undefined value during initialization. When the state passed to the reducer is undefined, it must explicitly return the initial state, which cannot be undefined. If no value is set for this reducer, consider using ...

Material UI button text not receiving props

Within my application, I have a situation where I utilize data attributes on buttons to gather specific contextual information in the modal that is triggered by clicking the button. This functionality depends on accessing the data attributes through e.targ ...

Difficulty positioning CSS, aligning flex-box grid with floating text

My attempt to include an aside section is causing alignment issues with the navigation menu and the aside itself. The problem seems to be related to using float:left for the 200x200 images. I want the layout to be well-positioned like the expected image. I ...