在反应js中显示父div上的子div

时间:2016-09-09 10:24:03

标签: javascript wordpress reactjs ecmascript-6

你好:)我对js的反应比较新,我试图在另一个div的子节点上应用动画,父div“portfolio-product-item”显示从wp rest api中提取的特色图像,以及子div“portfolio-product-item-details”包含帖子的内容。

我想要做的是在父div中悬停在精选图片上时显示内容,我的代码是这样的,我该如何实现呢?

    import React from 'react';
    import Home from './Home';
    require ('../../app.css');
    require ('../../animate.min.css');
    class Portfolio extends React.Component{
      render(){
       console.log(this.props.data.length);
       var contents=[];
       for (var i = 0; i < this.props.data.length; i++) {
       contents.push(
         <div className="col-xs-12 col-md-4">
            <div id="portfolio-product-item" >
              <img src={this.props.data[i].featured_image} />
              <div ref= "productDetails" id ="portfolio-product-item-details"  dangerouslySetInnerHTML={{__html: this.props.data[i].content.rendered}} />
              </div>
           </div>
        );
    }
    return(
      <div className = "container">
          <div className="row">
            <section className="portfolio">
               <h1>Our Latest Work</h1>
               <p id="below-header">These are some of the works that has been keeping us busy over the years. See for yourself what we can do.</p>
               <div className="col-xs-12 ">
                  {contents}
               </div>
            </section>
          </div>
      </div>
    )
  }
}
export default Portfolio;

2 个答案:

答案 0 :(得分:3)

React允许添加/删除虚拟DOM中的元素。使用onMouseEnter和onMouseLeave设置显示/隐藏状态。

<img 
  onMouseEnter={() => this.setState({ show: true })}
  onMouseLeave={() => this.setState({ show: false })} 
/>

然后根据状态显示/隐藏详细信息:

{this.state.show ? 
    <div ref= "productDetails" id ="portfolio-product-item-details"   
         dangerouslySetInnerHTML={{__html: this.props.data[i].content.rendered}}
    />
: null}

答案 1 :(得分:0)

我的解决方案是这样的

import React from 'react';
import Home from './Home';
require ('../../app.css');
require ('../../animate.min.css');
class Portfolio extends React.Component{
  render(){
  var contents=[];
  for (var i = 0; i < this.props.data.length; i++) {
    var productImage ={
      backgroundImage:'url('+ this.props.data[i].featured_image + ')',
      backgroundSize: '100% 100%'
    }
    contents.push(
      <div className="col-xs-12 col-md-6 col-lg-4">
          <div  id="portfolio-product-item" style ={productImage} >

            <div  ref= "productDetails" id ="portfolio-product-item-details"  dangerouslySetInnerHTML={{__html: this.props.data[i].content.rendered}} />
          </div>
      </div>
    );
  }
    return(
      <div className = "container">
          <div className="row">
            <section className="portfolio">
               <h1>Our Latest Work</h1>
               <p id="below-header">These are some of the works that has been keeping us busy over the years. See for yourself what we can do.</p>
               <div className="col-xs-12 ">
                  {contents}
               </div>
            </section>
          </div>
      </div>
    )
  }
}
export default Portfolio;

和css规则是这样的

  section.portfolio div#portfolio-product-item{
  height:370px;
  width:100%;
  background: #f0f0f0;
  margin:15px;
  position:relative;
  transform: rotate(4deg) ;
  box-shadow: 5px 5px 5px #909090;
  -webkit-font-smoothing: antialiased;
}
section.portfolio div#portfolio-product-item-details{
  height:100%;
  width:100%;
  padding:10px;
  text-align: center;
  color:#ffffff;
  position: absolute;
  top:0;
  background-color: #000000;
  opacity:0;
}
section.portfolio div#portfolio-product-item-details:hover{
  cursor:pointer;
  opacity:0.9;
  transition: all .5s ease-in-out;
}