javascript - Scroll an overflow component in reactnext - Stack Overflow

So I have a ponent that have a fixed width with an overflow-x-auto. I decided to hide the scroll bar an

So I have a ponent that have a fixed width with an overflow-x-auto. I decided to hide the scroll bar and replace it with 2 arrow/button both left and right. If I click left, it scroll to left and vice versa. How can I achieve this functionality ?

this is my ponent

<div className={` vertical-card w-7/12 flex flex-row overflow-x-scroll no-scrollbar pb-4`}>
            {
                dataConselor.map((item, index) => {
                    return <VerticalCard key={item.id} name={item.nama} specialist={item.specialist} shortdesc={item.shortdesc} img={item.img} />
                })
            }

        </div>

So I have a ponent that have a fixed width with an overflow-x-auto. I decided to hide the scroll bar and replace it with 2 arrow/button both left and right. If I click left, it scroll to left and vice versa. How can I achieve this functionality ?

this is my ponent

<div className={` vertical-card w-7/12 flex flex-row overflow-x-scroll no-scrollbar pb-4`}>
            {
                dataConselor.map((item, index) => {
                    return <VerticalCard key={item.id} name={item.nama} specialist={item.specialist} shortdesc={item.shortdesc} img={item.img} />
                })
            }

        </div>
Share Improve this question asked Apr 17, 2022 at 8:24 BlueBeretBlueBeret 6021 gold badge9 silver badges30 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 4
  1. Target the scrollable element (you can use useRef, since it's react):
const scrollable = useRef(null);

<div id="myElement" ref={scrollable}>
...
</div>
  1. Create the scroll function:
const scrollIt = (toRight) => {
  const scrollLength = ... //Calculate your scroll length however you want.
  scrollable.current.scrollBy({left: scrollLength * (toRight ? 1 : -1), behavior: "smooth"});
}
  1. Add this function to the left/right buttons:
<div id="toLeft" onClick={()=>scrollIt(false)}>...</div>
<div id="toRight" onClick={()=>scrollIt(true)}>...</div>

As you are using tailwind css, you can use a tailwind carousal ponent to achieve this behaviour.

There are various carousals available over here.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745217365a4617089.html

相关推荐

  • javascript - Scroll an overflow component in reactnext - Stack Overflow

    So I have a ponent that have a fixed width with an overflow-x-auto. I decided to hide the scroll bar an

    3小时前
    10

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信