-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmotion.js
More file actions
132 lines (111 loc) · 4.25 KB
/
motion.js
File metadata and controls
132 lines (111 loc) · 4.25 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
/* ============================================
MOTION.JS — Lightweight motion enhancements
============================================ */
(function() {
'use strict';
// ==========================================
// 1. FIX: Above-fold elements visible on load
// Immediately reveal any js-reveal or stagger
// elements that are already in the viewport
// ==========================================
function revealAboveFold() {
const reveals = document.querySelectorAll('.js-reveal, .stagger');
reveals.forEach(el => {
const rect = el.getBoundingClientRect();
if (rect.top < window.innerHeight && rect.bottom > 0) {
el.classList.add('visible');
}
});
}
// Run immediately and after a short delay for safety
revealAboveFold();
requestAnimationFrame(revealAboveFold);
// ==========================================
// 2. Card hover glow (cursor tracking)
// ==========================================
function initCardGlow() {
const cards = document.querySelectorAll('.card, .pricing-card, .pricing-card-full, .testimonial, .value-card');
cards.forEach(card => {
card.addEventListener('mousemove', (e) => {
const rect = card.getBoundingClientRect();
const x = ((e.clientX - rect.left) / rect.width * 100).toFixed(1);
const y = ((e.clientY - rect.top) / rect.height * 100).toFixed(1);
card.style.setProperty('--mouse-x', x + '%');
card.style.setProperty('--mouse-y', y + '%');
});
});
}
// ==========================================
// 3. Number counter animation
// Animates price numbers when scrolled into view
// ==========================================
function animateCounter(el, targetNum, prefix, suffix, duration) {
if (el.dataset.counted) return;
el.dataset.counted = 'true';
const startTime = performance.now();
const startNum = 0;
function update(currentTime) {
const elapsed = currentTime - startTime;
const progress = Math.min(elapsed / duration, 1);
// Ease-out curve
const eased = 1 - Math.pow(1 - progress, 3);
const current = Math.round(startNum + (targetNum - startNum) * eased);
el.textContent = prefix + current.toLocaleString() + suffix;
if (progress < 1) {
requestAnimationFrame(update);
} else {
el.textContent = prefix + targetNum.toLocaleString() + suffix;
}
}
requestAnimationFrame(update);
}
function initCounters() {
// Find price amounts that should be animated
const counterEls = document.querySelectorAll('[data-counter]');
if (counterEls.length === 0) return;
const counterObserver = new IntersectionObserver((entries) => {
entries.forEach(entry => {
if (entry.isIntersecting) {
const el = entry.target;
const target = parseInt(el.dataset.counter, 10);
const prefix = el.dataset.counterPrefix || '';
const suffix = el.dataset.counterSuffix || '';
const duration = parseInt(el.dataset.counterDuration || '800', 10);
// Small delay for visual effect
setTimeout(() => {
animateCounter(el, target, prefix, suffix, duration);
}, 100);
counterObserver.unobserve(el);
}
});
}, { threshold: 0.3 });
counterEls.forEach(el => counterObserver.observe(el));
}
// ==========================================
// 4. Smooth scroll for internal links
// ==========================================
function initSmoothScroll() {
document.querySelectorAll('a[href^="#"]').forEach(anchor => {
anchor.addEventListener('click', function(e) {
const target = document.querySelector(this.getAttribute('href'));
if (target) {
e.preventDefault();
target.scrollIntoView({ behavior: 'smooth', block: 'start' });
}
});
});
}
// ==========================================
// Initialize when DOM is ready
// ==========================================
if (document.readyState === 'loading') {
document.addEventListener('DOMContentLoaded', init);
} else {
init();
}
function init() {
initCardGlow();
initCounters();
initSmoothScroll();
}
})();