当前位置:   article > 正文

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

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

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 的。
2. 将 cssSeamlessScroll 组件 的代码复制到项目中

在这里插入图片描述

3.2 完整代码

3.2.1 newBet.tsx 代码

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;
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65

3.2.2 index.module.scss

.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;
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/盐析白兔/article/detail/65812
推荐阅读
相关标签
  

闽ICP备14008679号