如何在 ReactJs 中更改 img src onmouseover

** 我想在鼠标悬停时更改图像 src,我动态添加了多个图像。**

    
        const Servicesdata = [
          {
            ID: "01",title: "Power Generation",desc:
              " We have rich experience in building thermal,hydro,and combined cycle power plants. We provide customized ready-to-deploy solutions for power plants including total EPC and comprehensive Balance of Plant (BOP) and Flue-gas desulfurization (FGD) solutions.",imgsrc: "https://www.tataprojects.com/images/Transmission-Line.jpg",imgsrcHover: "https://www.tataprojects.com/images/Sunbstations-min.jpg"
          },{
            ID: "02",title: "Transmission",desc:
              "We have successfully commissioned more than 13,000 kms of transmission lines across multiple voltage levels including 800kv HVDC projects",imgsrc: "https://www.tataprojects.com/images/Sunbstations-min.jpg",{
            ID: "03",title: "Substations",desc:
              "Our optimally designed towers and substation structures allow us to reduce conductor wastage ensuring faster construction and on-time delivery.",imgsrc: "https://www.tataprojects.com/images/Tower-Manufactaring-Unit.jpg",{
            ID: "04",title: "Tower Manufacturing Unit",desc:
              "We have a state-of-the-art manufacturing unit to manufacture transmission line towers and structures. The unit is spread across 40 acres of land.",imgsrc: "https://www.tataprojects.com/images/Smart-Grid-min.jpg",imgsrcHover: "https://www.tataprojects.com/images/Sunbstations-min.jpg"
          }
        ];
    
        export default Servicesdata;
    
    
    
    
    
        import react from "react";
        import Servicesdata from "../data/Servicesdata";
    
        const Services = () => {
          return (
            <>
              <section classname="services">
                <div classname="container mt-5">
                  <div classname="row">
                    <div classname="col-md-12">
                      <h2 classname="text-center heading-style-1">Key Areas</h2>
                    </div>
                  </div>
                  {Servicesdata.map((val,index) => {
                    return (
                      <div classname="row featurette align-items-center">
                        <div classname="col-md-7">
                          <h2 classname="featurette-heading">{val.title}</h2>
                          <p classname="lead">{val.desc}</p>
                        </div>
                        <div classname="col-md-5">
                          <img src={val.imgsrc} classname="img-fluid" />
                        </div>
                      </div>
                    );
                  })}
                </div>
              </section>
            </>
          );
        };
    
        export default Services;
    
    
    
    
    
    
    
    
    
    
        <script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.6.3/umd/react.production.min.js"></script>
        <script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.6.3/umd/react-dom.production.min.js"></script>
    
    
    

CHENZHONGLIANG21 回答:如何在 ReactJs 中更改 img src onmouseover

我们可以使用 onMouseOveronMouseOut 事件处理程序来切换当前悬停图像的图像。

  • 当我们将鼠标悬停在特定对象的图像上时,我们可以将对象的 ID 存储在状态中
  • 并在鼠标移开时将其重置为“”
  • 在渲染中,我们可以使用对象 ID 检查状态中的 ID,如果它们匹配则使用 imgsrcHover 否则使用 imgsrc

const Servicesdata = [{ID:"01",title:"Power Generation",desc:" We have rich experience in building thermal,hydro,and combined cycle power plants. We provide customized ready-to-deploy solutions for power plants including total EPC and comprehensive Balance of Plant (BOP) and Flue-gas desulfurization (FGD) solutions.",imgsrc:"https://www.tataprojects.com/images/Transmission-Line.jpg",imgsrcHover:"https://www.tataprojects.com/images/Sunbstations-min.jpg"},{ID:"02",title:"Transmission",desc:"We have successfully commissioned more than 13,000 kms of transmission lines across multiple voltage levels including 800kv HVDC projects",imgsrc:"https://www.tataprojects.com/images/Sunbstations-min.jpg",{ID:"03",title:"Substations",desc:"Our optimally designed towers and substation structures allow us to reduce conductor wastage ensuring faster construction and on-time delivery.",imgsrc:"https://www.tataprojects.com/images/Tower-Manufactaring-Unit.jpg",{ID:"04",title:"Tower Manufacturing Unit",desc:"We have a state-of-the-art manufacturing unit to manufacture transmission line towers and structures. The unit is spread across 40 acres of land.",imgsrc:"https://www.tataprojects.com/images/Smart-Grid-min.jpg",imgsrcHover:"https://www.tataprojects.com/images/Sunbstations-min.jpg"}];

const { useState } = React;

const Services = () => {
  //Store the currently hovered object's id in the state
  //Initially it'll be ""
  const [currentHoveredId,setCurrentHoveredId] = useState("");
  
  //On mouse over update the id with the cuurent object's ID
  const onMouseOver = (id) => {
    setCurrentHoveredId(id);
  }
  
  //On moving the cursoe out of the image,then reset it to ""
  const onMouseOut = () => {
    setCurrentHoveredId("");
  }
 
  return ( 
    <section className="services">
      <div className="container mt-5">
        <div className="row">
          <div className="col-md-12">
            <h2 className="text-center heading-style-1">Key Areas</h2>
          </div>
        </div>
        {Servicesdata.map((val,index) => { 
          return (
            <div className="row featurette align-items-center" key={val.ID}>
              <div className="col-md-7">
                <h2 className="featurette-heading">{val.title}</h2>
                <p className="lead">{val.desc}</p>
              </div>
              <div className="col-md-5">
              {/* Toggle the image source based on the result of the id in state and the id of the current object */}
                <img src={currentHoveredId === val.ID ? val.imgsrcHover : val.imgsrc} 
                  className="img-fluid" 
                  onMouseOver={() => {onMouseOver(val.ID)}} 
                  onMouseOut={onMouseOut}/>
              </div>
            </div>
          ); 
        })}
      </div>
    </section>
  );
};

ReactDOM.render(<Services />,document.getElementById("react"));
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.8.0/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.8.0/umd/react-dom.production.min.js"></script>

<div id="react"></div>

我已经使用了你的代码,因为它刚刚添加了相应的事件处理程序和上面提到的状态。

,

在代码中进行以下修改:

this.setState({
  servicesData: ServicesData;
})

并在鼠标悬停时调用以下函数,将索引和 newSrc 作为参数传递:

imgSrcUpdate(index,newSrc) {
  let oldData = this.state.servicesData;
  oldData[index][src] = newSrc;
  this.setState({
    servicesData: oldData
  })
}

取而代之的是:

{Servicesdata.map((val,index) => { ...

使用:

{this.state.servicesData.map((val,index) => {....
本文链接:https://www.f2er.com/1275308.html

大家都在问