reactjs - ReactJS handleHover 更改类名
问题描述
我有行动组件
state = {
highlight: null
}
handleLeave = () => {
this.setState({
highlight: null
})
}
handleHover = (e) =>{
this.setState({
highlight: 'highlight'
})
}
<img onMouseEnter={this.handleHover} onMouseLeave={this.handleLeave} className={`${this.state.highlight}`} src="img" />
但是当我有一些图像时,例如:
<img onMouseEnter={this.handleHover} onMouseLeave={this.handleLeave} className={`${this.state.highlight}`} src="img" />
<img onMouseEnter={this.handleHover} onMouseLeave={this.handleLeave} className={`${this.state.highlight}`} src="img" />
然后,当您将鼠标悬停在其中一个上时,每个人都会获得 className 'highlight',但我只想突出显示我将鼠标悬停在其上的图像。提前感谢您的回答!
解决方案
您只需使用简单的 CSS 即可在您的图像上实现悬停效果。
像这样:
img.anyClassName:hover {
//css stuff
}
这对您的使用来说还不够吗?
推荐阅读
- c++ - 将可取消的作业卸载到需要更新 UI 的不同线程
- python - Python 调用“self.property”和创建返回“self.property”的方法有什么区别?
- java - 对 parseClass 的 GroovyClassLoader 调用成功,即使代码无法编译
- perl - 如何在最新的 perl 上安装额外的 perl 模块(活动状态)
- python - 如何在数组中查找中间数
- c# - 从 Entity Framework web.config 获取连接字符串
- python-3.x - 通过操作和创建新列对数据框进行复杂分组
- c++ - 将 Qt 应用程序与 CMake 和 Visual Studio 链接
- asp.net - 在 RowCreated 中对齐 HeaderCell
- python - Python 函数的 kwargs 参数的键是否保证为字符串类型?