Files
vant/packages/image-preview/src/image-preview.vue
张敏 034c66a77f 修复表单组件样式和单元测试用例 (#7)
* fix: loading small style, search style and dialog style

* fix: scroll to top

* fix mobile scroll

* fix scroll to top

* 文档细节优化

* unit test

* dialog and image-preview unit test

* fix form component style

* fix radio and checkbox style

* fix search component style
2017-04-24 17:33:40 +08:00

93 lines
1.9 KiB
Vue

<template>
<transition name="image-fade">
<div class="van-image-preview" ref="previewContainer" v-show="value" @click="handlePreviewClick">
<van-swipe>
<van-swipe-item v-for="item in images">
<img class="van-image-preview__image" @load="handleLoad" :src="item" alt="">
</van-swipe-item>
</van-swipe>
</div>
</transition>
</template>
<script>
import Popup from 'src/mixins/popup';
import VanSwipe from 'packages/swipe';
import VanSwipeItem from 'packages/swipe-item';
export default {
name: 'van-image-preview',
mixins: [Popup],
components: {
VanSwipe,
VanSwipeItem
},
props: {
overlay: {
default: true
},
lockOnScroll: {
default: true
},
closeOnClickOverlay: {
default: true
}
},
data() {
return {
images: [],
viewportSize: null
};
},
methods: {
handlePreviewClick() {
this.value = false;
},
handleLoad(event) {
const containerSize = this.$refs.previewContainer.getBoundingClientRect();
const ratio = containerSize.width / containerSize.height;
const target = event.currentTarget;
const targetRatio = target.width / target.height;
const centerClass = 'van-image-preview__image--center';
const bigClass = 'van-image-preview__image--big';
if (targetRatio > ratio) {
target.className += (' ' + centerClass);
} else {
target.className += (' ' + bigClass);
}
},
close() {
if (this.closing) return;
this.closing = true;
this.value = false;
/* istanbul ignore else */
if (this.lockOnScroll) {
setTimeout(() => {
if (this.overlay && this.bodyOverflow !== 'hidden') {
document.body.style.overflow = this.bodyOverflow;
}
this.bodyOverflow = null;
}, 200);
}
this.opened = false;
this.doAfterClose();
}
}
};
</script>