Scroll上下滚动轮轮播实现

在这里插入图片描述
如上图,实现功能是,列表上下滚动轮播,用户鼠标移动到列表区域,滚动动画停止,鼠标移开滚动继续。
上述列子用REACT+CSS技术框架实现。
React:

import React, { useState, useRef, useEffect } from 'react';
import styles from './index.module.css';

export default function Scroll() {
  const [isScroll, setIsScroll] = useState(true); // 用于当鼠标移入时,滚动停止
  const scrollerRef = useRef(null);
  const timer = useRef(null);

  useEffect(() => {
    timer.current = setInterval(() => {
      if (isScroll && scrollerRef.current) {
        if (
          scrollerRef.current.scrollHeight - 1 <=
          scrollerRef.current.scrollTop + scrollerRef.current.clientHeight
        ) {
          setTimeout(() => {
            scrollerRef.current.scrollTop = 0;
          }, 500);
        }
        let rollSpeed =
          parseFloat((1 / window.devicePixelRatio).toFixed(2)) + 0.01; // + 0.01 为了保证 ratio >= 1
        if (window.navigator.userAgent.indexOf('Firefox') >= 0) {
          rollSpeed = 1.1;
        }
        scrollerRef.current.scrollTop += rollSpeed;
      }
    }, 50);
    return () => {
      timer.current && window.clearInterval(timer.current);
    };
  }, [isScroll]);

  return (
    <div className={styles.container}>
      <div
        className={styles.wrapper}
        ref={scrollerRef}
        onMouseEnter={() => setIsScroll(false)}
        onMouseLeave={() => setIsScroll(true)}
      >
        {[1, 2, 3, 4, 5, 6, 7, 8, 9, 10].map((item, index) => (
          <div className={styles.item} key={index}>
            List-{item}
          </div>
        ))}
      </div>
    </div>
  );
}


css

.container {
  height: 300px;
  width: 300px;
  display: flex;
  .wrapper {
    width: 100%;
    overflow: auto;
    flex: 1;
    .item {
      width: 100%;
      height: 40px;
      box-sizing: border-box;
      padding: 0 10px;
      font-size: 18px;
      line-height: 38px;
      background-color: bisque;
      border-bottom: 1px solid #eee;
    }
    /* 滚动条相关属性配置 */
    &::-webkit-scrollbar {
      width: 3px;
      height: 3px;
    }
    &::-webkit-scrollbar-thumb {
      border-radius: 3px;
      background: #e3e3e3;
      -webkit-box-shadow: inset 0 0 5px #e3e3e3;
    }
    &::-webkit-scrollbar-track {
      background: #071d45;
      opacity: 0.8;
      border-radius: 3px;
    }
  }
}

相关推荐

  1. android ,广播 BulletinView

    2024-07-14 08:52:03       45 阅读
  2. 微信小程序文字无缝滚动效果)

    2024-07-14 08:52:03       38 阅读
  3. react 字滚动

    2024-07-14 08:52:03       54 阅读
  4. VUE +element ui 表格实现数据滚动效果

    2024-07-14 08:52:03       44 阅读

最近更新

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

    2024-07-14 08:52:03       66 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-07-14 08:52:03       70 阅读
  3. 在Django里面运行非项目文件

    2024-07-14 08:52:03       57 阅读
  4. Python语言-面向对象

    2024-07-14 08:52:03       68 阅读

热门阅读

  1. RabbitMQ的工作模式

    2024-07-14 08:52:03       17 阅读
  2. 跨域问题出现的原因,怎么解决?

    2024-07-14 08:52:03       21 阅读
  3. Isaac sim中使用不同的backone

    2024-07-14 08:52:03       17 阅读
  4. Python中的pytest的使用

    2024-07-14 08:52:03       26 阅读
  5. Power BI 工具介绍

    2024-07-14 08:52:03       24 阅读
  6. 【C语言】多线程服务器

    2024-07-14 08:52:03       22 阅读
  7. 数学建模如何创新

    2024-07-14 08:52:03       27 阅读
  8. 【Qt】使用临时对象的坑

    2024-07-14 08:52:03       21 阅读
  9. C++智能指针的用法

    2024-07-14 08:52:03       23 阅读
  10. vue怎样自定义指令?

    2024-07-14 08:52:03       21 阅读