Sticky Title Bar - ScrollTrigger Fix
Sticky Title Bar - ScrollTrigger Fix
The Problem
The featured title bar was not sticking to the top when scrolling. This was due to CSS position: sticky being broken by parent elements with:
overflow: hidden(main element)transformproperties (various parents)filterproperties (various parents)
CSS sticky is notoriously fragile and breaks easily.
The Solution
Use GSAP ScrollTrigger's pin() method instead of CSS sticky positioning.
ScrollTrigger is:
- ✅ Already loaded in the project
- ✅ Reliable and works regardless of parent CSS
- ✅ Easy to implement
- ✅ Properly integrated with Swup transitions
Implementation
JavaScript (assets/js/featured-title-bar.js)
this.scrollTrigger = ScrollTrigger.create({
trigger: featuredSection,
start: 'top top',
end: 'bottom top',
pin: titleBar,
pinSpacing: false,
markers: false
});
How It Works
- When you scroll into the featured section, the title bar pins to the top
- It stays pinned while you're scrolling through the section
- When you scroll past the section, it unpins
Integration
- Init:
main.jsinitializes on page load - Swup transitions:
swup-modern-clean.jscalls:cleanup()before page transition (kills ScrollTrigger)reinitialize()after page transition (recreates ScrollTrigger)
Files Changed
/assets/js/featured-title-bar.js- Added ScrollTrigger pin/assets/css/featured-title-bar.css- Removed CSS sticky positioning/assets/css/components/gallery.css- Changedoverflow-x: hiddentobodyinstead ofmain
Testing
Refresh the page and scroll down the homepage. The "Featured Work" title bar should now stick to the top while scrolling through the featured posts section.
Debugging
To see ScrollTrigger markers for debugging, set:
markers: true // in featured-title-bar.js line 56