1
+ <template >
2
+ <div class =" image-magnifier" >
3
+ <img :width =" width"
4
+ :height =" height"
5
+ :src =" src"
6
+ class =" image-magnifier__img"
7
+ @mouseover =" handleOver"
8
+ @mousemove =" handleMove"
9
+ @mouseout =" handleOut"
10
+ ref =" img"
11
+ />
12
+ <div class =" image-magnifier__mask" :style =" maskStyle" v-show =" zoomShow" ref =" mask" >
13
+ </div >
14
+ <div class =" image-magnifier__zoom" :style =" zoomStyle" v-show =" zoomShow" >
15
+ <img :src =" zoomSrc" :style =" zoomImgStyle" />
16
+ </div >
17
+ </div >
18
+ </template >
19
+
20
+ <script >
21
+ export default {
22
+ name: " ImageMagnifier" ,
23
+ props: {
24
+ width: {
25
+ default: ' auto'
26
+ },
27
+ height: {
28
+ default: ' auto'
29
+ },
30
+ src: {},
31
+ zoomSrc: {},
32
+ zoomWidth: {
33
+ default: ' auto'
34
+ },
35
+ zoomHeight: {
36
+ default: ' auto'
37
+ },
38
+ zoomClass: {},
39
+ maskWidth: {
40
+ default: 100
41
+ },
42
+ maskHeight: {
43
+ default: 100
44
+ },
45
+ maskBgColor: {
46
+ default: ' #409eff'
47
+ },
48
+ maskOpacity: {
49
+ default: .5
50
+ },
51
+ maskClass: {}
52
+ },
53
+ data () {
54
+ return {
55
+ zoomShow: false ,
56
+ imgRect: ' ' ,
57
+ maskRect: ' ' ,
58
+ maskX: 0 ,
59
+ maskY: 0 ,
60
+ zoomImage: ' ' ,
61
+ zoomLeft: ' ' ,
62
+ zoomImgWidth: 0 ,
63
+ zoomImgHeight: 0 ,
64
+ zoomPosition: {
65
+ x: 0 ,
66
+ y: 0
67
+ }
68
+ }
69
+ },
70
+ computed: {
71
+ maskStyle () {
72
+ return {
73
+ position: ' absolute' ,
74
+ width: ` ${ this .maskWidth } px` ,
75
+ height: ` ${ this .maskHeight } px` ,
76
+ opacity: this .maskOpacity ,
77
+ backgroundColor: this .maskBgColor ,
78
+ left: 0 ,
79
+ top: 0 ,
80
+ transform: ` translate(${ this .maskX } px, ${ this .maskY } px)` ,
81
+ pointerEvents: ' none'
82
+ }
83
+ },
84
+ zoomStyle () {
85
+ return {
86
+ width: ` ${ this .zoomWidth } px` ,
87
+ height: ` ${ this .zoomHeight } px` ,
88
+ position: ' absolute' ,
89
+ left: ` ${ this .zoomLeft } px` ,
90
+ top: 0 ,
91
+ overflow: ' hidden'
92
+ }
93
+ },
94
+ zoomImgStyle () {
95
+ return {
96
+ width: ` ${ this .zoomImgWidth } px` ,
97
+ height: ` ${ this .zoomImgHeight } px` ,
98
+ transform: ` translate(-${ this .zoomPosition .x } px, -${ this .zoomPosition .y } px)` ,
99
+ }
100
+ }
101
+
102
+ },
103
+ created () {
104
+
105
+ },
106
+ methods: {
107
+ handleOver () {
108
+ this .zoomShow = true ;
109
+ this .imgRect = this .$refs .img && this .$refs .img .getBoundingClientRect ();
110
+ this .$nextTick (() => {
111
+ this .maskRect = this .$refs .mask && this .$refs .mask .getBoundingClientRect ();
112
+ this .zoomImgWidth = (this .imgRect .width / this .maskRect .width ) * this .zoomWidth ;
113
+ this .zoomImgHeight = (this .imgRect .height / this .maskRect .height ) * this .zoomHeight ;
114
+ })
115
+
116
+
117
+ },
118
+ handleMove (e ) {
119
+ this .maskX = this .outXCheck (e .pageX - this .imgRect .left );
120
+ this .maskY = this .outYCheck (e .pageY - this .imgRect .top );
121
+ this .zoomLeft = this .imgRect .width + 10 ;
122
+ this .zoomPosition .x = this .maskX * (this .zoomImgWidth / this .imgRect .width )
123
+ this .zoomPosition .y = this .maskY * (this .zoomImgHeight / this .imgRect .height )
124
+ },
125
+ handleOut () {
126
+ this .zoomShow = false ;
127
+ },
128
+ outXCheck (x ) {
129
+ x = x - this .maskRect .width / 2 ;
130
+ if (x < 0 ) {
131
+ return 0 ;
132
+ }
133
+ if (x + this .maskRect .width > this .imgRect .width ) {
134
+ return this .imgRect .width - this .maskRect .width ;
135
+ }
136
+ return x;
137
+ },
138
+ outYCheck (y ) {
139
+ y = y - this .maskRect .height / 2 ;
140
+ if (y < 0 ) {
141
+ return 0 ;
142
+ }
143
+ if (y + this .maskRect .height > this .imgRect .height ) {
144
+ return this .imgRect .height - this .maskRect .height ;
145
+ }
146
+ return y;
147
+ }
148
+ }
149
+ }
150
+ </script >
151
+
152
+ <style scoped>
153
+ .image-magnifier {
154
+ position : relative ;
155
+ cursor : move ;
156
+ }
157
+
158
+ .image-magnifier__img {
159
+
160
+ }
161
+
162
+ .image-magnifier__mask {
163
+ }
164
+
165
+ .image-magnifier__zoom {
166
+ }
167
+ </style >
0 commit comments