diff --git a/packages/taro-components/src/components/image/index.js b/packages/taro-components/src/components/image/index.js index 2ae39d1f8c1e..e7ee5f9dac41 100644 --- a/packages/taro-components/src/components/image/index.js +++ b/packages/taro-components/src/components/image/index.js @@ -9,8 +9,7 @@ class Image extends Nerv.Component { constructor () { super(...arguments) this.state = { - isLoaded: false, - aspectFillMode: 'width' + isLoaded: false } this.imageOnLoad = this.imageOnLoad.bind(this) this.observer = {} @@ -46,11 +45,6 @@ class Image extends Nerv.Component { height: this.imgRef.height } }) - if (this.imgRef.naturalWidth > this.imgRef.naturalHeight) { - this.setState({ aspectFillMode: 'width' }) - } else { - this.setState({ aspectFillMode: 'height' }) - } onLoad && onLoad(e) } @@ -65,7 +59,6 @@ class Image extends Nerv.Component { imgProps, ...reset } = this.props - const { aspectFillMode } = this.state const cls = classNames( 'taro-img', { @@ -75,10 +68,7 @@ class Image extends Nerv.Component { ) const imgCls = classNames( 'taro-img__mode-' + - (mode || 'scaleToFill').toLowerCase().replace(/\s/g, ''), - { - [`taro-img__mode-aspectfill--${aspectFillMode}`]: mode === 'aspectFill' - } + (mode || 'scaleToFill').toLowerCase().replace(/\s/g, '') ) return ( diff --git a/packages/taro-components/src/components/image/style/index.scss b/packages/taro-components/src/components/image/style/index.scss index f8b72794839e..6db37336b3a3 100644 --- a/packages/taro-components/src/components/image/style/index.scss +++ b/packages/taro-components/src/components/image/style/index.scss @@ -13,26 +13,19 @@ img[src=""] { } &__mode { &-scaletofill { + object-fit: contain; width: 100%; height: 100%; } &-aspectfit { - max-width: 100%; - max-height: 100%; + object-fit: contain; + width: 100%; + height: 100%; } &-aspectfill { - position: absolute; - top: 50%; - left: 50%; - transform: translate(-50%, -50%); - &--width { - min-width: 100%; - height: 100%; - } - &--height { - width: 100%; - min-height: 100%; - } + object-fit: cover; + width: 100%; + height: 100%; } &-widthfix { width: 100%;