Is there a CSS “:drop-hover” pseudo-class?

前端 未结 2 1040
眼角桃花
眼角桃花 2021-02-11 16:35

Saying I have an input type=\"file\" field. One can drop a file on this input(like in Firefox) instead of clicking \"browse\" and selecting the file.

2条回答
  •  故里飘歌
    2021-02-11 17:21

    I had the same question and solved it a little differently than nashcheez. Still using JavaScript, though (I used jQuery here to simplify things):

    function drag(ev) {
      ev.dataTransfer.setData("text", "foo");
    }
    
    function allowDrop(ev) {
      $(ev.target).attr("drop-active", true);
      ev.preventDefault();
    }
    
    function leaveDropZone(ev) {
      $(ev.target).removeAttr("drop-active");
    }
    
    function drop(ev) {
      ev.preventDefault();
      $(ev.target).removeAttr("drop-active");
      alert(ev.dataTransfer.getData("text"));
    }
    #draggableItem {
      height: 50px;
      width: 150px;
      background-color: #eee;
    }
    
    #dropZone {
      height: 50px;
      width: 150px;
      background-color: #efe;
    }
    
    #dropZone[drop-active=true] {
      box-shadow: inset 0px 0px 0px 2px #00C;
    }
    
    
    
    Drag Me
    Drop Here

    I've tested this on Safari, Firefox and Chrome, but I haven't tried IE. I'm probably breaking a rule with the custom attribute, but it seems to work while I'm waiting for CSS4.

提交回复
热议问题