【发布时间】:2020-11-05 01:00:07
【问题描述】:
我正在尝试在 React 中创建内容滑块,并且在某种程度上我已经成功,但是我希望内容水平移动而不是垂直移动。现在所有的项目都堆叠在一起,我知道这就是为什么它们不能作为水平滑块工作,最初我尝试使用 flexbox 来对齐彼此相邻的项目,但随后它们无法使用 left 移动或直接在 CSS 中。
我目前正在做的是将内部 div(其中包含项目)的顶部设置为计算出的像素数,以便它显示下一个“幻灯片”。我一直在尝试和失败的是尝试让项目彼此相邻,而不是顶部只是更改滑块的左/右以显示项目。
(P.S. 我没有添加一种方法来停止滑块超过项目总数,之后会这样做)
这是反应代码:
import React, { Component } from "react";
import "../App.css";
class Item extends Component {
constructor(props) {
super(props);
this.state = {
id: 1,
top: "0px",
};
}
onClickSlide = (_) => {
const item = this.state.id;
this.setState({ top: item * 200 * -1 + "px" });
this.setState({ id: item + 1 });
};
render() {
let dang = [
{ title: "Header 1", content: "Lorem ipsum proin gravida" },
{ title: "Header 2", content: "Lorem ipsum proin gravida" },
{ title: "Header 3", content: "Lorem ipsum proin gravida" },
{ title: "Header 4", content: "Lorem ipsum proin gravida" },
];
const item = this.state.id;
let innerStyle = {top: item * 200 * -1 + "px"};
return (
<div className="Outer">
<div className="inner" onClick={this.onClickSlide} style={{ top: this.state.top }}>
{dang.map((data, i) => {
return (
<div className="items">
<h3>{data.title}</h3>
<h3>{data.content}</h3>
</div>
);
})}
</div>
</div>
);
}
}
export default Item;
这是 CSS:
.Outer {
display: block;
position: relative;
overflow: hidden;
width: 200px;
height: 200px;
}
.inner {
/* display: block; */
position: absolute;
transition: all 0.5s;
}
.items {
display: inline-block;
width: 200px;
height: 200px;
vertical-align: top;
}
【问题讨论】:
标签: css reactjs animation slider