<template>
<div class="root" v-loading="!loading">
<!-- <p>请将下列图片按顺序拖动至方框内</p> -->
<div class="source" :style="{ width: getSourceWidth(sourceList.length) + 'px' }">
<div class="source-box" style="" @mouseenter="handleMouseEnter(index)" @mouseleave="handleMouseLeave"
v-for="(item, index) in sourceList" :key="item.id" draggable="true" @dragstart="handleDragStart(item)"
@dragend="handleDragEnd($event, item, 'source', index)">
<div class="img-container">
<el-image :src="getUrl(item.imgUrl)" v-if="!item.isDorp" fit="contain" class="cover-image" />
<span v-if="!item.isDorp" :style="{ opacity: hoverIndex === index ? '1' : '0' }" class="preview"
@click.stop="toPreview(item.imgUrl)">
<el-icon color="#ccc" size="30">
<ZoomIn />
</el-icon>
</span>
<div class="content"></div>
</div>
</div>
</div>
<transition-group tag="div" class="container mt-30" v-if="loading" v-cloak>
<div class="item" @mouseenter="handleMouseEnter2(i)" @mouseleave="handleMouseLeave2"
:style="{ width: getTargetSize(items.length).width + 'px', height: getTargetSize(items.length).height + 'px' }"
:class="[{ 'active': isActive }, 'item' + i]" v-for="(item, i) in items" :key="item.id" draggable="true"
@dragstart="handleDragStart($event, item, i)" @dragover.prevent="handleDragOver($event, item, i)"
@dragenter="handleDragEnter($event, item, i)" @dragend="handleDragEnd($event, item)">
<span class="num">{{ i + 1 }}</span>
<div class="show-answer" :class="{ 'show-active': show }">
<span class="answer" v-show="item.order">{{ item.order }}</span>
</div>
<div class="img-container" v-if="item.imgUrl">
<el-image :src="getUrl(item.imgUrl)" fit="contain" class="cover-image" />
<div class="content"
:style="{ width: getTargetSize(items.length).width + 'px', height: getTargetSize(items.length).height + 'px' }">
</div>
<span :style="{ opacity: hoverIndex2 === i ? '1' : '0' }" class="preview"
@click.stop="toPreview(item.imgUrl)">
<el-icon color="#ccc" size="30">
<ZoomIn />
</el-icon>
</span>
</div>
</div>
</transition-group>
<div class="btn mt-30" style="text-align: right;">
<el-button type="primary" round size="mini" @click="showAnswer">{{ show ? '关闭答案' : '显示答案' }}</el-button>
<el-button type="primary" round size="mini" @click="randomArray">重做</el-button>
</div>
</div>
</template>
<script setup lang="ts" name="drag-select">
import { ref, reactive, defineEmits, defineProps, nextTick } from "vue";
import { useMessage, useMessageBox } from '@/hooks/message';
import { useAudioStore } from "@/store/audioStore";
import _ from 'lodash'
const audioStore = useAudioStore();
const emit = defineEmits(['viewImg']);
const isPlaying = computed(() => audioStore.isPlaying);
const isLoading = computed(() => audioStore.isLoading);
const props = defineProps({
option: {
type: Object,
default: () => { },
},
guideVoiceUrl: {
type: String,
default: '',
},
guide: {
type: String,
default: '',
},
})
const sourceObj = reactive({})
const targetIndex = ref(null)
const isActive = ref(false)
const show = ref(false)
const loading = ref(false)
const num = ref(0)
const sourceList = ref([])
const items = ref([])
const ending = ref(null)
const dragging = ref(null)
const hoverIndex = ref(null);
const hoverIndex2 = ref(null);
const handleMouseEnter = (index) => {
hoverIndex.value = index;
}
const handleMouseLeave = () => {
hoverIndex.value = null;
}
const handleMouseEnter2 = (index) => {
hoverIndex2.value = index;
}
const handleMouseLeave2 = () => {
hoverIndex2.value = null;
}
const toPreview = (url: string) => {
emit('viewImg', url);
}
const handleDragStart = (e: object, item: object, i: number) => {
// console.log(i);
if (typeof i === "undefined") {
isActive.value = false
} else {
isActive.value = true
}
dragging.value = item
}
const getTargetSize = (id: number) => {
if (id <= 3) {
return { width: 280, height: 190 };
} else if (id == 4) {
return { width: 240, height: 170 };
} else if (id == 5) {
return { width: 200, height: 150 };
} else if (id == 6) {
return { width: 170, height: 130 };
}
}
const getSourceWidth = (id: number) => {
if (id <= 3 || id >= 5) {
return 600;
} else {
return 420;
}
};
const handleDragEnd = (e: object, item: object, type: string, index: number) => {
// console.log(e,item);
if (type == 'source') {
let exists = items.value.some(j => j.order == item.order)
console.log(items.value, targetIndex.value)
if (exists) return false;
let result = (items.value[targetIndex.value].imgUrl !== undefined && items.value[targetIndex.value].imgUrl !== '') ? true : false;
if (result) return false;
// if(exists)return useMessage().error('格子内已有相同图片');
item.id = num.value += 1
items.value[targetIndex.value] = JSON.parse(JSON.stringify(item))
sourceList.value[index].isDorp = true
} else {
if (ending.value.id === dragging.value.id) return false;
let newItems = [...items.value]
const src = newItems.indexOf(dragging.value)
const dst = newItems.indexOf(ending.value)
newItems.splice(src, 1, ...newItems.splice(dst, 1, newItems[src]))
items.value = newItems
nextTick(() => {
dragging.value = null
ending.value = null
})
}
}
const handleDragOver = (e: object) => {
e.dataTransfer.dropEffect = 'move'
}
const handleDragEnter = (e: object, item: object, i: number) => {
targetIndex.value = i
e.dataTransfer.effectAllowed = 'move'
ending.value = item
}
const handlePlay = (url: string, id: string | number) => {
if (isPlaying.value) {
audioStore.stopAudio()
} else {
audioStore.playAudio(url, id);
}
}
const showAnswer = () => {
show.value = !show.value
}
const randomArray = () => {
loading.value = false
num.value = props.option.options.length;
//打乱数组顺序,没有需要可以去掉
sourceList.value = _.shuffle([{ order: 1, imgUrl: 'https://file.tomnclass.com/admin/2024/06/21/fdfb23c338864a22a21f49b707be6f18.PNG' }, { order: 2, imgUrl: 'https://file.tomnclass.com/admin/2024/06/21/92406d639b1a41a8993887fbc7f62db7.PNG' }, { order: 3, imgUrl: 'https://file.tomnclass.com/admin/2024/06/21/b1afb63dd9af464199b0efafeef9bd38.PNG' }, { order: 4, imgUrl: 'https://file.tomnclass.com/admin/2024/06/21/3d39730e18d443d986ee534cc7fc4b6b.PNG' }]).map((item, index) => {
return {
id: index,
...item
}
})
// items.value=[]
isActive.value = false
show.value = false
for (let index = 0; index < props.option.options.length; index++) {
items.value[index] = { id: index }
}
nextTick(() => {
loading.value = true
})
}
onMounted(() => {
randomArray()
})
</script>
<style scoped lang="scss">
:deep(.el-image) {
display: block;
}
.preview {
z-index: 9;
position: absolute;
top: 0px;
right: 5px;
transition: .3s;
}
.answer {
width: 50px;
height: 50px;
font-size: 20px;
text-align: center;
background-color: #51b27d;
// padding: 20px;
display: inline-block;
line-height: 50px;
border-radius: 50%;
opacity: .9;
font-weight: bold;
}
.show-answer {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
background: rgba($color: #000000, $alpha: .3);
display: flex;
justify-content: center;
align-items: center;
opacity: 0;
z-index: 9;
transition: .3s;
}
.show-active {
opacity: 1;
}
.guide {
.guide-title {
font-size: 22px;
// display: inline-block;
margin-right: 10px;
line-height: 30px;
}
.voice {
display: inline-block;
.voice-img {
width: 30px;
height: 30px;
display: inline-block;
}
}
}
.img-container {
position: relative;
.cover-image {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
user-select: none;
// object-fit: cover; /* 确保图片覆盖整个容器且不失真 */
}
.content {
position: relative;
width: 180px;
height: 120px;
z-index: 1;
/* 确保内容显示在图片之上 */
}
}
.source {
display: flex;
justify-content: center;
flex-wrap: wrap;
margin: 0 auto;
.source-box {
margin: 5px;
padding: 5px 2px;
// width: 200px;
text-align: center;
border: 1px solid #ccc;
border-radius: 5px;
}
}
.container {
display: flex;
flex-wrap: wrap;
justify-content: space-around;
.item {
position: relative;
// width: 200px;
min-height: 100px;
// padding: 10px 5px;
padding: 5px 2px;
// margin: 10px;
color: #fff;
border: 1px solid red;
border-radius: 5px;
margin-bottom: 30px;
// background-color: #fff;
.num {
font-size: 24px;
// font-weight: bold;
color: #999;
position: absolute;
// top: 10px;
left: 10px;
z-index: 9;
}
}
.active {
transition: all ease .5s;
}
}
</style>
卡片交换/拖拽交换 排序 vue3
最新推荐文章于 2024-07-07 16:20:57 发布