Creating a stunning, animated landing page in React using Locomotive Scroll and Spline Viewer involves integrating smooth scrolling effects with interactive 3D content. Below is a step-by-step guide to building such a landing page. We'll cover setting up the project, installing necessary dependencies, integrating Locomotive Scroll, embedding the Spline Viewer, and adding beautiful animations.
Table of Contents
- Project Setup
- Installing Dependencies
- Project Structure
- Integrating Locomotive Scroll
- Embedding Spline Viewer
- Styling the Landing Page
- Final Code
- Additional Enhancements
Project Setup
First, we'll set up a React project using Vite for its speed and efficiency. Alternatively, you can use create-react-app if you prefer.
Using Vite
# Install Vite globally if you haven't already
npm install -g create-vite
# Create a new React project
npm create vite@latest stunning-landing --template react
# Navigate into the project directory
cd stunning-landing
# Install dependencies
npm install
# Start the development server
npm run dev
Using Create React App
If you prefer using create-react-app:
npx create-react-app stunning-landing
cd stunning-landing
npm start
For this guide, we'll proceed with the assumption that you're using Vite, but the steps are similar for create-react-app.
Installing Dependencies
We'll need to install the following dependencies:
- Locomotive Scroll: For smooth scrolling and scroll-triggered animations.
- React Router DOM (optional): If you plan to have multiple pages or sections.
- SPLINE Viewer: We'll embed this via a script tag.
Install Locomotive Scroll:
npm install locomotive-scroll
Install additional dependencies if needed:
npm install gsap
Note: While Locomotive Scroll provides smooth scrolling, integrating it with GSAP can enhance animation capabilities.
Project Structure
Here's an overview of the project structure we'll use:
stunning-landing/
├── public/
│ └── index.html
├── src/
│ ├── components/
│ │ └── SplineViewer.js
│ ├── App.jsx
│ ├── main.jsx
│ └── styles/
│ ├── App.css
│ └── locomotive-scroll.css
├── package.json
└── vite.config.js
Integrating Locomotive Scroll
We'll create a custom hook to initialize Locomotive Scroll and ensure it works seamlessly with React's lifecycle.
1. Create a Custom Hook: useLocomotiveScroll.js
Create a new file in src/hooks/useLocomotiveScroll.js:
// src/hooks/useLocomotiveScroll.js
import { useEffect } from 'react';
import LocomotiveScroll from 'locomotive-scroll';
import 'locomotive-scroll/src/locomotive-scroll.scss';
const useLocomotiveScroll = () => {
useEffect(() => {
const scrollContainer = document.querySelector('[data-scroll-container]');
const locomotiveScroll = new LocomotiveScroll({
el: scrollContainer,
smooth: true,
// Optional: Customize the smoothness and other parameters
smoothMobile: true,
multiplier: 1,
});
return () => {
if (locomotiveScroll) locomotiveScroll.destroy();
};
}, []);
};
export default useLocomotiveScroll;
2. Import the Hook in App.jsx
// src/App.jsx
import React from 'react';
import './styles/App.css';
import useLocomotiveScroll from './hooks/useLocomotiveScroll';
import SplineViewer from './components/SplineViewer';
function App() {
useLocomotiveScroll();
return (
<div data-scroll-container>
{/* Navigation Section */}
<section className="section home-section" data-scroll-section>
<nav className="navbar">
<h1 className="logo">Stunning Landing</h1>
{/* Add navigation links if needed */}
</nav>
<div className="home-content">
<h2>Welcome to Our Website</h2>
<p>Experience the smoothest scrolling and interactive 3D animations.</p>
</div>
</section>
{/* Spline Viewer Section */}
<section className="section spline-section" data-scroll-section>
<SplineViewer />
</section>
{/* Additional Sections */}
<section className="section about-section" data-scroll-section>
<h2>About Us</h2>
<p>We create stunning websites with interactive designs.</p>
</section>
<section className="section contact-section" data-scroll-section>
<h2>Contact Us</h2>
<p>Get in touch for amazing web experiences.</p>
</section>
</div>
);
}
export default App;
Embedding Spline Viewer
Since spline-viewer is a Web Component and requires a script tag, we'll create a React component that handles embedding it properly.
1. Create SplineViewer.jsx
// src/components/SplineViewer.jsx
import React, { useEffect, useRef } from 'react';
const SplineViewer = () => {
const splineRef = useRef(null);
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;
splineRef.current.appendChild(script);
// Cleanup
return () => {
if (splineRef.current) {
splineRef.current.removeChild(script);
}
};
}, []);
return (
<div className="spline-container" data-scroll data-scroll-speed="1">
<spline-viewer
url="https://prod.spline.design/pSfIVnqtcseH07-o/scene.splinecode"
style={{ width: '100%', height: '100vh' }}
></spline-viewer>
</div>
);
};
export default SplineViewer;
Explanation:
- useRef: A reference to the container div to append the script.
- useEffect: Loads the
spline-viewer script when the component mounts and cleans it up on unmount.
- data-scroll & data-scroll-speed: Attributes used by Locomotive Scroll to control the scrolling behavior of the spline viewer. Adjust
data-scroll-speed as needed for desired parallax effects.
Styling the Landing Page
We'll add styles to make the landing page visually appealing. Create and modify CSS files accordingly.
1. Import Locomotive Scroll Styles
Ensure you have imported Locomotive Scroll's CSS in your main CSS file or within the component where it's used.
/* src/styles/App.css */
/* Import Locomotive Scroll styles */
@import 'locomotive-scroll/src/locomotive-scroll.scss';
/* Reset and base styles */
body, html, #root {
margin: 0;
padding: 0;
height: 100%;
overflow: hidden;
font-family: 'Arial', sans-serif;
}
[data-scroll-container] {
height: 100%;
overflow: hidden;
}
/* Section styles */
.section {
position: relative;
min-height: 100vh;
padding: 100px 50px;
box-sizing: border-box;
}
.home-section {
background: linear-gradient(135deg, #667eea, #764ba2);
color: #fff;
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
text-align: center;
}
.spline-section {
background: #f5f5f5;
display: flex;
justify-content: center;
align-items: center;
}
.about-section, .contact-section {
background: #ffffff;
color: #333;
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
text-align: center;
}
/* Navbar styles */
.navbar {
position: fixed;
top: 0;
width: 100%;
padding: 20px 50px;
display: flex;
justify-content: space-between;
align-items: center;
z-index: 100;
}
.logo {
font-size: 24px;
font-weight: bold;
}
/* Home content */
.home-content h2 {
font-size: 48px;
margin-bottom: 20px;
}
.home-content p {
font-size: 18px;
max-width: 600px;
}
/* Spline container */
.spline-container {
width: 100%;
height: 100vh;
position: relative;
}
/* About and Contact sections */
.about-section h2, .contact-section h2 {
font-size: 36px;
margin-bottom: 20px;
}
.about-section p, .contact-section p {
font-size: 18px;
max-width: 600px;
}
/* Responsive Design */
@media (max-width: 768px) {
.navbar {
padding: 15px 25px;
}
.home-content h2 {
font-size: 32px;
}
.home-content p,
.about-section p,
.contact-section p {
font-size: 16px;
}
}
Feel free to customize the colors, fonts, and layout as per your design preferences.
Final Code
1. main.jsx
// src/main.jsx
import React from 'react';
import ReactDOM from 'react-dom/client';
import App from './App';
import './styles/App.css';
ReactDOM.createRoot(document.getElementById('root')).render(
<React.StrictMode>
<App />
</React.StrictMode>
);
2. App.jsx
As provided earlier, integrates Locomotive Scroll and includes several sections with the Spline Viewer.
3. SplineViewer.jsx
As provided earlier, handles embedding the Spline Viewer Web Component.
4. useLocomotiveScroll.js
As provided earlier, initializes Locomotive Scroll.
Additional Enhancements
To make the landing page even more stunning, consider the following enhancements:
1. Scroll-Based Animations with GSAP
Integrate GSAP (GreenSock Animation Platform) for more advanced animations triggered by scroll events.
Installation:
npm install gsap
Example Usage:
// src/App.jsx
import { useEffect, useRef } from 'react';
import { gsap } from 'gsap';
import { ScrollTrigger } from 'gsap/ScrollTrigger';
// Initialize GSAP plugins
gsap.registerPlugin(ScrollTrigger);
function App() {
useLocomotiveScroll();
const aboutRef = useRef(null);
useEffect(() => {
gsap.from(aboutRef.current, {
opacity: 0,
y: 50,
duration: 1,
scrollTrigger: {
trigger: aboutRef.current,
start: 'top 80%',
toggleActions: 'play none none reverse',
},
});
}, []);
return (
<div data-scroll-container>
{/* Existing sections */}
{/* About Section with Ref */}
<section className="section about-section" data-scroll-section ref={aboutRef}>
<h2>About Us</h2>
<p>We create stunning websites with interactive designs.</p>
</section>
{/* Additional sections */}
</div>
);
}
This example fades in and slides up the "About Us" section as it comes into view.
2. Responsive Design Improvements
Ensure all elements are responsive across different devices. Test on various screen sizes and adjust CSS as necessary.
3. Accessibility Enhancements
- Semantic HTML: Use appropriate HTML tags.
- ARIA Attributes: Enhance accessibility for assistive technologies.
- Keyboard Navigation: Ensure users can navigate the page using the keyboard.
4. Performance Optimization
- Lazy Loading: Lazy load images and the Spline Viewer to improve initial load times.
- Code Splitting: Split code into chunks to reduce bundle size.
5. SEO Optimization
- Meta Tags: Add relevant meta tags for SEO.
- Content Structure: Use headings and structured content for better search engine understanding.
Complete Example
For clarity, here's the complete App.jsx and SplineViewer.jsx code.
App.jsx
// src/App.jsx
import React, { useEffect, useRef } from 'react';
import './styles/App.css';
import useLocomotiveScroll from './hooks/useLocomotiveScroll';
import SplineViewer from './components/SplineViewer';
import { gsap } from 'gsap';
import { ScrollTrigger } from 'gsap/ScrollTrigger';
gsap.registerPlugin(ScrollTrigger);
function App() {
useLocomotiveScroll();
const aboutRef = useRef(null);
const contactRef = useRef(null);
useEffect(() => {
// GSAP Animation for About Section
gsap.from(aboutRef.current, {
opacity: 0,
y: 50,
duration: 1,
scrollTrigger: {
trigger: aboutRef.current,
start: 'top 80%',
toggleActions: 'play none none reverse',
},
});
// GSAP Animation for Contact Section
gsap.from(contactRef.current, {
opacity: 0,
y: 50,
duration: 1,
delay: 0.5,
scrollTrigger: {
trigger: contactRef.current,
start: 'top 80%',
toggleActions: 'play none none reverse',
},
});
}, []);
return (
<div data-scroll-container>
{/* Navigation Section */}
<section className="section home-section" data-scroll-section>
<nav className="navbar">
<h1 className="logo">Stunning Landing</h1>
{/* Add navigation links if needed */}
</nav>
<div className="home-content">
<h2>Welcome to Our Website</h2>
<p>Experience the smoothest scrolling and interactive 3D animations.</p>
</div>
</section>
{/* Spline Viewer Section */}
<section className="section spline-section" data-scroll-section>
<SplineViewer />
</section>
{/* About Section */}
<section className="section about-section" data-scroll-section ref={aboutRef}>
<h2>About Us</h2>
<p>We create stunning websites with interactive designs.</p>
</section>
{/* Contact Section */}
<section className="section contact-section" data-scroll-section ref={contactRef}>
<h2>Contact Us</h2>
<p>Get in touch for amazing web experiences.</p>
</section>
</div>
);
}
export default App;
SplineViewer.jsx
// src/components/SplineViewer.jsx
import React, { useEffect, useRef } from 'react';
const SplineViewer = () => {
const splineRef = useRef(null);
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;
splineRef.current.appendChild(script);
// Cleanup
return () => {
if (splineRef.current) {
splineRef.current.removeChild(script);
}
};
}, []);
return (
<div className="spline-container" data-scroll data-scroll-speed="1">
<spline-viewer
url="https://prod.spline.design/pSfIVnqtcseH07-o/scene.splinecode"
style={{ width: '100%', height: '100vh' }}
></spline-viewer>
</div>
);
};
export default SplineViewer;
Conclusion
By following the steps above, you can create a visually stunning and interactive landing page using React, Locomotive Scroll, and Spline Viewer. Customize the sections, animations, and styles to match your brand and desired user experience. Remember to test across different devices and browsers to ensure consistency and responsiveness.
Feel free to reach out if you have any questions or need further assistance!