vue3原生实现图片对比

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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
<template>
<div class="picture-compare" @mousemove="onMouseMove" @mouseup="onMouseUp">
<div ref="img" class="img img1">
<img :src="props.img1" alt="" class="pic">
</div>
<div class="slide" ref="line" :style="{left: lineLeft - lineWidth / 2 + 'px'}" @mousedown="onMouseDown">
<div class="line"></div>
</div>
<div class="slide-box" ref="slide" :style="{width: slideWidth + 'px'}">
<div class="img img2" :style="{width: fullWidth + 'px' }">
<img :src="props.img2" alt="" class="pic">
</div>
</div>
</div>
</template>

<script lang="ts" setup>
/**
- 传入两张图片img1和img2即可,须传base64 或 绝对路径
*/

import { ref, onMounted} from 'vue'

const props = defineProps({
img1: {
type: String,
required: true
},
img2: {
type: String,
required: true
}
})

const img = ref<any>(null)
const slide = ref<any>(null)
const line = ref<any>(null)
const fullWidth = ref(0)
const slideWidth = ref(0)
const lineWidth = ref(0)
const lineLeft = ref(0)

const mouseStatus = ref(0) // 1 按下 0 未按下
const px = ref(0)

onMounted(() => {
fullWidth.value = img.value?.offsetWidth
slideWidth.value = fullWidth.value / 2
lineWidth.value = line.value?.offsetWidth
lineLeft.value = fullWidth.value / 2
})

function onMouseDown () {
mouseStatus.value = 1
}

function onMouseMove (e:MouseEvent) {
if (mouseStatus.value === 1) {
const dx = e.pageX - (px.value || e.pageX)
let newWidth = slideWidth.value - dx
let newLeft = lineLeft.value + dx
if (newWidth < 0) newWidth = 0
if (newLeft > fullWidth.value + lineWidth.value) newLeft = fullWidth.value + lineWidth.value
slideWidth.value = newWidth
lineLeft.value = newLeft
px.value = e.pageX
}
}

function onMouseUp () {
mouseStatus.value = 0
}
</script>

<style lang="scss" scoped>
.picture-compare {
position: relative;
width: 100%;
height: 100%;
overflow: hidden;

img {
user-select: none;
}
.img {
position: absolute;
z-index: 1;
right: 0;
top: 0;
width: 100%;
height: 100%;
}

.slide-box {
position: absolute;
right: 0;
z-index: 11;
height: 100%;
overflow: hidden;
}
.slide {
position: absolute;
top: 0;
z-index: 22;
height: 100%;
width: 15px;
cursor: col-resize;

&::before {
content: '';
position: absolute;
left: -8px;
top: 50%;
width: 0;
height: 0;
background-color: transparent;
border-width: 10px 10px 10px 0;
border-style: solid;
border-color: transparent #fff transparent transparent ;
}

&::after {
content: '';
position: absolute;
right: -7px;
top: 50%;
width: 0;
height: 0;
background-color: transparent;
border-width: 10px 0 10px 10px;
border-style: solid;
border-color: transparent transparent transparent #fff;
}


.line {
position: absolute;
left: 50%;
transform: translateX(-50%);
top: 0;
width: 2px;
height: 100%;
background-color: #fff;
color: red;
}
}

}


</style>

<style lang="scss">
.el-select__popper {
background-color: rgba(22, 39, 64, 0.7) !important;
border-color: rgb(26, 66, 133) !important;

.el-select-dropdown__item {
color: #eee;
}

.el-select-dropdown__item.hover,
.el-select-dropdown__item:hover {
background-color: rgb(26, 66, 133) !important;
}

.el-select-dropdown__item.selected {
font-weight: 1000;
color: #fff;
}
}

.el-popper.is-light .el-popper__arrow::before {
border: 1px solid rgb(26, 66, 133) !important;
background: rgba(22, 39, 64, 0.7) !important;
border-right: none !important;
border-bottom: none !important;
}
</style>