index.vue 5.8 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
<template>
  <uni-image v-on="$listeners">
    <div
      ref="content"
      :style="style"
    />
    <v-uni-resize-sensor
      v-if="mode === 'widthFix' || mode === 'heightFix'"
      ref="sensor"
      @resize="_fixSize()"
    />
  </uni-image>
</template>
<script>
function fixNumber (number) {
  // fix: 解决 Chrome 浏览器上某些情况下导致 1px 缝隙的问题
  if (typeof navigator && navigator.vendor === 'Google Inc.' && number > 10) {
    number = Math.round(number / 2) * 2
  }
  return number
}

export default {
  name: 'Image',
  props: {
    src: {
      type: String,
      default: ''
    },
    mode: {
      type: String,
      default: 'scaleToFill'
    },
    // TODO 懒加载
    lazyLoad: {
      type: [Boolean, String],
      default: false
38 39 40
    },
    draggable: {
      type: Boolean,
41
      default: false
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
    }
  },
  data () {
    return {
      originalWidth: 0,
      originalHeight: 0,
      originalStyle: { width: '', height: '' },
      contentPath: ''
    }
  },
  computed: {
    ratio () {
      return this.originalWidth && this.originalHeight ? this.originalWidth / this.originalHeight : 0
    },
    style () {
      let size = 'auto'
      let position = ''
      const repeat = 'no-repeat'

      switch (this.mode) {
        case 'aspectFit':
          size = 'contain'
          position = 'center center'
          break
        case 'aspectFill':
          size = 'cover'
          position = 'center center'
          break
        case 'widthFix':
        case 'heightFix':
          size = '100% 100%'
          break
        case 'top':
          position = 'center top'
          break
        case 'bottom':
          position = 'center bottom'
          break
        case 'center':
          position = 'center center'
          break
        case 'left':
          position = 'left center'
          break
        case 'right':
          position = 'right center'
          break
        case 'top left':
          position = 'left top'
          break
        case 'top right':
          position = 'right top'
          break
        case 'bottom left':
          position = 'left bottom'
          break
        case 'bottom right':
          position = 'right bottom'
          break
        default:
          size = '100% 100%'
          position = '0% 0%'
          break
      }

      return {
        'background-image': this.contentPath ? `url("${this.contentPath}")` : 'none',
        'background-position': position,
        'background-size': size,
        'background-repeat': repeat
      }
    }
  },
  watch: {
    src (newValue, oldValue) {
      this._loadImage()
    },
    mode (newValue, oldValue) {
      if (oldValue === 'widthFix' || oldValue === 'heightFix') {
        this._resetSize()
      }
      if (newValue === 'widthFix' || newValue === 'heightFix') {
        this._fixSize()
      }
Q
qiang 已提交
126 127 128 129 130 131
    },
    contentPath (val) {
      if (!val && this.__img) {
        this.__img.remove()
        delete this.__img
      }
132 133 134 135 136 137 138 139 140 141 142 143 144 145 146
    }
  },
  mounted () {
    this.originalStyle.width = this.$el.style.width || ''
    this.originalStyle.height = this.$el.style.height || ''
    this._loadImage()
  },
  beforeDestroy () {
    this._clearImage()
  },
  methods: {
    _fixSize () {
      if (this.ratio) {
        const $el = this.$el
        if (this.mode === 'widthFix') {
147
          const width = $el.offsetWidth
148 149 150 151
          if (width) {
            $el.style.height = fixNumber(width / this.ratio) + 'px'
          }
        } else if (this.mode === 'heightFix') {
152
          const height = $el.offsetHeight
153 154 155 156 157
          if (height) {
            $el.style.width = fixNumber(height * this.ratio) + 'px'
          }
        }
      }
158
      window.dispatchEvent(new CustomEvent('updateview'))
159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174
    },
    _resetSize () {
      this.$el.style.width = this.originalStyle.width
      this.$el.style.height = this.originalStyle.height
    },
    _resetData () {
      this.originalWidth = 0
      this.originalHeight = 0
      this.contentPath = ''
    },
    _loadImage () {
      const realImagePath = this.$getRealPath(this.src)
      if (realImagePath) {
        const img = this._img = this._img || new Image()
        img.onload = $event => {
          this._img = null
Q
qiang 已提交
175 176
          const width = this.originalWidth = img.width
          const height = this.originalHeight = img.height
177 178 179 180

          this._fixSize()

          this.contentPath = realImagePath
Q
qiang 已提交
181 182 183 184 185 186
          img.draggable = this.draggable
          if (this.__img) {
            this.__img.remove()
          }
          this.__img = img
          this.$el.appendChild(img)
187 188

          this.$trigger('load', $event, {
Q
qiang 已提交
189 190
            width,
            height
191 192 193 194 195 196 197 198 199 200 201 202 203 204
          })
        }
        img.onerror = $event => {
          this._img = null
          this._resetData()
          // 与微信小程序保持一致,保留之前样式
          // this._resetSize()
          this.$trigger('error', $event, {
            errMsg: `GET ${this.src} 404 (Not Found)`
          })
        }
        img.src = realImagePath
      } else {
        this._clearImage()
205
        this._resetData()
206
        // 与微信小程序保持一致,保留之前样式
207
        // this._resetSize()
208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233
      }
    },
    _clearImage () {
      const img = this._img
      if (img) {
        img.onload = null
        img.onerror = null
        this._img = null
      }
    }
  }
}
</script>
<style>
uni-image {
  width: 320px;
  height: 240px;
  display: inline-block;
  overflow: hidden;
  position: relative;
}

uni-image[hidden] {
  display: none;
}

Q
qiang 已提交
234
uni-image>div {
235 236 237 238
  width: 100%;
  height: 100%;
}

Q
qiang 已提交
239
uni-image>img {
240 241 242 243 244 245 246 247 248 249 250 251
  -webkit-touch-callout: none;
  -webkit-user-select: none;
  -moz-user-select: none;
  display: block;
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  opacity: 0;
}

Q
qiang 已提交
252
uni-image>.uni-image-will-change {
253 254 255
  will-change: transform;
}
</style>