SoFunction
Updated on 2025-03-10

Code examples to add ceiling effect in React project

1. Basic knowledge and preparation

First, we need to create a simple React project that demonstrates the ceiling effect. Can be usedcreate-react-appCreate a new project:

npx create-react-app react-sticky-header-demo

Then enter the project directory and start the development server:

cd react-sticky-header-demo
npm run start

Next, we willsrcCreate a directory calledfile and create a simple React component:

import React from 'react';
import './';
const StickyHeader = () => (
  <header className="sticky-header">
    <h1>Sticky Header</h1>
  </header>
);
export default StickyHeader;

To add style to this component, we willsrcCreate a directory calledFiles:

.sticky-header {
  background-color: #f1f1f1;
  padding: 20px;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
}

Finally, insrc/In the file, import and render this component:

import React from 'react';
import './';
import StickyHeader from './StickyHeader';
function App() {
  return (
    &lt;div className="App"&gt;
      &lt;StickyHeader /&gt;
      {/* Other components and content can be added here */}
    &lt;/div&gt;
  );
}
export default App;

Now, we have a basic React project to demonstrate the ceiling effect. Next, we will achieve this effect.

2. Achieve ceiling effect

To achieve ceiling effect, we need to listen to page scrolling events. When scrolling downwards exceeds a certain distance, we give.stickyclass name to fix it on the top of the screen.

Step 1: Create a CSS Style

First, inIn the file, we need to add a name called.stickyThe style of the following:

. {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  z-index: 100;
}

In this way, when adding to the component.stickyWhen the class name is, it will be pinned to the top of the screen.

Step 2: Use JavaScript to achieve ceiling effect in React component

Next, we need toFile modification components to listen for page scrolling events.

First, we need to convert the stateless component to a class component:

import React, { Component } from 'react';
import './';
class StickyHeader extends Component {
  // ...
}
export default StickyHeader;

Next, set the initial state of the component:

state = {
  isSticky: false,
};

existcomponentDidMountAdd a scroll event listener to the lifecycle method:

componentDidMount() {
  ('scroll', );
}

Also make sure to be incomponentWillUnmountRemove the scroll event listener in the lifecycle method to avoid memory leaks:

jsCopy code
componentWillUnmount() {
  ('scroll', );
}

Next we need to implementhandleScrollMethod, when the scrolling distance exceeds the standard value,isStickyStatus is set totrue

handleScroll = () => {
  const scrollHeight = ;
  const standardHeight = 60;
  if (scrollHeight > standardHeight) {
    ({ isSticky: true });
  } else {
    ({ isSticky: false });
  }
};

Finally, according toisStickyStatus Change Component's Class Name:

render() {
  const { isSticky } = ;
  return (
    <header className={`sticky-header${isSticky ? ' sticky' : ''}`}>
      <h1>Sticky Header</h1>
    </header>
  );
}

Now, when the page scrolls down,StickyHeaderThe components will be kept at the top of the screen, achieving a ceiling effect.

To sum up, to achieve ceiling effect in React project, you can monitor page scrolling events and control the position of components in combination with CSS style. Here we demonstrate how to achieve this effect with a simple example. Of course, in actual projects, you may encounter more complex situations, but this basic principle and method still apply. I hope this tutorial will be helpful for you to achieve ceiling effects in actual projects.

The above is the detailed content of the code example for adding ceiling effects to the React project. For more information about React ceiling effects, please pay attention to my other related articles!