index.vue 4.5 KB
Newer Older
fxy060608's avatar
fxy060608 已提交
1 2
<template>
  <uni-image v-bind="$attrs">
fxy060608's avatar
fxy060608 已提交
3 4
    <div ref="content" :style="modeStyle" />
    <img :src="realImagePath" />
fxy060608's avatar
fxy060608 已提交
5 6 7 8 9 10 11 12
    <v-uni-resize-sensor
      v-if="mode === 'widthFix'"
      ref="sensor"
      @resize="_resize"
    />
  </uni-image>
</template>
<script>
fxy060608's avatar
fxy060608 已提交
13
import { getRealPath } from '@dcloudio/uni-platform'
fxy060608's avatar
fxy060608 已提交
14 15 16 17 18
export default {
  name: 'Image',
  props: {
    src: {
      type: String,
fxy060608's avatar
fxy060608 已提交
19
      default: '',
fxy060608's avatar
fxy060608 已提交
20 21 22
    },
    mode: {
      type: String,
fxy060608's avatar
fxy060608 已提交
23
      default: 'scaleToFill',
fxy060608's avatar
fxy060608 已提交
24 25 26 27
    },
    // TODO 懒加载
    lazyLoad: {
      type: [Boolean, String],
fxy060608's avatar
fxy060608 已提交
28 29
      default: false,
    },
fxy060608's avatar
fxy060608 已提交
30
  },
fxy060608's avatar
fxy060608 已提交
31
  data() {
fxy060608's avatar
fxy060608 已提交
32 33 34
    return {
      originalWidth: 0,
      originalHeight: 0,
fxy060608's avatar
fxy060608 已提交
35
      availHeight: '',
fxy060608's avatar
fxy060608 已提交
36 37 38
    }
  },
  computed: {
fxy060608's avatar
fxy060608 已提交
39 40 41 42
    ratio() {
      return this.originalWidth && this.originalHeight
        ? this.originalWidth / this.originalHeight
        : 0
fxy060608's avatar
fxy060608 已提交
43
    },
fxy060608's avatar
fxy060608 已提交
44 45
    realImagePath() {
      return getRealPath(this.src)
fxy060608's avatar
fxy060608 已提交
46
    },
fxy060608's avatar
fxy060608 已提交
47
    modeStyle() {
fxy060608's avatar
fxy060608 已提交
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
      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':
          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-position:${position};background-size:${size};background-repeat:${repeat};`
fxy060608's avatar
fxy060608 已提交
98
    },
fxy060608's avatar
fxy060608 已提交
99 100
  },
  watch: {
fxy060608's avatar
fxy060608 已提交
101
    src(newValue, oldValue) {
fxy060608's avatar
fxy060608 已提交
102 103 104
      this._setContentImage()
      this._loadImage()
    },
fxy060608's avatar
fxy060608 已提交
105
    mode(newValue, oldValue) {
fxy060608's avatar
fxy060608 已提交
106 107 108 109 110 111
      if (oldValue === 'widthFix') {
        this.$el.style.height = this.availHeight
      }
      if (newValue === 'widthFix' && this.ratio) {
        this._fixSize()
      }
fxy060608's avatar
fxy060608 已提交
112
    },
fxy060608's avatar
fxy060608 已提交
113
  },
fxy060608's avatar
fxy060608 已提交
114
  mounted() {
fxy060608's avatar
fxy060608 已提交
115 116 117 118 119 120 121 122
    this.availHeight = this.$el.style.height || ''
    this._setContentImage()
    if (!this.realImagePath) {
      return
    }
    this._loadImage()
  },
  methods: {
fxy060608's avatar
fxy060608 已提交
123
    _resize() {
fxy060608's avatar
fxy060608 已提交
124 125 126 127
      if (this.mode === 'widthFix') {
        this._fixSize()
      }
    },
fxy060608's avatar
fxy060608 已提交
128
    _fixSize() {
fxy060608's avatar
fxy060608 已提交
129 130 131 132
      const elWidth = this._getWidth()
      if (elWidth) {
        let height = elWidth / this.ratio
        // fix: 解决 Chrome 浏览器上某些情况下导致 1px 缝隙的问题
fxy060608's avatar
fxy060608 已提交
133 134 135 136 137
        if (
          typeof navigator &&
          navigator.vendor === 'Google Inc.' &&
          height > 10
        ) {
fxy060608's avatar
fxy060608 已提交
138 139 140 141 142
          height = Math.round(height / 2) * 2
        }
        this.$el.style.height = height + 'px'
      }
    },
fxy060608's avatar
fxy060608 已提交
143 144 145 146
    _setContentImage() {
      this.$refs.content.style.backgroundImage = this.src
        ? `url("${this.realImagePath}")`
        : 'none'
fxy060608's avatar
fxy060608 已提交
147
    },
fxy060608's avatar
fxy060608 已提交
148
    _loadImage() {
fxy060608's avatar
fxy060608 已提交
149 150 151 152 153 154 155 156 157 158 159 160
      const _self = this
      const img = new Image()
      img.onload = function ($event) {
        _self.originalWidth = this.width
        _self.originalHeight = this.height

        if (_self.mode === 'widthFix') {
          _self._fixSize()
        }

        _self.$trigger('load', $event, {
          width: this.width,
fxy060608's avatar
fxy060608 已提交
161
          height: this.height,
fxy060608's avatar
fxy060608 已提交
162 163 164 165
        })
      }
      img.onerror = function ($event) {
        _self.$trigger('error', $event, {
fxy060608's avatar
fxy060608 已提交
166
          errMsg: `GET ${_self.src} 404 (Not Found)`,
fxy060608's avatar
fxy060608 已提交
167 168 169 170
        })
      }
      img.src = this.realImagePath
    },
fxy060608's avatar
fxy060608 已提交
171
    _getWidth() {
fxy060608's avatar
fxy060608 已提交
172
      const computedStyle = window.getComputedStyle(this.$el)
fxy060608's avatar
fxy060608 已提交
173 174 175 176 177 178
      const borderWidth =
        (parseFloat(computedStyle.borderLeftWidth, 10) || 0) +
        (parseFloat(computedStyle.borderRightWidth, 10) || 0)
      const paddingWidth =
        (parseFloat(computedStyle.paddingLeft, 10) || 0) +
        (parseFloat(computedStyle.paddingRight, 10) || 0)
fxy060608's avatar
fxy060608 已提交
179
      return this.$el.offsetWidth - borderWidth - paddingWidth
fxy060608's avatar
fxy060608 已提交
180 181
    },
  },
fxy060608's avatar
fxy060608 已提交
182
}
fxy060608's avatar
fxy060608 已提交
183
</script>