在react内,如何实现点击某按钮后div盒子内的横向滚动条向左或向右滑动

// 方案一

import React, { useRef, useState } from 'react';
import './App.css';
 
function App() {
  const scrollContainerRef = useRef(null);
  const [scrollPosition, setScrollPosition] = useState(0);
 
  const scrollLeft = () => {
    const container = scrollContainerRef.current;
    if (container) {
      const maxScroll = container.scrollWidth - container.clientWidth;
      const newPosition = Math.max(0, scrollPosition - 100); // 滑动量,可以根据需要调整
      setScrollPosition(newPosition);
      container.scrollLeft = newPosition;
    }
  };
 
  const scrollRight = () => {
    const container = scrollContainerRef.current;
    if (container) {
      const maxScroll = container.scrollWidth - container.clientWidth;
      const newPosition = Math.min(maxScroll, scrollPosition + 100); // 滑动量,可以根据需要调整
      setScrollPosition(newPosition);
      container.scrollLeft = newPosition;
    }
  };
 
  return (
    <div className="App">
      <button onClick={scrollLeft}>Scroll Left</button>
      <button onClick={scrollRight}>Scroll Right</button>
      <div
        ref={scrollContainerRef}
        className="scroll-container"
        onScroll={(e) => setScrollPosition(e.target.scrollLeft)}
      >
        {/* Your content here */}
        <div style={{ width: '3000px', height: '100px' }}>
          Long content to enable scrolling
        </div>
      </div>
    </div>
  );
}
 
export default App;


// 方案二

import React, { useRef, useState } from 'react';
import './App.css';
 
function App() {
  const scrollContainerRef = useRef(null);
  const [scrollLeft, setScrollLeft] = useState(0);
 
  const handleScrollLeft = (direction) => {
    const container = scrollContainerRef.current;
    if (container) {
      const newScrollLeft = container.scrollLeft + (direction === 'left' ? -100 : 100);
      setScrollLeft(newScrollLeft);
      container.scrollTo({
        left: newScrollLeft,
        behavior: 'smooth'
      });
    }
  };
 
  return (
    <div className="App">
      <div
        ref={scrollContainerRef}
        style={{ overflowX: 'auto', whiteSpace: 'nowrap', width: '300px' }}
      >
        <div style={{ width: '1000px', height: '50px' }}>
          这里是可以滚动的内容...
        </div>
      </div>
      <button onClick={() => handleScrollLeft('left')}>向左滑动</button>
      <button onClick={() => handleScrollLeft('right')}>向右滑动</button>
    </div>
  );
}
 
export default App;

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-07-11 16:56:03       67 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-07-11 16:56:03       72 阅读
  3. 在Django里面运行非项目文件

    2024-07-11 16:56:03       58 阅读
  4. Python语言-面向对象

    2024-07-11 16:56:03       69 阅读

热门阅读

  1. 差分进化算法

    2024-07-11 16:56:03       16 阅读
  2. 【Docker 入门】

    2024-07-11 16:56:03       26 阅读
  3. 从零手写实现 nginx-26-rewrite url 重写

    2024-07-11 16:56:03       22 阅读
  4. 虚拟化平台主流hypervisor工作原理分析

    2024-07-11 16:56:03       19 阅读
  5. 常用知识点问答

    2024-07-11 16:56:03       21 阅读
  6. SQL MySQL定时器/事件调度器(Event Scheduler)

    2024-07-11 16:56:03       24 阅读
  7. Unity 改造编辑器组件字段显示

    2024-07-11 16:56:03       24 阅读
  8. 解决selenium手动下载驱动问题

    2024-07-11 16:56:03       20 阅读
  9. 文本大模型下游任务与peft微调实战

    2024-07-11 16:56:03       20 阅读