Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Reactjs React,访问一个特定的id(比如css-works:nth-child)_Reactjs_Css Selectors - Fatal编程技术网

Reactjs React,访问一个特定的id(比如css-works:nth-child)

Reactjs React,访问一个特定的id(比如css-works:nth-child),reactjs,css-selectors,Reactjs,Css Selectors,我想在悬停期间添加到第一个和最后一个元素偏移(translateX)(分别为左和右) 在这种情况下,如何写入id的条件 css问题由第n个孩子解决 class Article extends React.Component{ constructor(props) { super(props) this.state = {showIncreaced: null} this.getImgStyle = this.getImgStyle.bind(

我想在悬停期间添加到第一个和最后一个元素偏移(translateX)(分别为左和右)

在这种情况下,如何写入id的条件

css问题由第n个孩子解决

class Article extends React.Component{  
    constructor(props) {
        super(props)
        this.state = {showIncreaced: null}

    this.getImgStyle = this.getImgStyle.bind(this);
    this.increase = this.increase.bind(this);
    }

    increase (incId) {
        this.setState({showIncreaced: incId})
    }

  getImgStyle (id) {
    return {
      width: '20vw',
      marginRight: '0.5vw',
      marginLeft: '0.5vw',
      position: 'relative',
      zIndex: this.state.showIncreaced === id ? '10' : '0',
      transform: this.state.showIncreaced === id ? 'scale(1.5, 1.5)' : 'scale(1, 1)'
    };
  }

    render(){   
        const TipStyle={                        
                marginBottom: '10px'
        }

    return(                     
        <div style={TipStyle}>                      
          <h2 style={{marginBottom: '1px'}}>{this.props.name}</h2>
          <div>
        {[1,2,3].map((id) => {
            return <img style={this.getImgStyle(id)} src={this.props[`img${id}`]} onMouseOver={this.increase.bind(this, id)} onMouseOut={this.increase} />
        })}                         
          </div>
        </div>                  
); 
}
}
类文章扩展了React.Component{
建造师(道具){
超级(道具)
this.state={showIncreaced:null}
this.getImgStyle=this.getImgStyle.bind(this);
this.increase=this.increase.bind(this);
}
增加(incId){
this.setState({showIncreaced:incId})
}
getImgStyle(id){
返回{
宽度:“20vw”,
marginRight:“0.5vw”,
边缘左图:“0.5vw”,
位置:'相对',
zIndex:this.state.showIncremaced==id?'10':'0',
转换:this.state.showIncremeraced==id?'scale(1.5,1.5)''scale(1,1)'
};
}
render(){
常量提示样式={
marginBottom:“10px”
}
报税表(
{this.props.name}
{[1,2,3].map((id)=>{
返回
})}                         
); 
}
}

.map
函数将索引作为第二个参数:

    {[1,2,3].map((id, index) => {
        // if (index === 0) { return other }
        // if (index === [1,2,3].length - 1) { return another }
        return <img style={this.getImgStyle(id)} src={this.props[`img${id}`]} onMouseOver={this.increase.bind(this, id)} onMouseOut={this.increase} />
    })}  
{[1,2,3]。地图((id,索引)=>{
//如果(索引===0){返回其他}
//如果(索引==[1,2,3].length-1{返回另一个}
返回
})}  

即使没有索引,您也可以用下面的方法完成。仅当参数不止一个时才需要此()

下面的一个也是有效的

{[1,2,3].map(id => {
        // if (index === 0) { return other }
        // if (index === [1,2,3].length - 1) { return another }
        return <img key={id} style={this.getImgStyle(id)} src={this.props[`img${id}`]} onMouseOver={this.increase.bind(this, id)} onMouseOut={this.increase} />
    })}  
{[1,2,3].map(id=>{
//如果(索引===0){返回其他}
//如果(索引==[1,2,3].length-1{返回另一个}
返回
})}  

map中的条件,返回sth或sth-elseal,所以在循环中生成img时,不要忘记向img添加一个key={id},并且key应该是唯一的,否则只会呈现第一个jsx元素