使用React获取图像尺寸

使用React获取图像尺寸,第1张

使用React获取图像尺寸

检索尺寸的方式

您可以通过js来实现您的目标:通过offsetHeight,offsetWidth。为了获得img的尺寸,img必须可见。您无法从缓存的img获取尺寸。

例如:https://jsbin.com/jibujocawi/1/edit?js,输出

class AtomicImage  extends Component {   constructor(props) {        super(props);        this.state = {dimensions: {}};        this.onImgLoad = this.onImgLoad.bind(this);    }    onImgLoad({target:img}) {        this.setState({dimensions:{height:img.offsetHeight,  width:img.offsetWidth}});    }    render(){        const {src} = this.props;        const {width, height} = this.state.dimensions;        return (<div>     dimensions width{width}, height{height}     <img onLoad={this.onImgLoad} src=http://www.kaotop.com/skin/sinaskin/image/nopic.gif     </div>    );    }}


欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/zaji/5477291.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-12-12
下一篇2022-12-12

发表评论

登录后才能评论

评论列表(0条)

    保存