react+swiper实现pc端无缝滚动

本文介绍如何使用Swiper实现列表的无缝滚动效果,并提供了一个基于React的实例代码。该示例通过Swiper组件展示了带有自动播放功能的垂直滚动表格。

使用swiper实现无缝滚动,代码没有做封装。可以根据自己的需求进行改动.

css样式


 .container{
  background: rgba(22, 63, 124);
  backdrop-filter: blur(5px);
  .swiper{
    .swiper-wrapper {
      -webkit-transition-timing-function: linear !important;
      /*之前是ease-out*/
      -moz-transition-timing-function: linear !important;
      -ms-transition-timing-function: linear !important;
      -o-transition-timing-function: linear !important;
      transition-timing-function: linear !important;
      margin: 0 auto !important;
    }
    .swiper-slide :hover{
      background-color: #f00;
    }
  }
  .table{
    display: flex;
    flex-direction: row;
    height: 100%;
    align-items: center;
  }
}

index文件

import React from "react";
// Import Swiper React components
import { Swiper, SwiperSlide } from 'swiper/react';
import { EffectFade, Autoplay } from "swiper";
// Import Swiper styles
import 'swiper/css';
import 'swiper/css/bundle';
import 'swiper/css/effect-fade';
import './style.less'

const ScrollTable = (props) => {
  // const { columns=[], data=[] } = props
  const columns = [
    {
      text: '名称',
      dataIndex: 'name',
      key: 'name',
      width: '50px'
    },
    {
      text: '年龄',
      dataIndex: 'age',
      key: 'age',
      // width: '100px'
    },
    {
      text: '性别',
      dataIndex: 'sex',
      key: 'sex',
      // width: '100px'
    },
  ]
  const data = [
    {name: '1', age: '1', sex: '男'},{name: '2', age: '2', sex: '女'},{name: '3', age: '3', sex: '男'},
    {name: '4', age: '4', sex: '女'},{name: '5', age: '5', sex: '女'},{name: '6', age: '6', sex: '男'}
  ]
  return (
    <div className="container" style={{ width: '300px' }}>
      {/* 生成表头 */}
      <div className="table">
        {
          columns.map(column => {
            return (
              <div key={column.key} style={column.width ? { width: column.width, flex: ''} : {flex: 1}}>{column.text}</div>
            )
          })
        }
      </div>
      {/* 滚动列表 */}
      <Swiper
        // 每项间隔
        spaceBetween={0}
        // 一页显示多少
        slidesPerView={5}
        onSlideChange={() => console.log('slide change')}
        onSwiper={(swiper) => console.log(swiper)}
        // 方向
        direction='vertical'
        // 引入模块
        modules={[EffectFade, Autoplay]}
        // 自动播放间隔
        autoplay={{
          delay: 0,
          disableOnInteraction: false,
          pauseOnMouseEnter: true
        }}
        // 播放速度
        speed={2000}
        // 是否循环
        loop
        observer
        observeParents
        autoHeight
        style={{ height: '200px' }}
      >
       { 
          data.map(item => {
              return (
                <SwiperSlide key={item.name}>
                  <div className="table">
                    {
                      columns.map(column => {
                        return (
                          <div key={item.name} style={column.width ? { width: column.width} : {flex: 1}}>
                            {item[column.dataIndex]}
                          </div>
                        )
                      })
                    }
                  </div>
                </SwiperSlide>
              )
            })
        }
      </Swiper>
    </div>
  )
}
export default ScrollTable

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值