gpt4 book ai didi

javascript - 拖动文件时更改元素的 CSS 属性

转载 作者:行者123 更新时间:2023-12-01 00:02:30 24 4
gpt4 key购买 nike

我在我的 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>

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'
}

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;
}

如您所见,当您将鼠标悬停在 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>

关于javascript - 拖动文件时更改元素的 CSS 属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60622268/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com