要获取Image组件中显示内容的真实大小,可以使用以下方法:
var img = document.getElementById('myImage');
var realWidth = img.naturalWidth;
var realHeight = img.naturalHeight;
class MyComponent extends React.Component {
constructor(props) {
super(props);
this.imageRef = React.createRef();
}
componentDidMount() {
const img = this.imageRef.current;
const realWidth = img.naturalWidth;
const realHeight = img.naturalHeight;
console.log("Real width: " + realWidth);
console.log("Real height: " + realHeight);
}
render() {
return <img ref={this.imageRef} src="example.jpg" />;
}
}
var img = document.getElementById('myImage');
var computedStyle = window.getComputedStyle(img);
var realWidth = parseFloat(computedStyle.width);
var realHeight = parseFloat(computedStyle.height);
这些方法可以帮助您获取Image组件中显示内容的真实大小,根据实际情况选择适合您的方法。
领取专属 10元无门槛券
手把手带您无忧上云