javascript - DragEnd event not fired for files - Stack Overflow

My template:<div class="upload-ponent" @dragend="log('end')" @dragent

My template:

<div class="upload-ponent" @dragend="log('end')" @dragenter="dragEntered" @drop.prevent @dragover.prevent>
    <div class="zone" @drop="dropped">
        
    </div>
    <p draggable="true">drag me</p>
</div>

My js:

export default {
        name: 'UploadComponent',
        methods: {
            log(str){
                console.log(str)
            },
            dragEntered(e){
                // change some styles
            },
            dropped(e){
                console.log(e.dataTransfer.files)
            }
        }
    }

The problem:
If I drag the P element and drop it or hit escape it will log "end". If I e with a file from my desktop and put it back on desktop or hit escape or drop it, the dragend event wont fire and it wont log anything

My template:

<div class="upload-ponent" @dragend="log('end')" @dragenter="dragEntered" @drop.prevent @dragover.prevent>
    <div class="zone" @drop="dropped">
        
    </div>
    <p draggable="true">drag me</p>
</div>

My js:

export default {
        name: 'UploadComponent',
        methods: {
            log(str){
                console.log(str)
            },
            dragEntered(e){
                // change some styles
            },
            dropped(e){
                console.log(e.dataTransfer.files)
            }
        }
    }

The problem:
If I drag the P element and drop it or hit escape it will log "end". If I e with a file from my desktop and put it back on desktop or hit escape or drop it, the dragend event wont fire and it wont log anything

Share Improve this question edited Nov 13, 2020 at 15:48 Brian Tompsett - 汤莱恩 5,89372 gold badges61 silver badges133 bronze badges asked Jul 26, 2017 at 8:40 JohnJohn 1,1231 gold badge12 silver badges38 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 8

I'm probably a few years late but maybe someone else will find this useful.

Note that dragstart and dragend events are not fired when dragging a file into the browser from the OS.

Source

So dragend event won't work but there's another solution, dragover event fires rapidly so you can set a timeout to clear the overlay if it stops firing for a second like this:

<template>
  <div class="advice-documents">
    <div
      v-if="isDragOverlayVisible"
      class="advice-documents__drop-zone"
    >
      <span>drop file to upload</span>
    </div>
  </div>
</template>

<script>
export default {
  data: () => ({
    isDragOverlayVisible: false,
    dragOverTimeout: undefined,
  }),

  created() {
    document.addEventListener('dragover', this.handleDragover)
    document.addEventListener('drop', this.handleDrop)
  },

  beforeDestroy() {
    document.removeEventListener('dragover', this.handleDragover)
    document.removeEventListener('drop', this.handleDrop)
  },

  methods: {
    handleDrop(event) {
      event.preventDefault()
      this.isDragOverlayVisible = false
    },

    handleDragover(event) {
      event.preventDefault()
      this.isDragOverlayVisible = true
      console.log('dragover')
      clearTimeout(this.dragOverTimeout)
      this.dragOverTimeout = setTimeout(() => {
        this.isDragOverlayVisible = false
      }, 1000)
    },
  },
}
</script>

<style>
.advice-documents__drop-zone {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background-color: rgba(0,0,0,0.7);
  color: var(--aab-light-green);
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 8vmin;
  z-index: 1;
}
</style>

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745613915a4636115.html

相关推荐

  • javascript - DragEnd event not fired for files - Stack Overflow

    My template:<div class="upload-ponent" @dragend="log('end')" @dragent

    6小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信