index.vue 2.2 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125
<template>
  <div
    v-if="visible"
    class="uni-system-preview-image"
    @click="_click"
  >
    <v-uni-swiper
      navigation="auto"
      :current.sync="index"
      :indicator-dots="false"
      :autoplay="false"
      class="uni-system-preview-image-swiper"
    >
      <v-uni-swiper-item
        v-for="(src, key) in urls"
        :key="key"
      >
        <image-view :src="src" />
      </v-uni-swiper-item>
    </v-uni-swiper>

    <div class="nav-btn-back">
      <i class="uni-btn-icon">&#xe650;</i>
    </div>
  </div>
</template>

<script>
import imageView from './image-view'

export default {
  name: 'PreviewImage',
  components: {
    imageView
  },
  props: {
    visible: {
      type: Boolean,
      default: false
    },
    urls: {
      type: Array,
      default: () => []
    },
    current: {
      type: [String, Number],
      default: 0
    }
  },
  data () {
    return {
      index: 0
    }
  },
  watch: {
    visible (val) {
      if (val) {
        const index =
          typeof this.current === 'number'
            ? this.current
            : this.urls.indexOf(this.current)
        this.index = index < 0 ? 0 : index
      }
    }
  },
  mounted () {
    const MAX_MOVE = 20
    let x = 0
    let y = 0
    this.$el.addEventListener('mousedown', event => {
      this.preventDefault = false
      x = event.clientX
      y = event.clientY
    })
    this.$el.addEventListener('mouseup', event => {
      if (
        Math.abs(event.clientX - x) > MAX_MOVE ||
        Math.abs(event.clientY - y) > MAX_MOVE
      ) {
        this.preventDefault = true
      }
    })
  },
  methods: {
    _click () {
      if (!this.preventDefault) {
        this.$emit('close')
      }
    }
  }
}
</script>
<style>
.uni-system-preview-image {
  display: block;
  position: fixed;
  left: 0;
  top: 0;
  width: 100%;
  height: 100%;
  z-index: 999;
  background: rgba(0, 0, 0, 0.8);
}
.uni-system-preview-image-swiper {
  position: absolute;
  left: 0;
  top: 0;
  width: 100%;
  height: 100%;
}
.uni-system-preview-image .nav-btn-back {
  position: absolute;
  box-sizing: border-box;
  top: 0;
  left: 0;
  width: 44px;
  height: 44px;
  padding: 6px;
  line-height: 32px;
  font-size: 26px;
  color: white;
  text-align: center;
  cursor: pointer;
}
</style>