Is there a way to conceal or modify the drag not allowed cursor while dragging an item?

Hello there!

I'm currently exploring how to hide the "drag not allowed" cursor while dragging an item.

If removing the cursor isn't possible, I would also be open to changing it. It seems like it should be a simple task, right?

Check out this small demonstration I created to showcase the issue: JSFiddle

box0.addEventListener('dragstart', dragStart);
box0.addEventListener('dragend', dragend);
box0.addEventListener('dragenter', dragEnter);
box0.addEventListener('dragover', dragOver);
container.addEventListener('dragenter', dragEnter);
container.addEventListener('dragover', dragOver);
container.addEventListener('dragend', dragEnd);

function dragStart(e) {
    e.dataTransfer.setData('text/plain', e.target.id);
    setTimeout(() => {e.target.classList.add('boxh');}, 0);
}
function dragEnd(e) {
    e.target.classList.remove('boxh');
}
function dragEnter(e) {
    e.preventDefault();
}

Answer №1

To avoid the prohibited cursor, it is important to ensure that the preventDefault() method is applied to the target element (!!) - with the optimal choice being the entire document

document.addEventListener("dragover", (event) => {
    event.preventDefault();
});

Answer №2

To ensure proper functionality of your drop zones and hide the not-allowed cursor, it is essential to implement preventDefault on both dragOver and dragEnter events.

