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
181
182
183
<template>
<!-- 放大镜 -->
<div style="display: flex;position: relative">
<div
class="box Magnifying"
:style="minImgBoxStyle"
@mouseleave="mouseLeave"
@mouseenter="mouseEnter"
@mousemove="mousemove($event)"
>
<!-- <div>{{title}}</div> -->
<!--原始照片-小照片 :src="finalMinIMGsrc"-->
<img ref="minImg" :style="minImgStyle" fit="contain" :src="imageurl">
<!--探测块-->
<div v-show="show" class="areaMark" :style="areaMarkStyle" />
</div>
<div v-show="show" class="box maxImgBox" :style="maxImgBoxStyle">
<!--放大后的照片-->
<img :style="maxImgStyle" fit="contain" :src="imageurl">
</div>
</div>
</template>
<script>
export default {
name: 'Magnifying',
props: {
imageurl: {},
imgsurl: {}, // 父组件图片
// imageurl: '',
minIMGsrc: String,
maxIMGsrc: String,
scale: {
type: Number,
default: 2
},
width: {
type: Number,
default: 380
},
height: {
type: Number,
default: 380
}
},
data() {
return {
show: false,
finalMinIMGsrc: '',
finalMaxIMGsrc: '',
imgBoxWidth: 380,
imgBoxHeight: 380,
areaWidth: 210,
areaHeight: 210,
areaMarkStyle: {},
minImgBoxStyle: {
cursor: 'move'
},
minImgStyle: {},
maxImgBoxStyle: {},
maxImgStyle: {
position: 'absolute'
}
}
},
watch: {
imgsurl: function(newVal, oldVal) {
this.finalMinIMGsrc = newVal // 监听父组件图片 改变组件内图片地址
console.log(newVal)
},
minIMGsrc() {
this.init()
},
maxIMGsrc() {
this.init()
}
},
mounted() {
this.init()
},
methods: {
init() {
this.imgBoxWidth = this.width
this.imgBoxHeight = this.height
this.$set(this.minImgStyle, 'width', this.imgBoxWidth + 'px')
this.$set(this.minImgStyle, 'height', this.imgBoxHeight + 'px')
this.$set(this.maxImgStyle, 'width', this.imgBoxWidth + 'px')
this.$set(this.maxImgStyle, 'height', this.imgBoxHeight + 'px')
this.$set(this.minImgBoxStyle, 'width', this.imgBoxWidth + 'px')
this.$set(this.minImgBoxStyle, 'height', this.imgBoxHeight + 'px')
this.$set(this.maxImgBoxStyle, 'width', this.imgBoxWidth + 'px')
this.$set(this.maxImgBoxStyle, 'height', this.imgBoxHeight + 'px')
this.$set(this.maxImgBoxStyle, 'left', this.imgBoxWidth + 'px')
this.areaWidth = this.imgBoxWidth / this.scale
this.areaHeight = this.imgBoxHeight / this.scale
this.finalMinIMGsrc = this.minIMGsrc
if (!this.maxIMGsrc) {
this.finalMaxIMGsrc = this.minIMGsrc
}
this.$set(this.areaMarkStyle, 'width', this.areaWidth + 'px')
this.$set(this.areaMarkStyle, 'height', this.areaHeight + 'px')
this.$set(this.maxImgStyle, 'transform', 'scale(' + this.scale + ')')
},
mouseEnter() {
this.show = true
},
mouseLeave() {
this.show = false
},
mousemove(e) {
// 获取文档顶端与屏幕顶部之间的距离
// scrollTop指的是“元素中的内容”超出“元素上边界”的那部分的高度
const documentScrollTop =
document.documentElement.scrollTop || document.body.scrollTop
// 获取鼠标相对于屏幕的坐标
const mouseClientX = e.clientX
const mouseClientY = e.clientY
// 获取小照片相对于屏幕位置信息
// getBoundingClientRect()用于获得页面中某个元素的左,上,右和下分别相对浏览器视窗的位置。
const minImgPosition = this.$refs.minImg.getBoundingClientRect()
const minImgX = minImgPosition.left
const minImgY = minImgPosition.top
// 计算出探测块相对于小图片的坐标
let areaLeft = mouseClientX - minImgX - this.areaWidth / 2
let areaTop = mouseClientY - minImgY - this.areaHeight / 2
if (documentScrollTop > 0) {
areaTop = documentScrollTop + areaTop
}
const minLeft = 0
const maxLeft = this.imgBoxWidth - this.areaWidth
const minTop = 0
const maxTop = this.imgBoxHeight - this.areaHeight
// 禁止探测块移出小图片
if (areaLeft < minLeft) {
areaLeft = minLeft
}
if (areaLeft > maxLeft) {
areaLeft = maxLeft
}
if (areaTop < minTop) {
areaTop = minTop
}
if (areaTop > maxTop) {
areaTop = maxTop
}
// 更新探测块的坐标
this.$set(this.areaMarkStyle, 'left', areaLeft + 'px')
this.$set(this.areaMarkStyle, 'top', areaTop + 'px')
// 更新放大后照片的坐标
this.$set(
this.maxImgStyle,
'left',
((this.scale - 1) * this.imgBoxWidth) / 2 - areaLeft * this.scale + 'px'
)
this.$set(
this.maxImgStyle,
'top',
((this.scale - 1) * this.imgBoxHeight) / 2 - areaTop * this.scale + 'px'
)
}
}
}
</script>
<style scoped>
.Magnifying {
height: 380px;
width: 380px;
}
.box {
border: 1px solid darkgray;
position: relative;
overflow: hidden;
box-sizing: border-box;
}
.areaMark {
position: absolute;
background: url(//img-tmdetail.alicdn.com/tps/i4/T12pdtXaldXXXXXXXX-2-2.png);
}
.maxImgBox {
position: absolute;
z-index: 999;
}
</style>