1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
| <template> <div class="file-drag" :class="{ 'is-drag': (drag === null ? false : true) }" @dragenter="ondragenter" @dragleave="ondragleave" @drop.prevent.stop="ondrop" @dragover.prevent="() => {}"> <slot></slot> </div> </template> <script>
import { elwarning } from '@/utils'; export default { name: 'drag', emits: ['drop'], props: { accept: { type: String }, disabled: { type: Boolean } }, data() { return { drag: null } }, methods: { _init() { }, ondragover(e) { }, ondragenter(e) { this.drag = e.target; console.log('ondragenter', e); }, ondragleave(e) { if (this.drag === e.target) { this.drag = null; console.log('ondragleave===>', e); } }, ondrop(e) { this.drag = null; if(this.disabled) { return elwarning('当前文件拖拽已禁用'); } let files = e?.dataTransfer?.files || []; if(this.accept) { let accept = this.accept.split(','); for(let file of files) { if(accept.includes(file.type)) { } else { elwarning('文件类型不正确:' + file.type); return; } } } console.log('ondrop ondrop', files); this.$emit('drop', files); }, }, mounted() { this._init(); } } </script> <style scoped lang="scss"> .file-drag { width: 100%; height: 100%;
&.is-drag { background: rgba(0, 135, 255, 0.1); border: 2px solid #0087ff; position: relative; } } </style>
|