forked from yesiamrajeev/Hacktoberfest2025-Portfolio
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathanimations.js
More file actions
180 lines (152 loc) · 5.65 KB
/
animations.js
File metadata and controls
180 lines (152 loc) · 5.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
/**
* Enhanced Animations for Hacktoberfest Portfolio
* This file contains improved animations and scroll effects
*/
// Animation Observer for Scroll-Based Animations
const animationObserver = new IntersectionObserver(
(entries) => {
entries.forEach((entry) => {
// Add different animation styles based on element type or position
if (entry.isIntersecting) {
entry.target.classList.add("animate");
// Add custom animations based on position in viewport
const position = entry.boundingClientRect.top / window.innerHeight;
if (position < 0.3) {
entry.target.classList.add("animate-fast");
} else if (position < 0.7) {
entry.target.classList.add("animate-medium");
} else {
entry.target.classList.add("animate-slow");
}
// Add random animation delays for staggered effect
if (entry.target.classList.contains("stagger-animation")) {
const delay = Math.random() * 0.5;
entry.target.style.animationDelay = `${delay}s`;
}
} else {
// Optional: Remove animations when scrolled out of view
// Uncomment to enable
// entry.target.classList.remove("animate", "animate-fast", "animate-medium", "animate-slow");
}
});
},
{
threshold: 0.2, // Trigger when 20% of element is visible
rootMargin: "0px 0px -100px 0px" // Adjust the trigger area
}
);
// Enhanced version of existing animation functions
function enhancedAnimateOnScroll() {
const elements = document.querySelectorAll(
'.animate-on-scroll, .project, .card, .achievement-card, .info-card, .social-link, .a1, .a2, .a3, .a4'
);
elements.forEach((element) => {
// Add stagger animation to certain elements
if (element.classList.contains('card') || element.classList.contains('social-link')) {
element.classList.add('stagger-animation');
}
animationObserver.observe(element);
});
}
// Parallax Effects
function setupParallaxEffects() {
const parallaxElements = document.querySelectorAll('.parallax');
window.addEventListener('scroll', () => {
const scrolled = window.pageYOffset;
parallaxElements.forEach(element => {
const speed = element.dataset.speed || 0.5;
const offset = scrolled * speed;
element.style.transform = `translateY(${offset}px)`;
});
});
}
// Smooth scrolling with enhanced easing
function setupSmoothScroll() {
document.querySelectorAll('a[href^="#"]').forEach(anchor => {
anchor.addEventListener('click', function(e) {
e.preventDefault();
const targetId = this.getAttribute('href');
if (targetId === '#') return;
const targetElement = document.querySelector(targetId);
if (targetElement) {
// Improved smooth scrolling with easing
const targetPosition = targetElement.getBoundingClientRect().top + window.pageYOffset;
const startPosition = window.pageYOffset;
const distance = targetPosition - startPosition;
const duration = 1000;
let start = null;
function step(timestamp) {
if (!start) start = timestamp;
const progress = timestamp - start;
const easeInOutQuad = t => t < 0.5 ? 2 * t * t : -1 + (4 - 2 * t) * t;
// Easing function for smoother animation
window.scrollTo(0, startPosition + distance * easeInOutQuad(Math.min(progress / duration, 1)));
if (progress < duration) {
window.requestAnimationFrame(step);
}
}
window.requestAnimationFrame(step);
}
});
});
}
// Add scroll indicator
function addScrollIndicator() {
const indicator = document.createElement('div');
indicator.className = 'scroll-indicator';
document.body.appendChild(indicator);
window.addEventListener('scroll', () => {
const winScroll = document.body.scrollTop || document.documentElement.scrollTop;
const height = document.documentElement.scrollHeight - document.documentElement.clientHeight;
const scrolled = (winScroll / height) * 100;
indicator.style.width = scrolled + "%";
});
}
// Improved scroll-to-top button functionality
function enhanceScrollToTopButton() {
const scrollBtn = document.getElementById('scrollToTop');
if (!scrollBtn) return;
window.addEventListener('scroll', () => {
if (window.pageYOffset > 300) {
scrollBtn.classList.add('show');
} else {
scrollBtn.classList.add('hide');
setTimeout(() => {
if (window.pageYOffset <= 300) {
scrollBtn.classList.remove('show');
scrollBtn.classList.remove('hide');
}
}, 300);
}
});
scrollBtn.addEventListener('click', (e) => {
e.preventDefault();
// Animated scroll to top with easing
const scrollToTop = () => {
const c = document.documentElement.scrollTop || document.body.scrollTop;
if (c > 0) {
window.requestAnimationFrame(scrollToTop);
window.scrollTo(0, c - c / 10);
}
};
window.requestAnimationFrame(scrollToTop);
});
}
// Initialize all animation enhancements when DOM is loaded
document.addEventListener('DOMContentLoaded', () => {
enhancedAnimateOnScroll();
setupParallaxEffects();
setupSmoothScroll();
addScrollIndicator();
enhanceScrollToTopButton();
// Add page transition effect
document.body.classList.add('page-loaded');
});
// Add resize handler to adjust animations on window resize
window.addEventListener('resize', () => {
// Debounced resize handler
clearTimeout(window.resizeTimeout);
window.resizeTimeout = setTimeout(() => {
enhancedAnimateOnScroll();
}, 250);
});