当前位置:首页 > React

react页面实现全屏滚动

2026-01-27 15:38:42React

实现全屏滚动的 React 方案

使用 react-full-page 库

安装 react-full-page 库:

npm install react-full-page

示例代码:

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

示例代码:

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 属性:

react页面实现全屏滚动

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 属性可能需要前缀

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

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

相关文章

css制作锁屏页面

css制作锁屏页面

使用CSS制作锁屏页面 锁屏页面通常包含一个背景、时间显示以及可能的解锁按钮或输入框。以下是实现锁屏页面的关键CSS代码和结构。 HTML结构 <!DOCTYPE html> <…

h5实现登录页面跳转页面跳转页面

h5实现登录页面跳转页面跳转页面

实现H5登录页面跳转 在H5中实现登录页面跳转可以通过多种方式完成,以下是几种常见方法: 使用window.location.href window.location.href = '目标页面UR…

vue页面实现日历

vue页面实现日历

实现基础日历布局 使用Vue的模板语法构建日历的基本HTML结构,通常需要包含星期标题和日期格子。月份切换按钮可以通过v-on绑定事件。 <template> <div cla…

vue 实现页面返回

vue 实现页面返回

监听浏览器返回事件 使用 window.addEventListener 监听 popstate 事件,在 Vue 的 mounted 钩子中绑定事件,并在 beforeDestroy 钩子中移除监听…

vue实现页面切换

vue实现页面切换

Vue 实现页面切换的方法 Vue 中实现页面切换通常使用 Vue Router,这是 Vue.js 官方的路由管理器。以下是几种常见的实现方式: 使用 Vue Router 的基本配置 安装 Vu…

实现vue页面回退

实现vue页面回退

使用 Vue Router 的编程式导航 在 Vue 组件中调用 this.$router.go(-1) 可以实现页面回退。该方法通过操作浏览器历史记录栈实现后退功能,类似于点击浏览器的后退按钮。…