Mau*_*rio 4 html javascript css events vue.js
我在我的 Vue JS Web 应用程序上设置了一个拖放框。该元素是一个简单的 div,当文件拖放到文件上时,它会处理文件。
我使用https://www.raymondcamden.com/2019/08/08/drag-and-drop-file-upload-in-vuejs作为指导。
HTML:
<div v-if="status == 'upload'">
<h3> Please fill the data sheet and upload it here!</h3>
<div class="downbox" @drop.prevent="addFile" @dragover.prevent>
<span style="font-size: 60px"><font-awesome-icon icon='cloud-upload-alt'/></span>
<br>
Click to Browse
<br>
or Drag and Drop a File Here
</div>
</div>
Run Code Online (Sandbox Code Playgroud)
JS:
addFile: function(e){
let droppedFiles = e.dataTransfer.files;
if ((droppedFiles[0].name).slice(-5) !== '.xlsx') {
this.$swal({
text: "Please upload a file of type .xlsx",
title: "Incorrect File Type!",
icon: "error",
})
this.status = 'upload'
}
else {
this.file = droppedFiles
this.status = 'show'
}
},
removeFile: function(){
this.file = null
this.status = 'upload'
}
Run Code Online (Sandbox Code Playgroud)
CSS:
.downbox {
width: 500px;
border-radius: 50px;
border-width: 6px;
border-color: white;
border-style: dashed;
background-color: #7a2ab3;
padding: 10px;
font-size: 25px;
font-weight: bold;
transition: 0.6s;
margin-left: auto;
margin-right: auto;
}
.downbox:hover{
background-color: #c56bc5;
}
Run Code Online (Sandbox Code Playgroud)
正如您所看到的,当您将鼠标悬停在 div 上时,背景颜色会发生变化。
但是,当我将文件拖到 div 上时,这种颜色变化不会显示。所以我不知道如果您单击拖动文件,它是否算作“:hover”。
不管怎样,我想知道我可以在代码中添加什么,以便当我将文件拖到 div 上时使 CSS 背景颜色属性发生变化。
我使用以下解决方案(此处简化):
<template>
<div ref="drag" :class="{over: isOver}">
...
</div>
</template>
<script>
...
mounted () {
// add the needed event listeners to the container
this.$refs.drag.addEventListener("dragover", () => {
this.isOver = true; // add class on drag over
});
this.$refs.drag.addEventListener("dragleave", () => {
this.isOver= false; // remove class on drag leave
});
}
</script>
<css>
.over {...}
</css>
Run Code Online (Sandbox Code Playgroud)