我正在尝试使用 React 和react-transition-group包构建自定义轮播。核心功能正在工作,但我正在努力编写 CSS 以同时执行旧项目的左滑出和新项目的左滑入。任何建议将不胜感激!我在下面包含了我失败的尝试。
HTML
import React, { Component } from 'react';
import { connect } from 'react-redux';
import { showModal } from '../actions';
import { CSSTransition, TransitionGroup } from 'react-transition-group'
class FeatureCarousel extends Component {
constructor(props) {
super(props);
this.state = { activeIndex: 0 };
...
}
...
componentDidMount() {
this.progressSlideshow();
}
progressSlideshow() {
this.timeout = setTimeout(function () {
this.next();
this.progressSlideshow();
}.bind(this), 3000);
}
...
render() {
const { activeIndex } = this.state;
const { articles, level } = this.props;
const slides = articles.map((article, index) => {
let { urlToImage, title } = article;
return (
<CSSTransition
key={index}
classNames="slide"
timeout={{ enter: 500, exit: 500 }}>
<div
key={index}
className="carousel-item"
onMouseEnter={this.handleMouseEnter}
onMouseLeave={this.handleMouseLeave} >
<div className="image-wrapper">
<img src={urlToImage} alt={title} />
</div>
<div className="carousel-caption d-md-block">
<h4>{title}</h4>
</div>
</div>
</CSSTransition>
)
})
return (
<div className="carousel slide" data-ride="carousel">
<div className="carousel-inner">
<TransitionGroup>
{slides[activeIndex]}
</TransitionGroup>
</div>
<a className="carousel-control-prev" role="button" onClick={e => this.previouse()}>
<span className="carousel-control-prev-icon" aria-hidden="true" />
<span className="sr-only">Previous</span>
</a>
<a className="carousel-control-next" role="button" onClick={() => this.next()}>
<span className="carousel-control-next-icon" aria-hidden="true" />
<span className="sr-only">Next</span>
</a>
</div>
);
}
}
export default connect(null, { showModal })(FeatureCarousel);
CSS
.slide-enter {
transform: translate(-100%);
}
.slide-enter.slide-enter-active {
transform: translate(0%);
transition: transform 500ms ease-in-out;
}
.slide-leave {
transform: translate(-100%);
}
.slide-leave.slide-leave-active {
transform: translate(0%);
transition: transform 500ms ease-in-out;
}