0
我想知道如何獲取圖像的高度和寬度,如果我呈現它的反應風格,所以我可以處理它以將其翻轉爲縱向模式,如果寬度較大比身高。react - 獲取要處理的圖像的寬度/高度
例子:https://codesandbox.io/s/k9kwv0kp93
示例代碼:
index.js
import React from 'react';
import { render } from 'react-dom';
import Hello from './Hello';
const styles = {
fontFamily: 'sans-serif',
textAlign: 'center',
};
const App =() => (
<div style={styles}>
<Hello name="CodeSandbox" />
<h2>Start editing to see some magic happen {'\u2728'}</h2>
</div>
);
render(<App />, document.getElementById('root'));
Hello.js
import React, {Component} from 'react';
export default class Hello extends Component
{
constructor()
{
super();
this.state = {img: null}
this.get_image = this.get_image.bind(this);
}
get_image()
{
let img = <img src="http://via.placeholder.com/350x150" alt="" />;
//manipulate here maybe
this.setState({
img
})
}
render()
{
return (
<div>
<button onClick={this.get_image}>Click me</button>
test
{this.state.img}
</div>
)
}
}
圍繞正確的想法,但它的高度和寬度都返回0。試圖添加一個onLoad它,但它並沒有改變結果:S –
請分享您的嘗試 – felixmosh
我可能會重命名該問題,因爲它更關於onload。 –