contao-thememanager/ctm-sticky-header
Installation
composer require contao-thememanager/ctm-sticky-header
Ensure the package is autoloaded in composer.json and run composer dump-autoload.
Enable in Theme Manager
Configure CSS/JS
_stickyheader.css to the Layout stylesheet field.js_ctm_stickyheader to the JavaScript template field.CSS Order
Ensure your theme’s CSS includes _stickyheader.css before _theme.css (Theme Manager core) for proper rendering.
First Use Case
$sh-background-start (e.g., #ffffff) and $sh-color-start (e.g., #333333) in the Sticky Header settings.Dynamic Styling
Use the start/end color variables ($sh-background-start, $sh-background-end) to create gradient or transition effects as the header scrolls. Example:
// _stickyheader.css
.sticky-header {
background: var(--sh-background-start);
transition: background 0.3s ease;
}
.sticky-header.scrolled {
background: var(--sh-background-end);
}
Integration with Contao Elements
$sh-color-start/$sh-color-end for consistent hover/active states.JavaScript Hooks Extend functionality via Contao’s frontend hooks. Example: Trigger custom logic when the header sticks:
// config/autoload.php
$GLOBALS['TL_HOOKS']['ctmStickyHeaderOnStick'][] = function() {
// Add your custom JS logic here
echo '<script>console.log("Header stuck!");</script>';
};
Responsive Adjustments
Use media queries in _stickyheader.css to disable sticking on mobile:
@media (max-width: 768px) {
.sticky-header { position: relative !important; }
}
Performance Optimization
_stickyheader.css with other critical CSS files to reduce HTTP requests.js_ctm_stickyheader loading until after DOM content is loaded (if using Contao’s onload hooks).CSS Specificity Conflicts
_theme.css. Use !important sparingly; instead, increase specificity in _stickyheader.css:
body .sticky-header { /* Target more precisely */ }
JavaScript Errors
js_ctm_stickyheader is loaded after the DOM is ready. If using Contao’s onload hooks, verify the hook is registered correctly:
$GLOBALS['TL_JAVASCRIPT']['ctm_stickyheader'] = 'js/ctm_stickyheader.js|static';
Scroll Behavior Issues
will-change: transform to the CSS for smoother animations:
.sticky-header { will-change: transform; }
Theme Manager Cache
?purge=1) after enabling the module or updating settings. The backend cache may persist old configurations.Browser Compatibility
IntersectionObserver if needed.Inspect Variables Use browser dev tools to verify CSS variables are applied:
.sticky-header {
background: var(--sh-background-start) !important; /* Temporarily force */
}
Check JS Console Look for errors in the console when scrolling. Common issues:
IntersectionObserver support (add a fallback).Fallback for No-JS Style the header to appear sticky by default in CSS:
.sticky-header { position: sticky; top: 0; }
Custom Transitions
Override the default JavaScript behavior by extending the ctmStickyHeader class:
// js/custom_stickyheader.js
document.addEventListener('DOMContentLoaded', function() {
const originalStick = ctmStickyHeader.stick;
ctmStickyHeader.stick = function() {
originalStick.call(this);
// Add custom logic (e.g., analytics tracking)
};
});
Additional Triggers
Use Contao’s onload hooks to add sticky behavior to non-header elements:
$GLOBALS['TL_HOOKS']['frontendOutput'][] = function() {
if (\Input::get('autoSticky')) {
echo '<script>document.querySelector(".custom-element").classList.add("sticky");</script>';
}
};
Server-Side Logic Dynamically enable/disable the sticky header based on user roles or page types:
$GLOBALS['TL_HOOKS']['getPageLayout'][] = function($page) {
if ($page->type === 'home') {
$GLOBALS['TL_CONFIG']['ctmStickyHeaderEnabled'] = true;
}
};
How can I help you explore Laravel packages today?