0.1.1 • Published 2 years ago

@epeejs/react-scroll-snap v0.1.1

Weekly downloads
-
License
MIT
Repository
github
Last release
2 years ago

react-scroll-snap

一个基于 React 的滚动快照组件

DEMO

Edit react-scroll-snap

安装

yarn add @epeejs/react-scroll-snap

用法

import ScrollSnap, { ScrollSnapAction } from '@epeejs/react-scroll-snap';
import React, { useEffect, useRef } from 'react';

const App = () => {
  const actionRef = useRef<ScrollSnapAction>();
  const colors = ['#E6F7FF', '#BAE7FF', '#91D5FF', '#69C0FF'];

  useEffect(() => {
    if (actionRef.current) {
      // 初始化时滑动到第三个元素
      actionRef.current.goTo(2);
    }
  }, []);

  return (
    <ScrollSnap
      actionRef={actionRef}
      onChange={(current) => {
        console.log(current);
      }}
    >
      {colors.map((m) => (
        <div
          style={{
            background: m,
            width: '80%',
            borderRadius: 16,
            height: 300,
            flexShrink: 0,
            // 设置停留时元素居中
            scrollSnapAlign: 'center',
          }}
        >
          {m}
        </div>
      ))}
    </ScrollSnap>
  );
};

API

属性说明类型默认值版本
onChange切换面板后的回调function(current:number)
actionRef操作函数ScrollSnapAction

方法

ScrollSnapAction

名称描述
goTo(index)切换到指定面板
0.1.1

2 years ago

0.1.0

3 years ago

0.0.2

3 years ago

0.0.1

3 years ago