Creating a stunning landing page with smooth scrolling effects using Locomotive Scroll and integrating the Spline Viewer for dynamic 3D animations can significantly enhance user experience. Below, I'll guide you through building this landing page in React step-by-step.
Table of Contents
- Prerequisites
- Setup
- Installing Dependencies
- Project Structure
- Implementing Locomotive Scroll
- Integrating Spline Viewer
- Styling the Landing Page
- Final Code Overview
- Running the Project
Prerequisites
- Node.js and npm installed on your machine.
- Basic knowledge of React.
- Familiarity with CSS for styling.
Setup
First, let's set up a new React project. You can use Create React App for this purpose.
npx create-react-app stunning-landing-page
cd stunning-landing-page
Installing Dependencies
We'll need to install Locomotive Scroll for smooth scrolling effects.
npm install locomotive-scroll
Additionally, since we're using Spline Viewer, we'll dynamically load its script within our React component.
Project Structure
Your project structure will look like this:
stunning-landing-page/
├── node_modules/
├── public/
│ └── index.html
├── src/
│ ├── components/
│ │ └── LandingPage.jsx
│ ├── App.js
│ ├── App.css
│ └── index.js
├── package.json
└── README.md
Implementing Locomotive Scroll
We'll create a LandingPage component where we'll implement the scroll effects.
1. Creating the LandingPage Component
Create a new file at src/components/LandingPage.jsx:
// src/components/LandingPage.jsx
import React, { useEffect, useRef } from 'react';
import LocomotiveScroll from 'locomotive-scroll';
import 'locomotive-scroll/src/locomotive-scroll.scss';
import './LandingPage.css'; // We'll create this next
const LandingPage = () => {
const scrollRef = useRef(null);
useEffect(() => {
// Initialize Locomotive Scroll
const scroll = new LocomotiveScroll({
el: scrollRef.current,
smooth: true,
multiplier: 1,
class: 'is-reveal',
});
// Clean up on unmount
return () => {
if (scroll) scroll.destroy();
};
}, []);
// Dynamically load Spline Viewer script
useEffect(() => {
const script = document.createElement('script');
script.type = 'module';
script.src = 'https://unpkg.com/@splinetool/viewer@1.9.28/build/spline-viewer.js';
script.async = true;
document.body.appendChild(script);
return () => {
document.body.removeChild(script);
};
}, []);
return (
<div data-scroll-container ref={scrollRef}>
{/* Hero Section */}
<section className="section hero" data-scroll-section>
<div className="content">
<h1 data-scroll data-scroll-speed="1">Welcome to Our Amazing Landing Page</h1>
<p data-scroll data-scroll-speed="2">
Experience smooth scrolling and stunning 3D animations.
</p>
</div>
</section>
{/* Spline Viewer Section */}
<section className="section spline-section" data-scroll-section>
<spline-viewer
url="https://prod.spline.design/pSfIVnqtcseH07-o/scene.splinecode"
camera-controls
className="spline-viewer"
data-scroll
data-scroll-speed="1"
></spline-viewer>
</section>
{/* Additional Content Sections */}
<section className="section" data-scroll-section>
<div className="content">
<h2 data-scroll data-scroll-speed="1">About Us</h2>
<p data-scroll data-scroll-speed="2">
We create beautiful and interactive web experiences.
</p>
</div>
</section>
<section className="section" data-scroll-section>
<div className="content">
<h2 data-scroll data-scroll-speed="1">Our Services</h2>
<p data-scroll data-scroll-speed="2">
From web development to 3D animations, we cover it all.
</p>
</div>
</section>
{/* Footer Section */}
<section className="section footer" data-scroll-section>
<div className="content">
<h2 data-scroll data-scroll-speed="1">Get in Touch</h2>
<p data-scroll data-scroll-speed="2">
Contact us to start your project today.
</p>
</div>
</section>
</div>
);
};
export default LandingPage;
Explanation:
-
Locomotive Scroll Initialization: We initialize Locomotive Scroll within a useEffect hook to ensure it's set up after the component mounts. The scrollRef references the main container that Locomotive Scroll will control.
-
Spline Viewer Script: Since Spline Viewer is a web component, we dynamically load its script to ensure it's correctly registered.
-
Sections: We have multiple sections with the data-scroll-section attribute. These sections can have content that gets animated on scroll.
2. Adding Styles for LandingPage
Create a CSS file for styling at src/components/LandingPage.css:
/* src/components/LandingPage.css */
.section {
min-height: 100vh;
display: flex;
align-items: center;
justify-content: center;
padding: 100px 20px;
box-sizing: border-box;
}
.hero {
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
color: white;
text-align: center;
}
.spline-section {
background: #f5f5f5;
display: flex;
align-items: center;
justify-content: center;
}
.spline-viewer {
width: 80%;
height: 500px;
}
.content {
max-width: 800px;
text-align: center;
}
h1, h2 {
font-size: 3rem;
margin-bottom: 20px;
}
p {
font-size: 1.5rem;
}
/* Footer Styling */
.footer {
background: #333;
color: white;
}
/* Responsive Design */
@media (max-width: 768px) {
h1, h2 {
font-size: 2.5rem;
}
p {
font-size: 1.2rem;
}
.spline-viewer {
width: 100%;
height: 300px;
}
}
Explanation:
-
Section Styling: Each section occupies the full viewport height with padding and centered content.
-
Hero Section: A vibrant gradient background with white text.
-
Spline Section: A light background to make the 3D model stand out.
-
Responsive Design: Adjustments for smaller screens to ensure readability and proper layout.
3. Integrating the LandingPage Component
Update src/App.js to include the LandingPage component.
// src/App.js
import React from 'react';
import LandingPage from './components/LandingPage';
import './App.css'; // Optional global styles
function App() {
return (
<div className="App">
<LandingPage />
</div>
);
}
export default App;
4. Optional Global Styles
You can add some global styles in src/App.css or use it to reset default browser styles.
/* src/App.css */
body {
margin: 0;
padding: 0;
font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
overflow: hidden; /* Locomotive Scroll handles scrolling */
}
* {
box-sizing: border-box;
}
Integrating Spline Viewer
Since the Spline Viewer is a web component, we've already included it in the LandingPage.jsx. However, to ensure proper functionality, make sure to:
-
Camera Controls: The camera-controls attribute allows users to interact with the 3D model.
-
Responsive Spline Viewer: The CSS ensures that the viewer adjusts based on screen size.
Note: The Spline Viewer will automatically animate based on user interactions and may also respond to scroll events if the scene is designed to do so.
Styling the Landing Page
The provided CSS ensures a clean and modern look with responsive design considerations. You can further enhance the styles by:
-
Adding Animations: Use CSS animations or libraries like Framer Motion for more dynamic effects.
-
Custom Fonts: Integrate Google Fonts or other font libraries for typography enhancement.
-
Enhanced Color Schemes: Adjust color palettes to match your brand identity.
Final Code Overview
public/index.html
Ensure your public/index.html has the following structure. Since we are dynamically loading the Spline Viewer script in LandingPage.jsx, no changes are needed here.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<link rel="icon" href="%PUBLIC_URL%/favicon.ico" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Stunning Landing Page</title>
</head>
<body>
<div id="root"></div>
</body>
</html>
src/index.js
Standard React entry point.
// src/index.js
import React from 'react';
import ReactDOM from 'react-dom/client';
import App from './App';
import './index.css'; // Optional global styles
const root = ReactDOM.createRoot(document.getElementById('root'));
root.render(
<React.StrictMode>
<App />
</React.StrictMode>
);
src/components/LandingPage.jsx
As provided above.
src/components/LandingPage.css
As provided above.
src/App.js
As provided above.
src/App.css
As provided above.
Running the Project
After setting up all the files, you can start your project with:
npm start
This will run the app in development mode. Open http://localhost:3000 to view it in your browser.
Additional Enhancements
To make the landing page even more stunning, consider the following enhancements:
-
Adding Parallax Effects:
Utilize Locomotive Scroll's data attributes to create parallax effects on different elements.
<h1 data-scroll data-scroll-speed="2" data-scroll-direction="horizontal">Welcome</h1>
-
Loading Indicators:
Show a loading spinner while the Spline Viewer or other assets are loading to improve user experience.
-
Interactive Elements:
Add buttons or links that smoothly scroll to different sections using Locomotive Scroll's API.
-
Accessibility:
Ensure that your landing page is accessible by adding appropriate aria labels and ensuring keyboard navigability.
-
SEO Optimization:
Use meta tags and semantic HTML to improve the page's SEO.
-
Dark Mode Toggle:
Implement a dark mode switch for better user personalization.
By following the steps above, you'll have a visually appealing and smoothly scrolling landing page that leverages the power of Locomotive Scroll and Spline Viewer. Feel free to customize and expand upon this foundation to build a unique and engaging web presence!