When scrolling, custom cursor object disappears from view as it moves along with the mouse

My goal is to have a custom object follow the mouse by changing the translate property and assigning x and y values to it. Everything works fine initially, the object appears and moves within the first frame. However, when scrolling down, the transform property changes but the object disappears. It only reappears when scrolling back to the top, acting as if it is tied to the top of the page.

I am using position fixed to keep the cursor in place while scrolling. I want the object to start moving from the cursor's position when scrolling stops or the mouse starts moving.

document.addEventListener("mousemove", e => {
  document.querySelector('.mover').style.transform = `translate(${event.pageX}px, ${event.pageY}px)`
  
});
.mover {
  width: 50px;
  height: 50px;
  position: fixed;
  left: -8px;
  top: -6px;
  transition-duration: .15s;
    transition-timing-function: ease-out;
}

html {
  cursor: none;
}

body {
  background: #F5F9FF;
  height: 3000px;
}
<div class="mover">
  <svg width="24" height="24" viewBox="0 0 24 24">
  <path d="M12,11.5A2.5,2.5 0 0,1 9.5,9A2.5,2.5 0 0,1 12,6.5A2.5,2.5 0 0,1 14.5,9A2.5,2.5 0 0,1 12,11.5M12,2A7,7 0 0,0 5,9C5,14.25 12,22 12,22C12,22 19,14.25 19,9A7,7 0 0,0 12 ,2Z"></path>
</svg>
</div>

Answer №1

Replace page with client

The clientX property from the MouseEvent interface indicates the horizontal position within the viewport of the application where the event took place, rather than the position within the page itself.

When you click on the left edge of the viewport, the clientX value will always be 0, even if the page is scrolled horizontally. source

document.addEventListener("mousemove", e => {
  document.querySelector('.mover').style.transform = `translate(${event.clientX}px, ${event.clientY}px)`
  
});
.mover {
  width: 50px;
  height: 50px;
  position: fixed;
  left: -8px;
  top: -6px;
  transition-duration: .15s;
  transition-timing-function: ease-out;
}

html {
  cursor: none;
}

body {
  background: #F5F9FF;
  height: 3000px;
}
<div class="mover">
  <svg width="24" height="24" viewBox="0 0 24 24">
  <path d="M12,11.5A2.5,2.5 0 0,1 9.5,9A2.5,2.5 0 0,1 12,6.5A2.5,2.5 0 0,1 14.5,9A2.5,2.5 0 0,1 12,11.5M12,2A7,7 0 0,0 5,9C5,14.25 12,22 12,22C12,22 19,14.25 19,9A7,7 0 0,0 12 ,2Z"></path>
</svg>
</div>

Answer №2

To determine the position of the scroll bar and adjust the element's position, you can utilize the scrollTop property.

document.addEventListener("mousemove", e => {
  const style = document.querySelector('.mover').style;
  const scrollTop = document.documentElement.scrollTop;
  const x = event.pageX;
  const y = event.pageY;
  style.transform = `translate(${x}px, ${y - scrollTop}px)`
  
});
.mover {
  width: 50px;
  height: 50px;
  position: fixed;
  left: -8px;
  top: -6px;
  transition-duration: .15s;
    transition-timing-function: ease-out;
}

html {
  cursor: none;
}

body {
  background: #F5F9FF;
  height: 3000px;
}
<div class="mover">
  <svg width="24" height="24" viewBox="0 0 24 24">
  <path d="M12,11.5A2.5,2.5 0 0,1 9.5,9A2.5,2.5 0 0,1 12,6.5A2.5,2.5 0 0,1 14.5,9A2.5,2.5 0 0,1 12,11.5M12,2A7,7 0 0,0 5,9C5,14.25 12,22 12,22C12,22 19,14.25 19,9A7,7 0 0,0 12 ,2Z"></path>
</svg>
</div>

It is important to note that the scrolled element may vary depending on the browser, with it being either body or html, therefore, it is recommended to verify:

which of those two is changing:

document.documentElement.scrollTop
document.body.scrollTop

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

"Unlock special birthday surprises with our exclusive AJAX birthday code

Looking for assistance to update the code snippet below for the current date: For example, if today is 18/04/2014, then: day=18 month=04 year=14 If you don't understand: <select name="day" id="day"> <option value="na">day</option> ...

Is there a way in CSS to apply multiple colors to individual letters within a word?

Is it possible to add random colors to every pixel of a letter or multiple colors to a letter using CSS? Check out this example. ...

Leverage the Frontend (Headless Commerce) to utilize the Admin API and retrieve products from Shopify

