react 使用react-seamless-scroll实现无缝滚动

文章目录

  • [1. 实现无缝滚动效果](#1. 实现无缝滚动效果)
  • [2. react-seamless-scroll 无缝滚动案例介绍](#2. react-seamless-scroll 无缝滚动案例介绍)
  • [3. react 项目集成](#3. react 项目集成)
    • [3.1 项目引入 `cssSeamlessScroll` 滚动组件](#3.1 项目引入 cssSeamlessScroll 滚动组件)
    • [3.2 完整代码](#3.2 完整代码)
      • [3.2.1 newBet.tsx 代码](#3.2.1 newBet.tsx 代码)
      • [3.2.2 index.module.scss](#3.2.2 index.module.scss)

1. 实现无缝滚动效果

  1. 实现单步向下滚动
  2. 点击更多展开,收起,调整 scroll 高度

2. react-seamless-scroll 无缝滚动案例介绍

  1. 项目地址 react-seamless-scroll
  2. 拉下来跑起来效果(gif录屏有点卡,实际很丝滑)
  3. 可以支持多种无缝滚动方案,如 向下滚动向左滚动滚动速度鼠标悬停单布停顿单行停顿时间数组属性更新数组添加数据图表滚动
  4. react-seamless-scroll 无缝滚动有两种实现方式,一种是 cssSeamlessScroll,另外一种是 jsSeamlessScroll

3. react 项目集成

3.1 项目引入 cssSeamlessScroll 滚动组件

1.这里我使用的 cssSeamlessScroll 的。

  1. cssSeamlessScroll 组件 的代码复制到项目中

3.2 完整代码

3.2.1 newBet.tsx 代码

ts 复制代码
import React, { useState } from 'react';
import style from '../style/index.module.scss';
// 导入滚动的cssSeamlessScroll
import CssSeamlessScroll from './cssSeamlessScroll/cssSeamlessScroll'

const NewBet = () => {
    const [height, setHeight] = useState(320);
    // mock 后端返回数据
    const [arr, setArr] = useState([
        { gameName: '象财神', player: 'Dorothy MurrayDorothy Murray', betAmount: 4020, profit: -476.53 },
        { gameName: '皇上吉祥', player: '隐身', betAmount: 4020, profit: 516.82 },
        { gameName: '水果丛林', player: 'Frederick Long', betAmount: 4020, profit: 809.59 },
        { gameName: '唐伯虎点秋香', player: '隐身', betAmount: 4020, profit: -928.13 },
        { gameName: '鼠鼠福福', player: 'Adele Moody', betAmount: 4020, profit: -901.85 },
        { gameName: '宝石侠-1111', player: 'Maggie Cobb', betAmount: 4020, profit: 135.91 },
        { gameName: '糖果连连爆', player: 'Jeremiah Harran', betAmount: 4020, profit: 960.88 },
        { gameName: '艳后之迷', player: 'Nellie Wong', betAmount: 4020, profit: 227.48 },
        { gameName: '象财神', player: 'Dorothy MurrayDorothy Murray', betAmount: 4020, profit: -476.53 },
        { gameName: '皇上吉祥', player: '隐身', betAmount: 4020, profit: 516.82 },
        { gameName: '水果丛林', player: 'Frederick Long', betAmount: 4020, profit: 809.59 },
        { gameName: '唐伯虎点秋香', player: '隐身', betAmount: 4020, profit: -928.13 },
        { gameName: '鼠鼠福福', player: 'Adele Moody', betAmount: 4020, profit: -901.85 },
        { gameName: '宝石侠-1111', player: 'Maggie Cobb', betAmount: 4020, profit: 135.91 },
        { gameName: '糖果连连爆', player: 'Jeremiah Harran', betAmount: 4020, profit: 960.88 },
        { gameName: '艳后之迷', player: 'Nellie Wong', betAmount: 4020, profit: 227.48 },
    ]);

    return (
        <div className={style.newBetContent}>
            <div className='px-3 text-white'>
                <div className={style.table}>
                    <div className={style.header}>
                        <span>游戏</span>
                        <span>玩家</span>
                        <span>投注</span>
                        <span>利润额</span>
                    </div>
                    <div className={style.scroll} style={{ height: height + 'px' }}>
                        <CssSeamlessScroll datas={arr} direction="down" duration={20} step={16}>
                            {arr.map((_item, _index) => (
                                <div className={style.item}>
                                    <span><img className='h-5 mr-1' src={require('@/branch/assets/images/home/majorVictory/icon-gold.png')} />{_item.gameName}</span>
                                    <span>{_item.player}</span>
                                    <span>{_item.betAmount}</span>
                                    <span className={_item.profit > 0 ? style.green : style.red}>{_item.profit}<img className='h-3 ml-1' src={require('@/branch/assets/images/home/majorVictory/icon-gold.png')} /></span>
                                </div>
                            ))}
                        </CssSeamlessScroll>
                    </div>
                    <div className={style.moreBtn} onClick={() => {
                        setHeight(height == 320 ? 520 : 320)
                    }}>
                        <span>展示更多</span>
                        {
                            height == 320 ?
                                <img className='h-3 ml-2' src={require('@/branch/assets/images/sclog/bh.png')} /> :
                                <img className='h-3 ml-2' src={require('@/branch/assets/images/sclog/zk.png')} />
                        }
                    </div>
                </div>
            </div>
        </div>
    );
};
export default NewBet;

3.2.2 index.module.scss

css 复制代码
.newBetContent {
    width: 100%;
    .table,
    .scroll {
        .header,
        .item {
            height: 40px;
            display: flex;
            align-items: center;
            color: #93acd3;
            font-size: 12px;
            span {
                text-align: left;
                img {
                    vertical-align: middle;
                }
                &:nth-child(1),
                &:nth-child(2) {
                    display: inline-block;
                    width: 103px;
                    white-space: nowrap; /* 强制内容在一行上显示,防止换行 */
                    overflow: hidden; /* 超出容器部分隐藏 */
                    text-overflow: ellipsis; /* 超出容器部分显示省略号 */
                }
                &:nth-child(2) {
                    text-align: center;
                    color: #fff;
                }
                &:nth-child(3) {
                    text-align: center;
                    display: inline-block;
                    width: 80px;
                    text-align: center;
                    color: #fff;
                }
                &:nth-child(4) {
                    display: inline-block;
                    width: 80px;
                    text-align: right;
                }
                &.green {
                    color: #3bc116;
                }
                &.red {
                    color: #f53202;
                }
            }
        }
        .header {
            height: 32px;
            line-height: 32px;
            span {
                &:nth-child(2),
                &:nth-child(3) {
                    color: #93acd3;
                }
            }
        }
    }
    .table {
        background: #161f2c;
        border-radius: 10px;
        padding: 0px 8px;
        text-align: center;
        .moreBtn {
            margin: 16px auto;
            display: inline-block;
            font-family: PingFang SC, PingFang SC-Regular;
            font-weight: Regular;
            text-align: left;
            line-height: 14px;
            background: #1e2837;
            border-radius: 10px;
            padding: 9px 12px;
            span {
                font-size: 14px;
                color: #93acd3;
            }
            img {
                margin-top: 1px;
            }
        }
    }
    .scroll {
        display: inline-block;
        width: 100%;
        overflow: hidden;
        position: relative;
        transition: height 0.5s ease;
    }
}
相关推荐
PleaSure乐事6 小时前
【React.js】AntDesignPro左侧菜单栏栏目名称不显示的解决方案
前端·javascript·react.js·前端框架·webstorm·antdesignpro
getaxiosluo6 小时前
react jsx基本语法,脚手架,父子传参,refs等详解
前端·vue.js·react.js·前端框架·hook·jsx
新星_7 小时前
函数组件 hook--useContext
react.js
阿伟来咯~8 小时前
记录学习react的一些内容
javascript·学习·react.js
吕彬-前端8 小时前
使用vite+react+ts+Ant Design开发后台管理项目(五)
前端·javascript·react.js
学前端的小朱8 小时前
Redux的简介及其在React中的应用
前端·javascript·react.js·redux·store
bysking10 小时前
【前端-组件】定义行分组的表格表单实现-bysking
前端·react.js
September_ning14 小时前
React.lazy() 懒加载
前端·react.js·前端框架
web行路人14 小时前
React中类组件和函数组件的理解和区别
前端·javascript·react.js·前端框架
番茄小酱00114 小时前
Expo|ReactNative 中实现扫描二维码功能
javascript·react native·react.js