Once you have established a valid drop zone, you can customize the cursor using e.dataTransfer.dropEffect with options such as "copy", "move", "link", or "none". (https://developer.mozilla.org/en-US/docs/Web/API/DataTransfer/dropEffect)

Unfortunately, there does not appear to be an option for a no-cursor setting.

onDragOver={e => {
    e.dataTransfer.dropEffect = "move";
    e.preventDefault()
}}
onDragEnter={e => {
    e.preventDefault()
}}

Answer №3

It's been quite some time since this question was asked, but I encountered the same issue today and found it surprisingly difficult to change the cursor while dragging an element using JavaScript.

Despite trying various methods like using the dataTransfer object, the dropEffect property, and the dragEvent event, none of them seemed to work for me, even after consulting the MDN Web Docs.

For those who are still struggling with changing the cursor during a drag operation, here's a solution that worked for me:

Key Point: Add a CSS class with pointer-events: none to elements where you don't want the "not allowed" cursor to appear.

// Identify the container
const file_container = document.querySelector("#file_container")

// Grab all the files
const files = document.querySelectorAll("[data-id='file']")

// Loop through each file
for (let file of files)
  {
    // Initiate the file drag action
    file.addEventListener("dragstart", (ev)=>{
      ev.target.classList.add("ds_awaiting_file")
    })

    // Allow dropping by preventing the dragover event
    file_container.addEventListener("dragover", (ev)=>{ev.preventDefault()})

    // Remove the added styles from the awaiting file
    document.addEventListener('dragend', ()=>{
      file.classList.remove("ds_awaiting_file")
    });
  }
/* Solution for Problem - prevent pointer events on div children */
[data-id=file] *
{
  pointer-events: none;
}

/* General Styling */
#file_container
{
  display: grid;
  grid-template-columns: 160px 160px 160px;
  grid-template-rows: 100px;
  grid-column-gap: 20px;
  grid-row-gap: 20px;
}

[data-id=file]
{
  border: 2px solid #1E3050;
  padding: 12px 60px
}

.block
{
  background-color: #1E3050;
  height: 40px;
  width: 40px;
}

.ds_awaiting_file
{
    opacity: 0.3;
}
<div id="file_container">

  <div data-id="file" draggable="true">
    <div class="block"></div>
    <p>File_1</p>
  </div>

  <div data-id="file" draggable="true">
    <div class="block"></div>
    <p>File_2</p>
  </div>

  <div data-id="file" draggable="true">
    <div class="block"></div>
    <p>File_3</p>
  </div>

  <div data-id="file" draggable="true">
    <div class="block"></div>
    <p>File_4</p>
  </div>

</div>

This approach will help in hiding the "drag not allowed" cursor when dragging, however, please note that in certain browsers like Chrome and Opera, borders and margins might still be identified as non-allowed elements.

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

Regulations for the Web Address of the Interactive Feature in AJAX

Recently, I decided to experiment with AJAX while trying to incorporate form validation from a database before allowing users to submit content. However, I encountered an issue with the URL segment of the Open function. It seems that when I use the full ...

What is preventing me from obtaining the select and input values?

I'm currently facing an issue where I am unable to retrieve the values of customInput and customSelect and store them in the state. The challenge arises when trying to integrate these components into a react dashboard material-ui setup. Strangely, whe ...

What could be causing the labels to stay stagnant while the image moves?

What could be causing the labels to remain static while the images (front and back of human anatomy) move on Galaxy S8 and iPhones? Check out the website at . Your assistance is greatly appreciated. ...

Issue encountered when implementing async functions in NodeJs middleware

I'm currently facing an issue while attempting to load the initial data for my blacklist from a Redis DB in my middleware code. The request to the database takes some time, causing it to fail. Below is the snippet of code that executes when the app s ...

I am having trouble with my React tutorial for tic tac toe as the handleClick function is not receiving the

Currently, I am working my way through the React tutorial available at: https://react.dev/learn/tutorial-tic-tac-toe My current challenge lies in passing a value to the handleClick function to determine which square should be set to 'X'. Despi ...

Leveraging NodeJS to handle server-side tasks and operations

Background: I am exploring the use of NodeJS for a project that involves scraping and storing content in Mongo. This process needs to be automated according to a set schedule. In addition, I need functions that can extract items from the Mongo database, o ...

Tips for creating a dynamic animation: How to make a div fall from the top and rotate

Hey everyone, I'm working on creating an animation where a phone falls from the top and then rotates and skews to give the illusion of a 3D shape when it reaches the bottom. However, I'm facing an issue with flickering in the animation when it hi ...

What is the best way to incorporate a scroll bar into a table that updates dynamically?

If I were to create an empty table in my index.html: <body> <table width="800" border="0" class="my-table"> <tr> </tr> </table> </body> Afterward, I would dynamically add rows and columns to my-table using ...

Delete the status message once the PDF has been successfully created using C# and Rotativa

We have implemented an "export to PDF" feature that generates a detailed report when the button is clicked. The PDF generation process is powered by Rotativa. While the PDF is being generated, we want to display a message (which is easy), and then dismiss ...

ng-bind-html is having trouble parsing the HTML correctly and binding it

Here is the code for my controller: myApp.controller('actionEditController', ['$scope', '$stateParams', '$sce',function ($scope, $stateParams, $sce) { $scope.table="<p>OOPSY</p>"; $sc ...

Encountering an error in Express while attempting to upload an image due to the inability to read the property 'file' of undefined

I am currently learning Express framework. I encountered an error while trying to upload an image using Express. The error message I received is "Cannot read property 'file' of undefined." Below are the code snippets that I am using, and I&apo ...

Can a Material UI Select element be made unresponsive to hover effects?

When a user hovers over an element, I want to achieve the following functionality: document.getElementById("dropdownCategory").disabled = true; <Tooltip arrow placement="right" title={props.description} > <FormControl id="dro ...

What is causing Firefox to display an additional line in this section?

After much effort, I've been attempting to eliminate the extra spacing on this page in Firefox: Do you see the menu? If you compare it to Chrome, you'll notice that the menu is positioned too low beneath the background, giving the layout a broke ...

verification tool for a less file's syntax

Searching for a tool to validate a specific less file at the syntax level. The issue lies in the validator not recognizing dependencies or detecting declared mixins. Many existing less processors do not work due to missing dependencies that cannot be prov ...

Performance testing with JMeter and Webdriver on multiple browser windows

Looking for help with JMeter and Webdriver to record UI tests. Stuck on selecting a popup window due to Javascript issues... Here is the current code snippet: var pkg = JavaImporter(org.openqa.selenium) var support_ui = JavaImporter(org.openqa.selenium.s ...

Ways to identify the current version of webpack installed

Currently in the process of utilizing Webpack to transpile and bundle multiple JS files. I am curious about determining the specific version that I am using. In this scenario, assuming that it is globally installed, how can I identify the version without ...

Generating Over 50,000 Text Particles with the Power of THREE.js

Seeking to generate a character particle system with over 50,000 individual letters. Came across a similar solution using XG here. The main challenge lies in the application's performance when creating this. Here is some concise pseudo code: var f ...

Endless Background Image HTML/CSS

I'm currently working on a website where I'd like to have a background image that is black with some characters. The height of the page can vary, being either 'M' pixels or 'N' pixels tall. My goal is to show only one instanc ...

The Express API is failing to recognize the data keys that were sent from the React frontend, despite being clearly specified

I am facing an issue while trying to send data to a REST API using React hosted in a separate application. The API does not seem to receive the keys sent, even though I checked the results in Chrome and found this output:(2) ["imageSrc", File]0: "imageSrc" ...

Recognizing when console.log() is used

Currently, I'm in the process of creating a test case for a debugging technique that utilizes console.log() to write messages to the JavaScript console. The objective of the test is to verify that the message has indeed been successfully logged to the ...