2

I want the user to be able to drag and drop a file into the page and trigger an event. Right now i have an overlay which is on top of the page with z-index: 100 and it works to drop files onto it. My issue is that it is overlapping every other element on the page (including elements that cover the entire background), which means i cannot click anything below; text, buttons, play on a video, everything below cannot be interacted with.

I was thinking to have the clicking removed so it can be clicked through, but still keep the hovering event so files can be dropped on it. I have had no luck figure this out, looked at multiple other SO questions for answers but none have worked as i want it to. I'm looking for pure JS (or NodeJS, since that can be used in my case), CSS and HTML. Besides that I'm open to suggestions on how to get this done. Thanks in advance!

I tried doing it using window.ondrop, window.ondragover, etc. instead of an overlay but this seems to be disrupted by the other elements on the page.

I've also tried to add pointer-events: none; and user-select: none; in combo and alone.

Putting the overlay on a layer below all the elements makes it uninteractable for the same reason as the background elements becomes uninteractable when the overlay is on top.

JSFiddle: https://jsfiddle.net/apLuykz3/1/ I need the button below to be clickable, as well as any other element, but maintain the hover event when dragging files onto the window.

HTML

<div id="dropOverlay"></div>

CSS

div#dropOverlay{
  position: fixed;
  left: 0;
  top: 0;
  width: 100vw;
  height: 100vh;
  z-index: 100;
}

div#dropOverlay[show="true"]{
  background: rgba(0, 0, 0, 0.5);
}

JavaScript

// Drop events
  document.querySelector("#dropOverlay").ondrop = function (e) {
    // e.preventDefault();
    var files = e.dataTransfer.files;
    for (var i = 0; i < files.length; i++) {
      console.log(files[i]);
    }
    this.removeAttribute("show");
  }

  document.querySelector("#dropOverlay").ondragenter = function (e) {
    e.stopPropagation();
    e.preventDefault();
    this.setAttribute("show", "true");
  }

  document.querySelector("#dropOverlay").ondragleave = function (e) {
    e.stopPropagation();
    e.preventDefault();
    this.removeAttribute("show");
  }

  document.querySelector("#dropOverlay").ondragover = function (e) {
    e.preventDefault();
  }
Lucasion
  • 21
  • 3
  • share a fiddle to see and test the code. – Dhananjai Pai Oct 09 '19 at 17:44
  • @DhananjaiPai I added a JSFiddle to the question – Lucasion Oct 09 '19 at 21:25
  • @Artur This still has an issue which i explained above, but i might have not been clear enough. https://lucasion.xyz/f/01.00.56-10.10.19.gif Check that gif. It shows that the hovering is inconsistent when you touch the other elements as well – Lucasion Oct 09 '19 at 23:01
  • Oh wow, you're quick. Yeah, I just noticed the problem for the other one. [jsfiddle](https://jsfiddle.net/nepL2yt3/) Try this one? – dw_ Oct 09 '19 at 23:26
  • @Artur well, this would work if there's no elements between the cursor and the overlay... But i believe i forgot to mention that in my case, there are background elements. https://jsfiddle.net/4z0k5yx7/2/ This is a lot more accurate of what i have on my actual page, and the overlay isn't being "hovered over" since it's all the way in the back. But if it isn't all the way in the back, it blocks other elements. – Lucasion Oct 09 '19 at 23:41

0 Answers0