导入
<StateButton imgt={require('../../img/ask/icon_mb_t.png')}
imgf={require('../../img/ask/icon_mb_f.png')}
text="下载模板" colort='#46C066FF' colorf='#585E6EFF'></StateButton>
代码
import React, { Component } from 'react'
import imgDefultT from '../img/ask/icon_mb_t.png'
import imgDefultF from '../img/ask/icon_mb_f.png'
export class StateButton extends Component {
constructor(props) {
super(props)
this.state = {
text: '',
colort: '',
colorf: '',
imgf: imgDefultF,
imgt: imgDefultT,
isOver: false
}
}
componentWillMount() {
console.log(this.props);
if (this.props) {
this.setState({
text: this.props.text ? this.props.text : '按钮',
colort: this.props.colort ? this.props.colort : '#fff',
colorf: this.props.colorf ? this.props.colorf : '#000',
imgf: this.props.imgf ? this.props.imgf : imgDefultF,
imgt: this.props.imgt ? this.props.imgt : imgDefultT,
})
}
}
render() {
const { text, colort, colorf, isOver, imgf, imgt } = this.state
return (
<div style={{ alignItems: 'center', padding: '5px', display: 'flex', width: '100px', fontSize: "12px" }}
onMouseOver={() => { this.mouseOverEvent() }}
onMouseOut={() => { this.mouseOutEvent() }}>
<img src={isOver ? imgt : imgf} style={{ marginRight: '8px' }} />
<span style={{ color: isOver ? colort : colorf }}>{text}</span>
</div>
)
}
// 鼠标焦点高亮显示
mouseOverEvent() {
this.setState({
isOver: true
})
}
// 鼠标焦点高亮取消
mouseOutEvent() {
this.setState({
isOver: false
})
}
}
export default StateButton