当前位置:首页 > React

react页面实现全屏滚动

2026-01-27 15:38:42React

实现全屏滚动的 React 方案

使用 react-full-page 库

安装 react-full-page 库:

npm install react-full-page

示例代码:

react页面实现全屏滚动

import React from 'react';
import { FullPage, Slide } from 'react-full-page';

const App = () => {
  return (
    <FullPage>
      <Slide>
        <div style={{ backgroundColor: 'red', height: '100vh' }}>Section 1</div>
      </Slide>
      <Slide>
        <div style={{ backgroundColor: 'blue', height: '100vh' }}>Section 2</div>
      </Slide>
      <Slide>
        <div style={{ backgroundColor: 'green', height: '100vh' }}>Section 3</div>
      </Slide>
    </FullPage>
  );
};

使用 react-scroll 库

安装 react-scroll 库:

npm install react-scroll

示例代码:

react页面实现全屏滚动

import React from 'react';
import { Link, animateScroll as scroll } from 'react-scroll';

const App = () => {
  return (
    <div>
      <nav>
        <Link to="section1" smooth={true} duration={500}>Section 1</Link>
        <Link to="section2" smooth={true} duration={500}>Section 2</Link>
        <Link to="section3" smooth={true} duration={500}>Section 3</Link>
      </nav>

      <div id="section1" style={{ height: '100vh', backgroundColor: 'red' }}>Section 1</div>
      <div id="section2" style={{ height: '100vh', backgroundColor: 'blue' }}>Section 2</div>
      <div id="section3" style={{ height: '100vh', backgroundColor: 'green' }}>Section 3</div>
    </div>
  );
};

自定义实现方案

通过监听滚动事件和 CSS 实现:

import React, { useState, useEffect } from 'react';

const App = () => {
  const [currentSection, setCurrentSection] = useState(0);
  const sections = ['red', 'blue', 'green'];

  useEffect(() => {
    const handleWheel = (e) => {
      if (e.deltaY > 0) {
        setCurrentSection(prev => Math.min(prev + 1, sections.length - 1));
      } else {
        setCurrentSection(prev => Math.max(prev - 1, 0));
      }
    };

    window.addEventListener('wheel', handleWheel);
    return () => window.removeEventListener('wheel', handleWheel);
  }, []);

  return (
    <div style={{ overflow: 'hidden', height: '100vh' }}>
      {sections.map((color, index) => (
        <div 
          key={index}
          style={{
            transform: `translateY(-${currentSection * 100}vh)`,
            transition: 'transform 0.5s ease',
            height: '100vh',
            backgroundColor: color
          }}
        >
          Section {index + 1}
        </div>
      ))}
    </div>
  );
};

CSS-only 解决方案

使用纯 CSS 的 scroll-snap 属性:

import React from 'react';

const App = () => {
  return (
    <div style={{
      height: '100vh',
      overflowY: 'scroll',
      scrollSnapType: 'y mandatory'
    }}>
      <div style={{
        height: '100vh',
        scrollSnapAlign: 'start',
        backgroundColor: 'red'
      }}>Section 1</div>
      <div style={{
        height: '100vh',
        scrollSnapAlign: 'start',
        backgroundColor: 'blue'
      }}>Section 2</div>
      <div style={{
        height: '100vh',
        scrollSnapAlign: 'start',
        backgroundColor: 'green'
      }}>Section 3</div>
    </div>
  );
};

注意事项

  • 移动端兼容性需要考虑触摸事件处理
  • 性能优化:避免频繁的 DOM 操作和重绘
  • 可访问性:确保键盘导航和屏幕阅读器支持
  • 浏览器兼容性:某些 CSS 属性可能需要前缀

以上方案可根据项目需求选择,库方案更完整但体积较大,自定义方案更灵活但需要处理更多细节。

标签: 全屏页面
分享给朋友:

相关文章

vue 实现页面注册

vue 实现页面注册

Vue 实现页面注册功能 创建注册表单组件 在 Vue 项目中创建一个注册表单组件,通常命名为 Register.vue。该组件包含用户名、邮箱、密码等输入字段,以及提交按钮。 <templa…

php实现页面跳转

php实现页面跳转

PHP 页面跳转方法 在 PHP 中,可以通过多种方式实现页面跳转,以下是几种常见的方法: 使用 header() 函数 header() 函数是 PHP 中最常用的跳转方法,通过发送 HTTP 头…

jquery页面跳转

jquery页面跳转

jQuery 页面跳转方法 使用 jQuery 实现页面跳转可以通过多种方式完成,以下是几种常见的方法: 使用 window.location.href 通过修改 window.location.h…

vue页面实现流程

vue页面实现流程

Vue 页面实现流程 创建 Vue 项目 使用 Vue CLI 或 Vite 初始化项目。Vue CLI 适用于传统项目,Vite 适合现代轻量级应用。 安装依赖后,通过命令行工具运行项目初始化命令,…

vue实现页面签字

vue实现页面签字

实现页面签名的Vue方案 在Vue中实现页面签名功能,可以通过HTML5的Canvas元素结合手势或鼠标事件来捕获用户绘制轨迹。以下是具体实现方法: 安装签名库(可选) 使用现成的Vue签名库如vu…

弹窗能用h5页面实现吗

弹窗能用h5页面实现吗

H5实现弹窗的方法 H5页面可以通过HTML、CSS和JavaScript实现多种弹窗效果,包括模态框、提示框、悬浮通知等。以下是常见的实现方式: 使用HTML和CSS创建基础弹窗 通过<…