javascript - react 显示 block 与悬停信息

标签 javascript reactjs ecmascript-6

我有很多带有元素的方 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/

相关文章:

javascript - Component 前面的语法是什么?

javascript - 获取所有已声明模块导入的列表 (SystemJS)

javascript - 如何动态创建 JavaScript 数组(JSON 格式)?

javascript - ExtJS6:在网格面板的列中传递整个对象或 2 个字段

css - 如何在 React 中创建全局样式

reactjs - 在 index.js 文件中导出 react jsx-components

javascript - 单击函数动态创建按钮

javascript - 根据字符串列表数组动态查找indexof

javascript - 命名导出与导出对象

javascript - react 更新现有的 css 类