gpt4 book ai didi

javascript - react 显示 block 与悬停信息

转载 作者:行者123 更新时间:2023-12-03 01:09:55 28 4
gpt4 key购买 nike

我有很多带有元素的方 block 。当用户将鼠标悬停在项目上时,我想显示项目信息(项目附近的 block )。这是一个例子: First example Second example

最佳答案

您可以通过以下方式在 React 中实现此目的。

class nameOfClass extends React.Component{
constructor(props){
super(props)
this.state = {
box1Hover:false,
box2Hover:false,
box3Hover:false,
box4Hover:false,
}
this.trueDisplay = this.trueDisplay.bind(this)
this.falseDisplay = this.falseDisplay.bind(this)
}
trueDisplay(e){

this.setState({[e.target.name]:true})
}
falseDisplay(e){

this.setState({[e.target.name]:false})
}
render(){
return(
<div>
<div name="box1Hover" onMouseEnter={this.trueHover} onMouseLeave={this.falseHover}>
....
// your on hover content in bellow line
{this.state.box1Hover?<h5>Mouse hovering over me</h5>:""}
....
<div>
<div name="box2Hover" onMouseEnter={this.trueHover} onMouseLeave={this.falseHover}>
....
// your on hover content in bellow line
{this.state.box2Hover?<h5>Mouse hovering over me</h5>:""}
....
<div>
<div name="box3Hover" onMouseEnter={this.trueHover} onMouseLeave={this.falseHover}>
....
// your on hover content in bellow line
{this.state.box3Hover?<h5>Mouse hovering over me</h5>:""}
....
<div>
</div>
)
}
}

if you know jquery then you can use tooltip https://www.w3schools.com/bootstrap/bootstrap_tooltip.asp

关于javascript - react 显示 block 与悬停信息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52242977/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com