【发布时间】:2017-11-26 13:48:55
【问题描述】:
我正在尝试将图像缩放为双倍点击放大按钮,
点击缩小按钮
我的问题是,当单击放大按钮时(因此,图像大小是两倍)
如果它的大小超过容器,图像的左侧(或图像的顶部)被切断。
我该怎么办?
同样的问题,CSS Transform scale scrolling issue ...但这不是一个好主意。
因为它在缩小时也会将焦点缩放到“左上角”
图像,所以中心对齐是不可能的
(我想应用 transform: scale(..) using transform-origin: center)
我知道的唯一方法是每次计算图像大小,并为截止应用边距,但很难应用
有什么想法吗? :o
代码如下所示。
constructor() {
super()
this._refs = { ratio: 100 }
}
getImageStyle() {
return {
transform: scale(calc(${this.state.ratio} / 100)),
'transform-origin': 'center'
}
}
zoomIn() {
this.setState({ ratio: this.state.ratio + 25 })
}
zoomIn() {
this.setState({ ratio: this.state.ratio - 25 })
}
render() {
const { src } = this.props
return (
<div
className={styles.wrapper}
<img
style={this.getImageStyle()}
ref={(elem) => setRefToNode(this._refs, 'image', elem)}
className={styles.image}
src={src} />
</div>
)
}
和css。
.wrapper {
display: flex;
position: relative;
width: 100%;
height: 100%;
align-items: center;
justify-content: center;
overflow: scroll;
.image {
position: relative;
max-width: 100%;
max-height: 100%;
background-color: white;
}
}
【问题讨论】:
-
寻求代码帮助的问题必须在问题本身中包含重现该问题所需的最短代码,最好是Stack Snippet。见How to create a Minimal, Complete, and Verifiable example
标签: javascript html css reactjs sass