Changing the cursor on drag operation

Having trouble changing the cursor while dragging an object? I'm struggling to find a solution to change the cursor properly during dragging.

My setup involves a React component where I attempted to change the cursor without success. Despite my efforts, the cursor remains the same during dragging (stuck as the default crossed circle).

function App() {
  const dragTarget = useRef(null);

  function dragStartHandler(e) {
    dragTarget.current.style.cursor = 'move';
  }

  function dragEndHandler() {
    dragTarget.current.style.cursor = 'auto';
  }

  return (
    <div
      draggable
      onDragStart={dragStartHandler}
      onDragEnd={dragEndHandler}
      id="dragTarget"
      className="draggable-element"
    >
      Drag me!
    </div>
  );
}

Seeking guidance on how to effectively control cursor styles during dragging. Any help is greatly appreciated.

Already tried the suggestions provided in previous answers, but no luck. Screenshots attached below: https://i.sstatic.net/PRvdR.png

Answer №1

You have the option to apply styles using the :active selector:

div {
   cursor: grab;
}

div:active {
  cursor: grabbing;
}

Answer №2

It is a common limitation that most browsers do not support changing the cursor style. The cursor style for a dragged element is typically controlled by the browser and cannot be overridden with CSS or JavaScript.

One workaround is to modify the cursor style of the entire document body, which will affect the cursor style for the entire page. You can achieve this by including the following code snippet in your project:

function dragStartHandler(e) {
document.body.style.cursor = 'move';
}

function dragEndHandler() {
document.body.style.cursor = 'auto';
}

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

Accessing the name attribute of option tags in a controller: A guide

Within my blade file, the following code snippet is present: <select class="form-control" name="region_id_report"><option name="blank">No Region</option></select> Inside my controller, a variable named $regionidreport stores the v ...

Incorporating URL addresses and pagination features using React.Js and Material-UI components

I have a functional component-page on my website, where I display a table that fetches data from an API. To improve user experience, I implemented pagination using the Material-UI library. While the pagination functionality works fine in updating the table ...

add a border to an image when it is hovered over

I'm having trouble getting the hover effect to work on my images. I suspect that I may be targeting the CSS hover incorrectly, and there could also be a conflict with the jQuery code that is attached to the images. Here is the link to the fiddle: ht ...

What could be the reason for this JavaScript malfunctioning in both Chrome and Safari browsers?

Why is it that the javascript works fine in Firefox for the gallery on the page below, but doesn't seem to be functioning properly in Chrome and Safari? In Chrome, the big image isn't displaying but the thumbnails are, and in Safari nothing show ...

What is the best way to connect images within my database?

Struggling to come up with a title for this question, but I'll do my best to explain clearly. I'm working with PHP & HTML and using a MYSQL Database. So, on my member site, each new member is assigned a random football player as their avatar fro ...

Understanding how to deduce parameter types in TypeScript

How can I infer the parameter type? I am working on creating a state management library that is similar to Redux, but I am having trouble defining types for it. Here is the prototype: interface IModel<S, A> { state: S action: IActions<S, A&g ...

FusionCharts Gauges may share similar code, but each one produces a unique output

Currently, I am developing a project that involves creating a dashboard with 3 gauges. These gauges are enclosed in bootstrap cards with a column value set to 4. In the layout of the dashboard, these 3 cards are positioned next to each other. I have succe ...

What could be causing my Bootstrap Carousel to malfunction?

Currently, I am developing a MEAN stack application with Angular 7. In this project, I have incorporated a Bootstrap Carousel, but unfortunately, it seems to be malfunctioning. #slider .item { height: 500px; } .carousel-caption { font-size: 18px; } ...

Refreshing the page using location.reload triggers a reload of various elements

I am currently working on a website that supports multiple languages and utilizes a cookie named nav_lang to determine the user's preferred language for navigation. Whenever a user selects a new language, the cookie is updated accordingly and the page ...

There is a missing file extension "css" in the import/extensions for "typeface-roboto"

I have recently begun working on a new Ruby on Rails application with React, utilizing the webpacker gem. In addition to that, I have included material-ui and eslint-config-airbnb. However, when I ran the linter, it flagged the following error: Missing fi ...

hiding html elements by using the display property set to none instead of physically removing

I am currently utilizing an if-else statement to display different HTML structures. As a result, the entire HTML is being rendered twice. Is there a way we can utilize 'display: none' instead? I attempted to use it in th ...

Is there a way to stop Material UI from dulling the color of my AppBar when using dark mode in my theme?

When I use mode: "dark" in my Material UI theme, it causes the color of my AppBar to become desaturated. Switching it to mode: "light" resolves this issue. This is how my theme is configured: const theme = createTheme({ palette: { ...

How to Make a Div Tag Fade Effect with JavaScript in an External File

*** New Team Member Notice *** I'm currently working on an assignment and trying to implement a simple fade effect on a box. While it seems like this should be straightforward, I'm encountering some obstacles. Currently, the button causes the bo ...

Updating Variables in Azure DevOps Code Prior to DeploymentORModifying Variables

Can Azure DevOps automate code updates upon release? For instance, in my React app, I have a setting file with export const ISPROD = false. I want Azure DevOps to modify this value to true before building and deploying the app. Is this achievable? Please ...

Launching a new tab with a specific URL using React

I'm attempting to create a function that opens a new tab with the URL stored in item.url. The issue is, the item.url property is provided by the client, not by me. Therefore, I can't guarantee whether it begins with https:// or http://. For insta ...

How can I show two unique chart modals simultaneously?

Below is the chart that I am currently working on: https://i.stack.imgur.com/bHSj6.png While I am able to display the charts separately, I am encountering difficulties when trying to display both of them on the same chart line. <!DOCTYPE html> < ...

Use Python to enter a value on a webpage coded in HTML

My current project involves automating the input of values into a webpage. However, I have encountered a significant hurdle with the Mechanize library as it does not recognize the forms on my page which are <input> elements, unlike the typical form.n ...

Retrieving Information from Website Components in JavaFX Web View

I am currently developing a Java FX program that loads a folder containing HTML/CSS/JS files with 3 different websites. While the websites are displaying correctly in the webview, I am looking for a way to capture user interactions, such as checkbox selec ...

Next.js 14: Issue with Framer Motion's useScroll not updating when applied to an element container

I'm currently working with Next.js version 14.2.5 and Framer Motion version 11.3.21. My project involves creating a horizontal card carousel with scrolling functionality. To monitor the scrolling progress, I've implemented the useScroll hook prov ...

Begin counting starting from 1 all the way up to 24, then feel free

I've developed a counter that increments from 0 to 24, and once it reaches 24: setInterval(function(){DayAndNight()}, 1000); var iState = 12; function DayAndNight() { //console.log('test'); switch(iState) ...