<script type="module" src="https://unpkg.com/ionicons@7.1.0/dist/ionicons/ionicons.esm.js"></script>
<script nomodule src="https://unpkg.com/ionicons@7.1.0/dist/ionicons/ionicons.js"></script>
<!-- Navigation -->
<nav>
<div class="nav-container">
<div class="logo">ScrollDemo</div>
<div class="nav-links">
<button class="nav-btn" onclick="scrollToElement('home')">
<ion-icon name="home-outline"></ion-icon>
Home
</button>
<button class="nav-btn" onclick="scrollToElement('about')">
<ion-icon name="person-outline"></ion-icon>
About
</button>
<button class="nav-btn" onclick="scrollToElement('services')">
<ion-icon name="briefcase-outline"></ion-icon>
Services
</button>
<button class="nav-btn" onclick="scrollToElement('portfolio')">
<ion-icon name="images-outline"></ion-icon>
Portfolio
</button>
<button class="nav-btn" onclick="scrollToElement('contact')">
<ion-icon name="mail-outline"></ion-icon>
Contact
</button>
</div>
</div>
</nav>
<!-- Home Section -->
<section id="home">
<div class="section-content">
<h2>Welcome to ScrollDemo</h2>
<p>Click any navigation button above to smoothly scroll to different sections of the page. This demo showcases JavaScript's scroll-to-element functionality with smooth animations.</p>
<div class="scroll-indicator">
<ion-icon name="arrow-down-outline"></ion-icon>
Scroll down to explore
</div>
</div>
</section>
<!-- About Section -->
<section id="about">
<div class="section-content">
<h2>About Section</h2>
<p>This section demonstrates how JavaScript can scroll to specific elements by their ID. The scrolling is smooth and animated, providing a better user experience.</p>
<div class="feature-grid">
<div class="feature-card">
<ion-icon name="flash-outline"></ion-icon>
<h3>Fast</h3>
<p>Instant navigation to any section</p>
</div>
<div class="feature-card">
<ion-icon name="color-palette-outline"></ion-icon>
<h3>Smooth</h3>
<p>Beautiful scroll animations</p>
</div>
<div class="feature-card">
<ion-icon name="phone-portrait-outline"></ion-icon>
<h3>Responsive</h3>
<p>Works on all devices</p>
</div>
</div>
</div>
</section>
<!-- Services Section -->
<section id="services">
<div class="section-content">
<h2>Services Section</h2>
<p>The scroll functionality works with multiple methods: element.scrollIntoView() for smooth scrolling, and window.scrollTo() for precise positioning. Both methods are demonstrated in this page.</p>
<div class="scroll-indicator">
<ion-icon name="code-outline"></ion-icon>
Powered by JavaScript
</div>
</div>
</section>
<!-- Portfolio Section -->
<section id="portfolio">
<div class="section-content">
<h2>Portfolio Section</h2>
<p>Each section has a unique ID attribute that JavaScript uses as a target. When you click a navigation button, the page smoothly scrolls to bring that section into view.</p>
<div class="feature-grid">
<div class="feature-card">
<ion-icon name="rocket-outline"></ion-icon>
<h3>Method 1</h3>
<p>scrollIntoView() with smooth behavior</p>
</div>
<div class="feature-card">
<ion-icon name="settings-outline"></ion-icon>
<h3>Method 2</h3>
<p>Custom offset calculations</p>
</div>
<div class="feature-card">
<ion-icon name="checkmark-circle-outline"></ion-icon>
<h3>Best Practice</h3>
<p>Accessibility-friendly scrolling</p>
</div>
</div>
</div>
</section>
<!-- Contact Section -->
<section id="contact">
<div class="section-content">
<h2>Contact Section</h2>
<p>You've reached the bottom! Notice the scroll-to-top button that appears. Click it to smoothly return to the home section. This demonstrates how scrolling can enhance navigation in long-form content.</p>
</div>
</section>
<!-- Scroll to Top Button -->
<button class="scroll-top" id="scrollTopBtn" onclick="scrollToElement('home')">
<ion-icon name="arrow-up-outline"></ion-icon>
</button>
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
html {
scroll-behavior: smooth;
scroll-padding-top: 80px;
}
body {
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, sans-serif;
line-height: 1.6;
color: #333;
}
/* Navigation Bar */
nav {
position: fixed;
top: 0;
left: 0;
right: 0;
background: rgba(255, 255, 255, 0.95);
backdrop-filter: blur(10px);
box-shadow: 0 2px 10px rgba(0,0,0,0.1);
z-index: 1000;
padding: 0 20px;
}
.nav-container {
max-width: 1200px;
margin: 0 auto;
display: flex;
justify-content: space-between;
align-items: center;
padding: 16px 0;
}
.logo {
font-size: 24px;
font-weight: 700;
color: #8B4513;
}
.nav-links {
display: flex;
gap: 12px;
flex-wrap: wrap;
}
.nav-btn {
display: flex;
align-items: center;
gap: 6px;
padding: 10px 20px;
background: linear-gradient(135deg, #8B4513 0%, #A0522D 100%);
color: white;
border: none;
border-radius: 8px;
font-size: 14px;
font-weight: 500;
cursor: pointer;
transition: all 0.3s;
text-decoration: none;
}
.nav-btn:hover {
background: linear-gradient(135deg, #6F3609 0%, #8B4513 100%);
transform: translateY(-2px);
}
.nav-btn ion-icon {
font-size: 18px;
}
/* Sections */
section {
min-height: 100vh;
display: flex;
align-items: center;
justify-content: center;
padding: 100px 20px 60px;
}
.section-content {
max-width: 800px;
text-align: center;
}
section h2 {
font-size: 48px;
margin-bottom: 20px;
color: #333;
}
section p {
font-size: 18px;
color: #666;
margin-bottom: 30px;
line-height: 1.8;
}
.scroll-indicator {
display: inline-flex;
align-items: center;
gap: 8px;
padding: 12px 24px;
background: rgba(255, 255, 255, 0.2);
border-radius: 8px;
color: white;
font-weight: 500;
margin-top: 20px;
}
.scroll-indicator ion-icon {
font-size: 24px;
animation: bounce 2s infinite;
}
@keyframes bounce {
0%, 20%, 50%, 80%, 100% {
transform: translateY(0);
}
40% {
transform: translateY(-10px);
}
60% {
transform: translateY(-5px);
}
}
/* Section Colors */
#home {
background: linear-gradient(135deg, #6F4E37 0%, #8B6F47 100%);
color: white;
}
#home h2,
#home p {
color: white;
}
#about {
background: linear-gradient(135deg, #D2B48C 0%, #F5DEB3 100%);
}
#services {
background: linear-gradient(135deg, #8B4513 0%, #A0522D 100%);
color: white;
}
#services h2,
#services p {
color: white;
}
#portfolio {
background: linear-gradient(135deg, #DEB887 0%, #F4A460 100%);
}
#contact {
background: linear-gradient(135deg, #654321 0%, #8B4513 100%);
color: white;
}
#contact h2,
#contact p {
color: white;
}
/* Scroll to Top Button */
.scroll-top {
position: fixed;
bottom: 30px;
right: 30px;
width: 50px;
height: 50px;
background: linear-gradient(135deg, #8B4513 0%, #A0522D 100%);
color: white;
border: none;
border-radius: 50%;
font-size: 24px;
cursor: pointer;
display: none;
align-items: center;
justify-content: center;
box-shadow: 0 4px 12px rgba(0,0,0,0.2);
transition: all 0.3s;
z-index: 999;
}
.scroll-top:hover {
background: linear-gradient(135deg, #6F3609 0%, #8B4513 100%);
transform: translateY(-5px);
}
.scroll-top.show {
display: flex;
}
.feature-grid {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
gap: 20px;
margin-top: 40px;
}
.feature-card {
background: white;
padding: 24px;
border-radius: 12px;
box-shadow: 0 4px 12px rgba(0,0,0,0.1);
transition: transform 0.3s;
}
.feature-card:hover {
transform: translateY(-5px);
}
.feature-card ion-icon {
font-size: 48px;
color: #8B4513;
margin-bottom: 12px;
}
.feature-card h3 {
font-size: 20px;
margin-bottom: 8px;
color: #333;
}
.feature-card p {
font-size: 14px;
color: #666;
}
@media (max-width: 768px) {
section h2 {
font-size: 36px;
}
.nav-links {
gap: 8px;
}
.nav-btn {
padding: 8px 16px;
font-size: 13px;
}
}
// Method 1: Scroll to element using scrollIntoView with smooth animation
function scrollToElement(elementId) {
const element = document.getElementById(elementId);
if (element) {
// Use smooth scrolling with proper timing
element.scrollIntoView({
behavior: 'smooth',
block: 'start',
inline: 'nearest'
});
}
}
// Alternative Method 2: Scroll using animated scrollTo
function scrollToElementWithOffset(elementId, offset = 80) {
const element = document.getElementById(elementId);
if (element) {
const elementPosition = element.getBoundingClientRect().top;
const offsetPosition = elementPosition + window.pageYOffset - offset;
window.scrollTo({
top: offsetPosition,
behavior: 'smooth'
});
}
}
// Method 3: Custom smooth scroll animation for maximum browser compatibility
function smoothScrollTo(elementId, duration = 800) {
const element = document.getElementById(elementId);
if (!element) return;
const targetPosition = element.getBoundingClientRect().top + window.pageYOffset - 80;
const startPosition = window.pageYOffset;
const distance = targetPosition - startPosition;
let startTime = null;
function animation(currentTime) {
if (startTime === null) startTime = currentTime;
const timeElapsed = currentTime - startTime;
const run = easeInOutCubic(timeElapsed, startPosition, distance, duration);
window.scrollTo(0, run);
if (timeElapsed < duration) requestAnimationFrame(animation);
}
function easeInOutCubic(t, b, c, d) {
t /= d / 2;
if (t < 1) return c / 2 * t * t * t + b;
t -= 2;
return c / 2 * (t * t * t + 2) + b;
}
requestAnimationFrame(animation);
}
// Show/hide scroll-to-top button based on scroll position
window.addEventListener('scroll', function() {
const scrollTopBtn = document.getElementById('scrollTopBtn');
if (window.pageYOffset > 300) {
scrollTopBtn.classList.add('show');
} else {
scrollTopBtn.classList.remove('show');
}
});
// Optional: Handle URL hash navigation
window.addEventListener('load', function() {
if (window.location.hash) {
const targetId = window.location.hash.substring(1);
setTimeout(() => scrollToElement(targetId), 100);
}
});
No comments yet. Be the first!