mirror of
https://gitee.com/vant-contrib/vant.git
synced 2025-04-06 03:57:59 +08:00
[improvement] ImagePreview: optimize static methods (#2396)
This commit is contained in:
parent
b81dff92fe
commit
f5e2ab063c
@ -51,6 +51,15 @@ import { range } from '../utils';
|
|||||||
const MAX_ZOOM = 3;
|
const MAX_ZOOM = 3;
|
||||||
const MIN_ZOOM = 1 / 3;
|
const MIN_ZOOM = 1 / 3;
|
||||||
|
|
||||||
|
function getDistance(touches) {
|
||||||
|
return Math.sqrt(
|
||||||
|
Math.abs(
|
||||||
|
(touches[0].clientX - touches[1].clientX) *
|
||||||
|
(touches[0].clientY - touches[1].clientY)
|
||||||
|
)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
export default create({
|
export default create({
|
||||||
name: 'image-preview',
|
name: 'image-preview',
|
||||||
|
|
||||||
@ -156,15 +165,6 @@ export default create({
|
|||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
getDistance(touches) {
|
|
||||||
return Math.sqrt(
|
|
||||||
Math.abs(
|
|
||||||
(touches[0].clientX - touches[1].clientX) *
|
|
||||||
(touches[0].clientY - touches[1].clientY)
|
|
||||||
)
|
|
||||||
);
|
|
||||||
},
|
|
||||||
|
|
||||||
startMove(event) {
|
startMove(event) {
|
||||||
const image = event.currentTarget;
|
const image = event.currentTarget;
|
||||||
const rect = image.getBoundingClientRect();
|
const rect = image.getBoundingClientRect();
|
||||||
@ -183,7 +183,7 @@ export default create({
|
|||||||
this.moving = false;
|
this.moving = false;
|
||||||
this.zooming = true;
|
this.zooming = true;
|
||||||
this.startScale = this.scale;
|
this.startScale = this.scale;
|
||||||
this.startDistance = this.getDistance(event.touches);
|
this.startDistance = getDistance(event.touches);
|
||||||
},
|
},
|
||||||
|
|
||||||
onTouchStart(event) {
|
onTouchStart(event) {
|
||||||
@ -213,7 +213,7 @@ export default create({
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (this.zooming && touches.length === 2) {
|
if (this.zooming && touches.length === 2) {
|
||||||
const distance = this.getDistance(touches);
|
const distance = getDistance(touches);
|
||||||
const scale = (this.startScale * distance) / this.startDistance;
|
const scale = (this.startScale * distance) / this.startDistance;
|
||||||
this.scale = range(scale, MIN_ZOOM, MAX_ZOOM);
|
this.scale = range(scale, MIN_ZOOM, MAX_ZOOM);
|
||||||
}
|
}
|
||||||
|
@ -34,6 +34,10 @@
|
|||||||
<script>
|
<script>
|
||||||
import create from '../utils/create';
|
import create from '../utils/create';
|
||||||
|
|
||||||
|
function makePage(number, text, active) {
|
||||||
|
return { number, text, active };
|
||||||
|
}
|
||||||
|
|
||||||
export default create({
|
export default create({
|
||||||
name: 'pagination',
|
name: 'pagination',
|
||||||
|
|
||||||
@ -100,19 +104,19 @@ export default create({
|
|||||||
|
|
||||||
// Add page number links
|
// Add page number links
|
||||||
for (let number = startPage; number <= endPage; number++) {
|
for (let number = startPage; number <= endPage; number++) {
|
||||||
const page = this.makePage(number, number, number === this.value);
|
const page = makePage(number, number, number === this.value);
|
||||||
pages.push(page);
|
pages.push(page);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Add links to move between page sets
|
// Add links to move between page sets
|
||||||
if (isMaxSized && this.showPageSize > 0 && this.forceEllipses) {
|
if (isMaxSized && this.showPageSize > 0 && this.forceEllipses) {
|
||||||
if (startPage > 1) {
|
if (startPage > 1) {
|
||||||
const previousPageSet = this.makePage(startPage - 1, '...', false);
|
const previousPageSet = makePage(startPage - 1, '...', false);
|
||||||
pages.unshift(previousPageSet);
|
pages.unshift(previousPageSet);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (endPage < pageCount) {
|
if (endPage < pageCount) {
|
||||||
const nextPageSet = this.makePage(endPage + 1, '...', false);
|
const nextPageSet = makePage(endPage + 1, '...', false);
|
||||||
pages.push(nextPageSet);
|
pages.push(nextPageSet);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -138,10 +142,6 @@ export default create({
|
|||||||
this.$emit('input', page);
|
this.$emit('input', page);
|
||||||
this.$emit('change', page);
|
this.$emit('change', page);
|
||||||
}
|
}
|
||||||
},
|
|
||||||
|
|
||||||
makePage(number, text, active) {
|
|
||||||
return { number, text, active };
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
Loading…
x
Reference in New Issue
Block a user