Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(components): h5端image组件缩放方式使用css实现 #6205

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 2 additions & 12 deletions packages/taro-components/src/components/image/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {}
Expand Down Expand Up @@ -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)
}

Expand All @@ -65,7 +59,6 @@ class Image extends Nerv.Component {
imgProps,
...reset
} = this.props
const { aspectFillMode } = this.state
const cls = classNames(
'taro-img',
{
Expand All @@ -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 (
Expand Down
21 changes: 7 additions & 14 deletions packages/taro-components/src/components/image/style/index.scss
Original file line number Diff line number Diff line change
Expand Up @@ -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%;
Expand Down