简体   繁体   English

第一次使用React-Transition-Group

[英]First time using React-Transition-Group

I have a image viewer component that as one large image and 3 smaller ones below it. 我有一个图像查看器组件,它是一个大图像,下面是三个小图像。 When you hover over one of the bottom images it changes the large image to the corresponding image. 当您将鼠标悬停在底部图像之一上时,它将大图像更改为相应的图像。 I would like to add a fade-in/fade-out transition when the larger image changes. 当较大的图像更改时,我想添加一个淡入/淡出过渡。 So far I can get a fade-in and a fade-out on the mouse completely leaving the smaller image but, not hovering between the images. 到目前为止,我可以在鼠标上进行淡入和淡出,从而完全留下较小的图像,但不会在图像之间徘徊。 Here is my code: 这是我的代码:

import React, { Component } from 'react';
import Transition from 'react-transition-group/Transition'
import Image0 from './Image1.jpg'
import Image1 from './Image2.jpg'
import Image2 from './Image3.jpg'
import './ImageViewer.css';

const duration = 300;
const Images = [Image0, Image1, Image2]

const defaultStyle = {
  transition: `opacity ${duration}ms ease-in-out`,
  opacity: 0,
}

const transitionStyles = {
  entering: { opacity: 1 },
  entered:  { opacity: 1 },
  exiting: { opacity: .9 },
  exited: { opacity: 0.01 }
};

const Fade = ({ in: inProp, currentImage }) => (
  <Transition in={inProp} timeout={duration}>
    {(state) => (
      <div style={{
        ...defaultStyle,
        ...transitionStyles[state]
      }}>
        <img src={currentImage}/>
      </div>
    )}
  </Transition>
);

class App extends Component {
  state = { show: false, currentImg: Images[0] }
  handleClick = e => {
    this.setState({
      show: !this.state.show,
      currentImg: Images[Number(e.currentTarget.dataset.id)]
    })
  }
  render() {
    return (
      <div className="App">
        <Fade in={this.state.show} currentImage={this.state.currentImg}/> 
          <div className="small_image_wrapper">
          <img  className="small_image" 
                src={Image0} 
                alt="A wonderful bed"
                data-id={0}
                onMouseOver={this.handleClick}
                onMouseOut={this.handleClick}
            />
          <img  className="small_image"
                src={Image1} 
                alt="A wonderful bed"
                data-id={1}
                onMouseOver={this.handleClick}
                onMouseOut={this.handleClick}
            />
          <img  className="small_image"
                src={Image2} 
                alt="A wonderful bed"
                data-id={2}
                onMouseOver={this.handleClick}
                onMouseOut={this.handleClick}
            />

        </div>
      </div>
    );
  }
}

export default App;

Also here's a link to what I have so far with transitions: https://peaceful-jones-ee2ca2.netlify.com/ And without transitions: https://practical-lamport-9cc94e.netlify.com/ 另外,这里是我到目前为止在过渡方面的链接: https : //peaceful-jones-ee2ca2.netlify.com/在没有过渡的情况下: https : //practical-lamport-9cc94e.netlify.com/

I am not sure but you have to wrap all your images into TransitionGroup 我不确定,但是您必须将所有图像包装到TransitionGroup

import {TransitionGroup}, {Transition} from 'react-transition-group';
...
render() {
return (
  <div className="App">
    <TransitionGroup>
       <Fade in={this.state.show} currentImage={this.state.currentImg}/> 
    </TransitionGroup>
    <div className="small_image_wrapper">
       /* here comes small images */
    </div>
  </div>
)};

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

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