• react实现列表滚动组件


    1.需求

    在开发项目的时候,从服务端获取到数据列表后,展示给用户看:需要实现数据自动滚动效果,怎么实现呢?如下图所示:

    2.实现

    把上面需要实现的功能写成一个组件,页面直接调用该组件即可,代码如下:

    要引入组件页面的代码:   

    1. import Scroll from "../../components/Scroll";
    2. const Index = () => {
    3. return (
    4. <div class={style.main}>
    5. <Scroll />
    6. div>
    7. }
    8. export default Index;

    组件:

    1. import style from "./style.less";
    2. import React, { useEffect, useRef, useState } from "react";
    3. const Scroll = () => {
    4. const timer = useRef(null);
    5. //这里的数据是通过服务端api接口获取的
    6. const marqueeList = [
    7. {
    8. phone: "155****1111",
    9. content: "签到获取",
    10. money: 12.22,
    11. },
    12. {
    13. phone: "151****1111",
    14. content: "签到获取",
    15. money: 2,
    16. },
    17. {
    18. phone: "152****1111",
    19. content: "签到获取",
    20. money: 2.22,
    21. },
    22. ...
    23. ];
    24. //是否滚动
    25. const [isScrolle, setIsScrolle] = useState(true);
    26. //滚动速度,值越小,滚动越快
    27. const scrollSpeed = 100;
    28. const warper = useRef();
    29. //原数据
    30. const childDom = useRef();
    31. //拷贝数据
    32. const childDomCopy = useRef();
    33. //鼠标移动,移除方法
    34. const hoverHandler = (flag) => setIsScrolle(flag);
    35. useEffect(() => {
    36. // 设置轮播滚动多拷贝一层,让它无缝滚动
    37. childDom.current.innerHTML = childDomCopy.current.innerHTML;
    38. if (isScrolle) {
    39. if (timer.current) {
    40. clearInterval(timer.current);
    41. }
    42. timer.current = setInterval(() => {
    43. warper.current.scrollTop >= childDom.current.scrollHeight
    44. ? (warper.current.scrollTop = 0)
    45. : warper.current.scrollTop++;
    46. }, scrollSpeed);
    47. }
    48. return () => {
    49. clearInterval(timer.current);
    50. };
    51. }, [isScrolle]);
    52. return (
    53. <div class={style.content}>
    54. <div class={style.parent} ref={warper}>
    55. <div class={`${style.ul_scoll}`} ref={childDomCopy}>
    56. {marqueeList.map((value, index) => (
    57. <li
    58. style={{backgroundColor: index % 2 == 0 ? "#S1AAAA" : "#ABCDEF"}}
    59. key={value}
    60. onMouseOver={() => hoverHandler(false)}
    61. onMouseLeave={() => hoverHandler(true)}
    62. >
    63. <div class={style.li_intro}>
    64. <div>
    65. {value.phone}
    66. div>
    67. <div>
    68. {value.content}
    69. div>
    70. <div class={style.li_money_intro}>
    71. <div class={style.li_money}>
    72. +{value.money}
    73. div>
    74. <div class={style.li_rmb}>
    75. RMB
    76. div>
    77. div>
    78. div>
    79. li>
    80. ))}
    81. div>
    82. <div class={`${style.ul_scoll}`} ref={childDomCopy}>div>
    83. div>
    84. div>
    85. );
    86. };
    87. export default Scroll;

    css:

    1. .content {
    2. width: 100%;
    3. height: 14.16rem;
    4. overflow: hidden;
    5. position: relative;
    6. margin: auto;
    7. }
    8. .parent {
    9. top: 1rem;
    10. position: relative;
    11. height: 11.16rem;
    12. overflow: hidden;
    13. line-height: 2.5rem;
    14. overflow-y: scroll;
    15. scrollbar-width: none;
    16. -ms-overflow-style: none;
    17. }
    18. .parent::-webkit-scrollbar {
    19. display: none;
    20. }
    21. .ul_scoll li {
    22. width: 100%;
    23. height: 2.5rem;
    24. font-size: 0.9rem;
    25. font-weight: bold;
    26. text-align: center;
    27. letter-spacing: 0.025rem;
    28. line-height: 2.5rem;
    29. color: red;
    30. }
    31. .li_intro {
    32. color: #205F59;
    33. font-weight: 930;
    34. flex-direction: row;
    35. align-items: center;
    36. justify-content: space-between;
    37. display: flex;
    38. padding-left: 0.25rem;
    39. padding-right: 0.25rem;
    40. }
    41. .li_money_intro {
    42. display:flex;
    43. color: #39B54A;
    44. }
    45. .li_oney {
    46. font-size: 1rem;
    47. }
    48. .li_rmb {
    49. color:#FF6000;
    50. margin-left:0.8rem;
    51. font-size: 0.8rem;
    52. }

    这样就能够实现数据不停滚动了

  • 相关阅读:
    sql2o 将 sql 语句的结果转换为对象支持命名参数
    实践历练的力量
    mysql之update语句锁分析
    RT-Thread STM32F407 定时器
    ASP.NET Core 6框架揭秘实例演示[07]:文件系统
    液压切管机配套用液压泵站比例阀放大器
    操作系统(六)| 文件系统下 文件使用 共享 保护
    Java接口(Interface)
    Java网络通信:IP、端口、协议、IP地址操作类-InetAddress
    # 从浅入深 学习 SpringCloud 微服务架构(三)注册中心 Eureka(3)
  • 原文地址:https://blog.csdn.net/zhoupenghui168/article/details/133169327