Keshav 39245e1ec2 | 4 months ago | |
---|---|---|
README.md | 4 months ago |
While Webflow's visual interface makes animations and parallax effects accessible, adding a touch of code can unlock more creative possibilities and optimize performance. Here are some examples:
1. Basic Parallax Scrolling:
Example:
JavaScript
// Scroll factor (adjust for desired effect)
const scrollFactor = 0.5;
function updateParallax(event) {
const scrollTop = window.scrollY;
for (const element of document.querySelectorAll('.parallax-element')) {
const yOffset = element.dataset.offset * scrollFactor;
element.style.transform = `translateY(${yOffset}px)`;
}
}
window.addEventListener('scroll', updateParallax);
// Set data-offset attributes on parallax elements
// (e.g., data-offset="2" for twice the scroll speed)
2. Animated Parallax with Intersection Observer:
Example:
JavaScript
const options = {
root: null,
threshold: 0.5,
};
const observer = new IntersectionObserver((entries) => {
for (const entry of entries) {
if (entry.isIntersecting) {
entry.target.classList.add('parallax-animation');
} else {
entry.target.classList.remove('parallax-animation');
}
}
});
const parallaxElements = document.querySelectorAll('.parallax-element');
parallaxElements.forEach((element) => observer.observe(element));
3. Microinteractions with JavaScript:
Example:
JavaScript
const element = document.getElementById('micro-target');
element.addEventListener('mouseenter', () => {
element.classList.add('hover-animation');
});
element.addEventListener('mouseleave', () => {
element.classList.remove('hover-animation');
});
window.addEventListener('scroll', () => {
// Update element styles based on scroll position
});
Need custom Webflow Development?