[英]how to put Custom Next and Prev Scroll Buttons for div react
I have two buttons.我有两个按钮。
Prev
and Next
Button. Prev
和Next
按钮。 On below, I have one horizontal div
with multiple items with scroll bar
.在下方,我有一个
horizontal div
,其中包含多个带scroll bar
的项目。
Here's the UI looks like:这是用户界面的样子:
The Scroll Bar working good.滚动条工作正常。 But I have prev and next buttons.
但我有上一个和下一个按钮。 When I click on two buttons, it should scroll left and right.
当我点击两个按钮时,它应该左右滚动。 I tried.
我试过了。 But I don't know how to make it in react.
但我不知道如何让它做出反应。
Here's the Code I tried:这是我试过的代码:
export default function App() {
const data = [1, 2, 3, 4, 5, 6, 7, 8, 9, 0];
return (
<div>
<div
style={{
display: "flex",
justifyContent: "flex-end",
gap: "50px",
marginBottom: "30px"
}}
>
<div style={{ cursor: "pointer" }}>Prev</div>
<div style={{ cursor: "pointer" }}>Next</div>
</div>
<div
style={{
display: "flex",
gap: "150px",
overflow: "scroll",
backgroundColor: "aqua"
}}
>
{data.map((item, index) => (
<div>item {item}</div>
))}
</div>
<div></div>
</div>
);
}
I don't know how to make it.我不知道该怎么做。 Please help me with some solutions.
请帮我解决一些问题。
You can use scrollLeft property and useRef.您可以使用 scrollLeft 属性和 useRef。 Here is my solution.
这是我的解决方案。
export default function App() {
const data = [1, 2, 3, 4, 5, 6, 7, 8, 9, 0];
const ref = React.useRef(null)
const handleScroll = (offset) => {
if (ref.current) {
ref.current.scrollLeft += offset;
}
}
return (
<div>
<div
style={{
display: "flex",
justifyContent: "flex-end",
gap: "50px",
marginBottom: "30px"
}}
>
<div onClick={() => handleScroll(-30)} style={{ cursor: "pointer" }}>Prev</div>
<div onClick={() => handleScroll(30)} style={{ cursor: "pointer" }}>Next</div>
</div>
<div
style={{
display: "flex",
gap: "150px",
overflow: "scroll",
backgroundColor: "aqua"
}}
ref={ref}
>
{data.map((item, index) => (
<div key={index}>item {item}</div>
))}
</div>
<div></div>
</div>
);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.