Attempting to retrieve products from Shopify using the Admin API (GraphQL) through my frontend. I utilized the following code: *I implemented "axios" on Quasar Framework, utilizing Headless Commerce const response = await this.$axios({ url: "https: ...

Creating triangular side tabs with borders using CSS and HTML styling

I'm looking for a way to create triangle side navigation tabs similar to this example. I attempted using :after pseudo-element in my CSS: .tabs input + label:after{ content:""; float:left; position:absolute; top:0; right:-12px; width:0; height:0; bor ...

Tips on how to automatically resize the browser window to a specific width and height once it has been minimized

If a JSP page is minimized onload, we need to find a way to restore it through a JavaScript call. Are there any other methods available for achieving this? The restoration should be to the height and width selected by the user. I have attempted the followi ...

How can we ensure that the previous selection is cleared in jQgrid only if the checkbox is not clicked

I've set up a grid with the option for multiselect: true so that I can delete multiple rows at once. When the onSelectRow event is triggered, data related to the ID is loaded and shown to the user. Everything seems to be working fine in this example ...

Unable to display menu content text using jQuery

Currently experimenting with jQuery to create a dynamic submenu. The goal is to have a sub menu appear when the main menu is clicked, and then disappear when an item in the sub menu is selected, revealing additional information within a div. Unfortunately, ...

Creating a photo grid with consistent order is simple using flexbox

I'm currently working on an image grid that consists of 2 rows laid out horizontally. Initially, the first row contains 4 images while the second row has 2 images. Users should have the ability to add more images to the second row. The issue I am faci ...

Updating array with user input using Ajax

For the past few days, I have been struggling to extract data from a PHP page (array) and store this data in a jQuery array. The issue arises when trying to send data to the PHP page using the following code: $.ajax({ type: "POST", url: 'test ...

Generating individual div elements for every piece of data in ReactJS with the help of Redux

I am utilizing redux to manage the data within a React application. Each block of data is displayed within its own DIV element. My goal is to have each div separated by space and transformed into an accordion card. I am seeking guidance on the best appro ...

Adjustable Bootstrap Progress Bar - Modify element width on the fly

I have encountered an issue with my progress bars on the webpage. The static HTML version works perfectly fine, but the dynamically created one using jQuery seems to be instantly at 100% without any delay in progression. To illustrate the problem better, ...

event videojs termination malfunction

I am trying to set up an event that triggers when my video finishes playing. I have placed the script right below my video tag. <script type="text/javascript> var myPlayer = videojs("session_video"); videojs("session_video").r ...

Trouble with applying CSS class to checkboxlist

I am trying to display a border around each checkbox item in a checkboxlist. I believe that it can be achieved by setting the td cssclass as the checkboxlist saves items in td. However, the code I tried below is not working. Here is the ASPX code: < ...

What is the best way to ensure that two div elements within a list element fully utilize the available width on a webpage?

I am looking to create a 'settings' div that adjusts its width based on the available space on a page. This could range from 1000px to 600px, and I want the div to always be next to the 'title' div without wrapping onto the next line. F ...

Transforming the text to a new line

I have been attempting to format lengthy texts from a JSON file, but I haven't been successful. The text keeps displaying as multiple sections within a single response, which can be seen in the image below. I've tested solutions like word-break a ...

Display the outline of a translucent image

Looking to create an image reveal effect on my website. I want to display only the outline of a transparent image using brightness, then gradually reveal the full image by removing the brightness effect. Currently, I achieve this with a black outline usi ...

Modifying the title of a webpage with a Bootstrap design

As a newcomer to HTML/CSS, I recently utilized a Bootstrap theme from Themezy to build my personal website. The template includes a navigation bar with tabs labeled Top, Work, Portfolio, and Contact. I felt the need to change Top to About, so I replaced ...

Using an ampersand in my XML code is preventing it from being loaded correctly using jQuery

I am currently facing an issue with displaying descriptions within an XML document and loading it into my application using jQuery. Whenever I attempt to include '&' in the code, it causes a breakage during loading. I have also tried &# ...

Unexpectedly, the Discord bot abruptly disconnects without any apparent cause

Hey there! I've encountered an issue with my Discord bot - it keeps disconnecting after a few hours without any apparent cause! This particular bot is designed to regularly ping the Apex Legends game servers to check their status and display the ser ...

Eliminate the space between the navigation bar and carousel when using Materialize with Vue.js and Laravel

I am facing an issue with the gap between the navbar and carousel in my materialize design. I have implemented Vue components for each div and Laravel as the backend. Using Sass for CSS preprocessing, I attempted to adjust the margins for the body, navbar, ...