大屏幕可视化
import React, { Component } from 'react';
import debounce from 'lodash.debounce'
import s from './index.less'
class Comp extends Component{
constructor(p) {
super(p)
this.state={
scale: this.getScale()
}
}
componentDidMount() {
window.addEventListener('resize', this.setScale)
}
getScale=() => {
const {width=1920, height=1080} = this.props
let ww=window.innerWidth/width
let wh=window.innerHeight/height
return ww<wh?ww: wh
}
setScale = debounce(() => {
let scale=this.getScale()
this.setState({ scale })
}, 500)
render() {
const {width=1920, height=1080, children} = this.props
const {scale} = this.state
return(
<div
className={s['scale-box']}
style={{
transform: `scale(${scale}) translate(-50%, -50%)`,
WebkitTransform: `scale(${scale}) translate(-50%, -50%)`,
width,
height
}}
>
{children}
</div>
)
}
componentWillUnmount() {
window.removeEventListener('resize', this.setScale)
}
}
export default Comp
本文来自博客园,作者:玖捌,转载请注明原文链接:https://www.cnblogs.com/fyh0912/p/15745269.html
浙公网安备 33010602011771号