123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237 |
- <template>
- <Modal
- :class="prefixCls"
- v-model="modalIsShow"
- title="图片预览"
- fullscreen
- footer-hide
- @on-visible-change="visibleChange"
- >
- <div slot="header"></div>
- <div :class="[`${prefixCls}-close`]" @click="cancel">
- <i class="el-icon-circle-close"></i>
- <Icon type="ios-close" />
- </div>
- <div :class="[`${prefixCls}-body`]" ref="ReviewBody" @click="cancel">
- <div
- :class="[`${prefixCls}-guide`, `${prefixCls}-guide-prev`]"
- @click.stop="showPrev"
- >
- <Icon type="ios-arrow-back" />
- </div>
- <div
- :class="[`${prefixCls}-guide`, `${prefixCls}-guide-next`]"
- @click.stop="showNext"
- >
- <Icon type="ios-arrow-forward" />
- </div>
- <div
- :class="[
- `${prefixCls}-imgs`,
- { [`${prefixCls}-imgs-nosition`]: nosition }
- ]"
- :style="styles"
- v-show="!loading && curImage.imgSrc"
- v-if="modalIsShow"
- >
- <img
- :src="curImage.imgSrc"
- :alt="curImage.name"
- ref="PreviewImgDetail"
- />
- </div>
- <div :class="[`${prefixCls}-none`]" v-if="!curImage.imgSrc">
- <Icon type="md-image" />
- <p>暂无数据</p>
- </div>
- </div>
- <div :class="[`${prefixCls}-footer`]">
- <ul>
- <li title="旋转" @click.stop="toRotate">
- <Icon type="ios-refresh-circle" />
- </li>
- </ul>
- </div>
- <div :class="[`${prefixCls}-loading`]" v-show="loading">
- <Icon class="ivu-load-loop" type="ios-loading" />
- </div>
- </Modal>
- </template>
- <script>
- const prefixCls = "cc-image-preview";
- export default {
- name: "simple-image-preview",
- props: {
- curImage: {
- type: Object,
- default() {
- return {};
- }
- }
- },
- data() {
- return {
- prefixCls,
- modalIsShow: false,
- styles: { width: "", height: "", top: "", left: "", transform: "" },
- initWidth: 500,
- transform: {
- scale: 1,
- rotate: 0
- },
- loading: false,
- loadingSetT: null,
- nosition: false
- };
- },
- // watch: {
- // "curImage.imgSrc": {
- // handler(val) {
- // if (val) this.loading = true;
- // }
- // }
- // },
- methods: {
- visibleChange(visible) {
- if (!visible) return;
- // this.loading = true;
- this.$nextTick(() => {
- this.registfileLoad();
- });
- },
- registfileLoad() {
- const imgDom = this.$refs.PreviewImgDetail;
- imgDom.onload = () => {
- this.rezizeImage(imgDom);
- };
- },
- rezizeImage(imgDom) {
- const { naturalWidth, naturalHeight } = imgDom;
- const imageSize = this.getImageSizePos({
- win: {
- width: this.$refs.ReviewBody.clientWidth,
- height: this.$refs.ReviewBody.clientHeight
- },
- img: {
- width: naturalWidth,
- height: naturalHeight
- },
- rotate: 0
- });
- this.styles = Object.assign(this.styles, {
- width: imageSize.width + "px",
- height: imageSize.height + "px",
- top: imageSize.top + "px",
- left: imageSize.left + "px",
- transform: ""
- });
- this.transform = {
- scale: 1,
- rotate: 0
- };
- // this.loading = false;
- console.log(11);
- setTimeout(() => {
- this.nosition = false;
- }, 100);
- },
- getImageSizePos({ win, img, rotate }) {
- const imageSize = {
- width: 0,
- height: 0,
- top: 0,
- left: 0
- };
- const isHorizontal = !!(rotate % 180);
- const rateWin = isHorizontal
- ? win.height / win.width
- : win.width / win.height;
- const hwin = isHorizontal
- ? {
- width: win.height,
- height: win.width
- }
- : win;
- const rateImg = img.width / img.height;
- if (rateImg <= rateWin) {
- imageSize.height = Math.min(hwin.height, img.height);
- imageSize.width = Math.floor(
- (imageSize.height * img.width) / img.height
- );
- } else {
- imageSize.width = Math.min(hwin.width, img.width);
- imageSize.height = Math.floor(
- (imageSize.width * img.height) / img.width
- );
- }
- imageSize.left = (win.width - imageSize.width) / 2;
- imageSize.top = (win.height - imageSize.height) / 2;
- return imageSize;
- },
- cancel() {
- this.modalIsShow = false;
- this.$emit("on-close");
- },
- open() {
- this.modalIsShow = true;
- },
- showPrev() {
- this.$emit("on-prev");
- // this.initData();
- },
- showNext() {
- this.$emit("on-next");
- // this.initData();
- },
- // dome-move
- setStyleTransform() {
- const { scale, rotate } = this.transform;
- this.styles.transform = `scale(${scale}, ${scale}) rotate(${rotate}deg)`;
- },
- toRotate() {
- this.transform.rotate = this.transform.rotate + 90;
- this.setStyleTransform();
- // 调整图片尺寸
- const { naturalWidth, naturalHeight } = this.$refs.PreviewImgDetail;
- const imageSize = this.getImageSizePos({
- win: {
- width: this.$refs.ReviewBody.clientWidth,
- height: this.$refs.ReviewBody.clientHeight
- },
- img: {
- width: naturalWidth,
- height: naturalHeight
- },
- rotate: this.transform.rotate
- });
- this.styles = Object.assign(this.styles, {
- width: imageSize.width + "px",
- height: imageSize.height + "px",
- top: imageSize.top + "px",
- left: imageSize.left + "px"
- });
- // 360度无缝切换到0度
- if (this.transform.rotate >= 360) {
- setTimeout(() => {
- this.nosition = true;
- this.transform.rotate = 0;
- this.setStyleTransform();
- setTimeout(() => {
- this.nosition = false;
- }, 100);
- }, 200);
- }
- }
- }
- };
- </script>
|