【发布时间】:2019-02-05 11:04:03
【问题描述】:
我正在尝试使用 flexbox 和 React(没有 JQuery、库或 DOM 操作)做一个 Carousel(别名 Slider)。
为了重新排序项目,我使用了 flexbox 的属性order。但是,此属性不支持任何过渡效果,因此为了实现效果,我正在尝试做与本文解释的相同的事情:https://blog.envylabs.com/the-order-property-flexbox-carousels-87a168567820(使用transform)
尽管如此,我在响应能力、不同步骤(例如将 3 张幻灯片而不是仅一张)、过渡效果方面遇到了很多问题...
如果有人能帮助我解决这个错误的问题,我将不胜感激......经过很多时间后,我现在头疼......
我在 stackblitz 中添加了我的代码:
另外我在这里添加代码:
carousel.js
import React, { Component } from 'react'
import './carousel.scss'
const STEP = 1
export default class Carousel extends Component {
constructor(props) {
super(props)
this.next = this.next.bind(this)
this.prev = this.prev.bind(this)
this.resetSet = this.resetSet.bind(this)
this.state = { ref: 0, isSet: true, isReversing: false }
}
getOrder(index) {
const { items } = this.props
const { ref } = this.state
const order = index - ref
if (order >= 0) {
return order
}
return items.length - order
}
resetSet() {
setTimeout(() => {
this.setState({ isSet: true })
}, 50)
}
next() {
const { ref } = this.state
const { items } = this.props
const newRef = ref + STEP
if (newRef < items.length) {
this.setState({
ref: newRef,
isSet: false,
isReversing: false,
}, this.resetSet)
}
}
prev() {
const { ref } = this.state
const newRef = ref - STEP
if (newRef >= 0) {
this.setState({
ref: newRef,
isSet: false,
isReversing: true,
}, this.resetSet)
}
}
render() {
const { title, items } = this.props
const { isSet, isReversing } = this.state
const classSet = isSet ? 'is-set' : ''
const classReversing = isReversing ? 'is-reversing' : ''
return (
<>
{title && <h2 className="carousel-title">{title}</h2>}
<div className="carousel-wrapper" role="listbox">
<div
role="button"
onClick={this.prev}
tabIndex={0}
className="arrow"
>
⬅️
</div>
<div className={`carousel ${classSet} ${classReversing}`}>
{items.map((item, index) => (
<div
key={item}
style={{ order: this.getOrder(index) }}
className="item"
>{item}</div>
))}
</div>
<div
className="arrow"
role="button"
onClick={this.next}
tabIndex={0}
>
➡️
</div>
</div>
</>
)
}
}
carousel.scss
.carousel-title {
font-family: 'PT_Serif-Web-BoldItalic';
font-size: 20px;
display: grid;
width: 100%;
align-items: center;
text-align: center;
grid-template-columns: minmax(20px, 1fr) auto minmax(20px, 1fr);
grid-gap: 20px;
&:before,
&:after {
content: '';
border-top: 1px solid;
}
}
.carousel-wrapper {
width: 100%;
display: flex;
justify-content: center;
.widget {
margin: 0 10px;
}
.arrow {
cursor: pointer;
align-self: center;
margin: 40px;
}
.carousel {
display: flex;
flex-direction: row;
max-width: 750px;
overflow: hidden;
transform: translateX(100%);
&.is-reversing {
transform: translateX(-100%)
}
&.is-set {
transform: none;
transition: transform 0.5s cubic-bezier(0.23, 1, 0.32, 1);
}
@media(max-width: 1049px){
width: 500px;
}
@media(max-width: 800px){
width: 240px;
}
}
}
.item {
background-color: #f6f6f6;
display: flex;
font-size: 30px;
justify-content: center;
align-items: center;
min-width: 230px;
height: 400px;
margin: 10px
}
body {
overflow: hidden
}
使用示例:
<Carousel title="My carousel" items={[1,2,3,4,5,6,7,8,9]} />
非常感谢您对我的帮助!
【问题讨论】:
-
到目前为止做得很好,我正在检查这个是否可以提供帮助;)
-
@DanielVafidis 我很感激。非常感谢
标签: javascript html css reactjs flexbox