简体   繁体   English

在React中的onMouseOver上显示不同的图像

[英]Display different images onMouseOver in React

I'm looking to display a different image for every link I hover over. 我希望为我悬停的每个链接显示不同的图像。 When I hover over each link, both images display on top of each other. 当我将鼠标悬停在每个链接上时,两个图像都显示在彼此的顶部。 I feel as if my issue stems from the conditional, which will show any image I place within it and not just one specific image. 我觉得我的问题似乎源于条件,它将显示我放置在其中的任何图像,而不仅仅是一个特定的图像。

I'm wondering if there's a better approach. 我想知道是否有更好的方法。 Perhaps holding the images within the state? 也许将图像保存在状态内?

My code: 我的代码:

class PhotoIndex extends Component {
state = {
    hover: false
}

mouseOver = () => {
    this.setState({ hover: true })
}

mouseOut = () => {
    this.setState({ hover: false })
}

render() {
    return (
        <Wrapper>
            <IndexWrapper>
                <li>
                    <StyledLink
                        onMouseOver={this.mouseOver}
                        onMouseOut={this.mouseOut}
                        to="/checkered-flag/">Checkered Flag

                        {this.state.hover
                            ?
                            <Fade >
                                <div style={{ position: 'relative' }}>
                                    <img 
                                        style={{ position: 'absolute', top: '-200px', left: '100%' }} 
                                        src={car14} 
                                        alt="red car parked in a parkin lot" 
                                    />
                                </div>
                            </Fade>
                            : null}
                    </StyledLink>
                </li>
                <li>
                    <StyledLink
                        onMouseOver={this.mouseOver}
                        onMouseOut={this.mouseOut}>
                        Birds Nest  

                        {this.state.hover
                            ?
                            <Fade >
                                <div style={{ position: 'relative' }}>
                                    <img 
                                        style={{ position: 'absolute', top: '-200px', left: '100%' }} 
                                        src={car15} 
                                        alt="blue car parked in a grassy field" 
                                    />
                                </div>
                            </Fade>
                            : null}                         
                    </StyledLink>
                </li>
                <li>
                    <StyledLink>The Grand Turret</StyledLink>
                </li>
                <li>
                    <StyledLink>Simulation Theory</StyledLink>
                </li>
            </IndexWrapper>
        </Wrapper>
    )
}

} }

I will try to simplify the code to explain the solution. 我将尝试简化代码以解释该解决方案。 If you wish to go with this solution the images should be numbered in order with a set structure. 如果您希望使用此解决方案,则应使用固定的结构对图像进行编号。 For example car0.jpg ,car1.jpg, car2.jpg ..... 例如car0.jpg,car1.jpg,car2.jpg .....

ImageGetter.js ImageGetter.js

import car1 from './cars/car1.jpg';
import car2 from './cars/car2.jpg';

export default {
  car1,car2
}

In the above code I am importing the images and exporting them so they can be consumed by any component which uses the ImageGetter object. 在上面的代码中,我将导入图像并将其导出,以便任何使用ImageGetter对象的组件都可以使用它们。

PhotoIndex.js PhotoIndex.js

import ImageGetter from './ImageGetter';

class PhotoIndex extends Component {
  constructor() {
    super();
    this.state = {
      carImgNum: '0',
      hover: false
    }
  }

  mouseOver = () => {
    const max = 5; //Max number of images
    const newcarImgNum = Math.floor(Math.random() * Math.floor(max));
    this.setState({ hover: true, carImgNum: newcarImgNum });
  }

  render() {
    const { carImgNum } = this.state;
    return (
        <div onMouseOver={this.mouseOver}>
          <img src={ImageGetter[`car${carImgNum}`]} alt="" />
        </div>
    )
  }
}

export default PhotoIndex;

You will have to create a default state for the number of the image which will be displayed. 您将必须为要显示的图像编号创建默认状态。 Over here the default image displayed will be car0.jpg. 在此处显示的默认图像将为car0.jpg。

In mouseOver function you will have to define how many images are available. 在mouseOver函数中,您将必须定义可用的图像数量。 (You can make the number of images dynamic with some other function too.). (您也可以使用其他功能来使图像数量动态化。)。 It then creates a random number from 0 to the max number you specified and sets the value to the carImgNum state. 然后,它将创建一个从0到您指定的最大数的随机数,并将其值设置为carImgNum状态。

Over in the render method I am de structuring the state to get the carImgNum value. 在render方法中,我正在解构状态以获得carImgNum值。

I then pass the ImageGetter into src of the image tag and dynamically target the image i need to pass using templatestrings. 然后,我将ImageGetter传递到image标记的src中,并使用templatestrings动态定位需要传递的图像。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM