Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit b7ab306

Browse files
committedSep 12, 2019
fix: delay timeoutId clear
1 parent a45d982 commit b7ab306

8 files changed

+70
-66
lines changed
 

‎example/App.vue

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,10 @@
44
width="400"
55
height="300"
66
zoom-width="400"
7-
zoom-height="300"></ImageMagnifier>
7+
zoom-height="300"
8+
:delay-in="300"
9+
></ImageMagnifier>
810
</template>
9-
1011
<script>
1112
import ImageMagnifier from '../src/components/ImageMagnifier.vue'
1213
import image from './img/DA2D9393-4081-4384-B493-95DA1620C26D.png'

‎lib/magnifier.common.js

Lines changed: 21 additions & 20 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎lib/magnifier.common.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎lib/magnifier.umd.js

Lines changed: 21 additions & 20 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎lib/magnifier.umd.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎lib/magnifier.umd.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎lib/magnifier.umd.min.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎src/components/ImageMagnifier.vue

Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,12 @@
44
:height="height"
55
:src="src"
66
class="image-magnifier__img"
7-
@mouseover="handleOver"
7+
@mouseenter="handleOver"
88
@mousemove="handleMove"
9-
@mouseout="handleOut"
9+
@mouseleave="handleOut"
1010
ref="img"
1111
/>
12-
<div class="image-magnifier__mask" :class="maskClass" :style="maskStyle" v-show="zoomShow" ref="mask">
12+
<div class="image-magnifier__mask" :class="maskClass" :style="maskStyle" ref="mask">
1313
</div>
1414
<div class="image-magnifier__zoom" :class="zoomClass" :style="zoomStyle" v-show="zoomShow">
1515
<img :src="zoomSrc" :style="zoomImgStyle"/>
@@ -73,6 +73,8 @@
7373
x: 0,
7474
y: 0
7575
},
76+
zoomInTimeoutId: null,
77+
zoomOutTimeoutId: null,
7678
}
7779
},
7880
computed: {
@@ -94,7 +96,8 @@
9496
transform: `translate(${this.maskX}px, ${this.maskY}px)`,
9597
willChange: 'transform',
9698
pointerEvents: 'none',
97-
zIndex: 1000
99+
zIndex: 1000,
100+
visibility: this.zoomShow ? 'visible' : 'hidden',
98101
}
99102
},
100103
zoomStyle() {
@@ -123,25 +126,22 @@
123126
},
124127
methods: {
125128
handleOver() {
129+
clearTimeout(this.zoomOutTimeoutId);
130+
this.calcZoomSize();
126131
if (this.delayIn === 0) {
127132
this.zoomShow = true;
128-
this.calcZoomSize();
129-
}else {
130-
setTimeout(() => {
133+
} else {
134+
this.zoomInTimeoutId = setTimeout(() => {
131135
this.zoomShow = true;
132-
this.calcZoomSize();
133-
},this.delayIn)
136+
}, this.delayIn)
134137
}
135138
},
136-
calcZoomSize () {
137-
this.$nextTick(() => {
138-
this.imgRect = this.$refs.img && this.$refs.img.getBoundingClientRect();
139-
this.maskRect = this.$refs.mask && this.$refs.mask.getBoundingClientRect();
140-
141-
//计算大图宽高
142-
this.zoomImgWidth = (this.imgRect.width / this.maskRect.width) * this.zoomWidth;
143-
this.zoomImgHeight = (this.imgRect.height / this.maskRect.height) * this.zoomHeight;
144-
})
139+
calcZoomSize() {
140+
this.imgRect = this.$refs.img && this.$refs.img.getBoundingClientRect();
141+
this.maskRect = this.$refs.mask && this.$refs.mask.getBoundingClientRect();
142+
//计算大图宽高
143+
this.zoomImgWidth = (this.imgRect.width / this.maskRect.width) * this.zoomWidth;
144+
this.zoomImgHeight = (this.imgRect.height / this.maskRect.height) * this.zoomHeight;
145145
},
146146
handleMove(e) {
147147
this.maskX = this.outXCheck(e.pageX - this.imgRect.left);
@@ -153,10 +153,11 @@
153153
this.zoomPosition.y = this.maskY * (this.zoomImgHeight / this.imgRect.height)
154154
},
155155
handleOut() {
156+
clearTimeout(this.zoomInTimeoutId);
156157
if (this.delayOut === 0) {
157158
this.zoomShow = false;
158-
}else {
159-
setTimeout(() => {
159+
} else {
160+
this.zoomOutTimeoutId = setTimeout(() => {
160161
this.zoomShow = false;
161162
}, this.delayOut);
162163
}

0 commit comments

Comments
 (0)
Please sign in to comment.