
检索尺寸的方式
您可以通过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> ); }}欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)