Edit: browser-scrollbar.js
(function(window, document) { 'use strict'; let dnxteSimpleBarInstance; // Main function to wrap body content and initialize SimpleBar function initializeCustomScrollbar() { try { // Check if config exists if (typeof window.dnxte_scroll_config === 'undefined') { console.warn('DNXTE: Browser scrollbar config not found'); return false; } // Check if already initialized if (document.querySelector('#dnxte_simplebar')) { return true; } // Check if SimpleBar library is loaded if (typeof window.SimpleBar === 'undefined') { console.error('DNXTE: SimpleBar library not loaded'); return false; } const config = window.dnxte_scroll_config; // Wrap body content wrapBodyContent(config); // Initialize SimpleBar after DOM is ready requestAnimationFrame(() => { initializeSimpleBar(config); applyCustomStyling(config); }); return true; } catch (error) { console.error('DNXTE: Browser scrollbar initialization error:', error); return false; } } // Wrap body content with SimpleBar structure function wrapBodyContent(config) { const direction = config.alignment || 'ltr'; const autoHide = config.autohide || 'true'; // Create wrapper element const wrapper = document.createElement('div'); wrapper.id = 'dnxte_simplebar'; wrapper.className = 'dnxte_scrollbar_element'; wrapper.setAttribute('data-simplebar-auto-hide', autoHide); wrapper.setAttribute('data-simplebar-direction', direction); wrapper.style.height = '100vh'; // Move all body children to wrapper while (document.body.firstChild) { wrapper.appendChild(document.body.firstChild); } // Append wrapper to body document.body.appendChild(wrapper); } // Initialize SimpleBar function initializeSimpleBar(config) { try { const container = document.querySelector('#dnxte_simplebar'); if (!container || typeof window.SimpleBar === 'undefined') { return; } // Clean up existing instance if (dnxteSimpleBarInstance && typeof dnxteSimpleBarInstance.unMount === 'function') { dnxteSimpleBarInstance.unMount(); } // Convert autohide value to proper format let autoHideValue = true; switch(config.autohide) { case 'true': autoHideValue = true; break; case 'false': autoHideValue = false; break; case 'auto': autoHideValue = 'auto'; break; default: autoHideValue = true; } // Initialize SimpleBar with proper configuration dnxteSimpleBarInstance = new SimpleBar(container, { autoHide: autoHideValue, scrollbarMinSize: 25, scrollbarMaxSize: 100, forceVisible: config.autohide === 'false' }); // Remove aria-label attribute from content wrapper to prevent conflicts const contentWrapper = container.querySelector('.simplebar-content-wrapper'); if (contentWrapper) { contentWrapper.removeAttribute('aria-label'); } // Add custom wheel handling if needed if (config.step || config.speed) { addCustomWheelHandler(container, config); } // Set up scroll event propagation for Divi fixed header setupDiviCompatibility(); // Dispatch ready event document.dispatchEvent(new CustomEvent('dnxteSimpleBarReady', { detail: { instance: dnxteSimpleBarInstance } })); } catch (error) { console.error('DNXTE: SimpleBar initialization failed:', error); } } // Apply custom styling function applyCustomStyling(config) { const container = document.querySelector('#dnxte_simplebar'); if (!container) return; // Apply padding based on alignment if (config.alignment === "rtl") { container.style.paddingLeft = (config.width || 12) + 'px'; } else { container.style.paddingRight = (config.width || 12) + 'px'; } // Apply CSS custom properties for styling const styles = { '--scrollbar-width': (config.width || 12) + 'px', '--scrollbar-border-radius': (config.radius || 6) + 'px', '--scrollbar-thumb-color': config.thumbColor || '#888', '--scrollbar-track-color': config.trackColor || '#f1f1f1', '--scrollbar-thumb-shadow-color': config.shadowThumb || 'rgba(0,0,0,0.2)', '--scrollbar-rail-shadow-color': config.shadowTrack || 'rgba(0,0,0,0.1)', '--scrollbar-shadow-offset-y': (config.offsetY || 2) + 'px', '--scrollbar-shadow-blur': (config.blur || 4) + 'px' }; Object.entries(styles).forEach(([property, value]) => { container.style.setProperty(property, value); }); container.setAttribute('data-alignment', config.alignment || 'ltr'); } // Add custom wheel handler for scroll step and speed function addCustomWheelHandler(container, config) { const scrollableNode = dnxteSimpleBarInstance.getScrollElement(); if (!scrollableNode) return; const scrollStep = parseInt(config.step) || 40; const scrollSpeed = parseInt(config.speed) || 50; const speedMultiplier = scrollSpeed / 50; function handleWheel(e) { e.preventDefault(); const delta = e.deltaY * speedMultiplier; const scrollAmount = delta > 0 ? scrollStep : -scrollStep; scrollableNode.scrollTop += scrollAmount; } scrollableNode.addEventListener('wheel', handleWheel, { passive: false }); } // Set up Divi theme compatibility function setupDiviCompatibility() { if (!dnxteSimpleBarInstance) return; const scrollableElement = dnxteSimpleBarInstance.getScrollElement(); if (!scrollableElement) return; let lastScrollTop = 0; let isScrolling = false; let scrollTimer = null; // Function to handle fixed header logic function handleDiviFixedHeader() { const currentScrollTop = scrollableElement.scrollTop; const mainHeader = document.getElementById('main-header'); if (!mainHeader) return; // Get header height for offset calculation const headerHeight = mainHeader.offsetHeight; const scrollThreshold = headerHeight + 50; // Add some buffer // Check if we should show fixed header if (currentScrollTop > scrollThreshold) { // Add fixed header class if not already present if (!mainHeader.classList.contains('et-fixed-header')) { mainHeader.classList.add('et-fixed-header'); document.body.classList.add('et_fixed_header'); // Trigger custom event for other scripts that might be listening window.dispatchEvent(new CustomEvent('et-fixed-header-enabled')); } // Handle scroll direction for hide/show logic if (currentScrollTop > lastScrollTop && currentScrollTop > scrollThreshold + 150) { // Scrolling down - hide header mainHeader.classList.add('et-hide-fixed-header'); } else { // Scrolling up - show header mainHeader.classList.remove('et-hide-fixed-header'); } } else { // Remove fixed header class if (mainHeader.classList.contains('et-fixed-header')) { mainHeader.classList.remove('et-fixed-header'); mainHeader.classList.remove('et-hide-fixed-header'); document.body.classList.remove('et_fixed_header'); // Trigger custom event window.dispatchEvent(new CustomEvent('et-fixed-header-disabled')); } } lastScrollTop = currentScrollTop; } // Debounced scroll handler function onScroll() { if (!isScrolling) { window.requestAnimationFrame(function() { handleDiviFixedHeader(); isScrolling = false; }); isScrolling = true; } // Clear and set timer for scroll end detection clearTimeout(scrollTimer); scrollTimer = setTimeout(function() { handleDiviFixedHeader(); }, 150); } // Listen to SimpleBar scroll events scrollableElement.addEventListener('scroll', onScroll, { passive: true }); // Initial check on load setTimeout(handleDiviFixedHeader, 100); // Also propagate scroll position to window for other scripts let propagationFrame = null; scrollableElement.addEventListener('scroll', function() { if (propagationFrame) { cancelAnimationFrame(propagationFrame); } propagationFrame = requestAnimationFrame(function() { // Update a custom property that other scripts can check window.dnxteScrollTop = scrollableElement.scrollTop; window.dnxteScrollLeft = scrollableElement.scrollLeft; // Dispatch a custom scroll event window.dispatchEvent(new CustomEvent('dnxte-scroll', { detail: { scrollTop: scrollableElement.scrollTop, scrollLeft: scrollableElement.scrollLeft } })); }); }); } // Initialize when DOM is ready function init() { // Wait for both DOM and jQuery to be ready if (document.readyState === 'loading') { document.addEventListener('DOMContentLoaded', function() { // Additional check for jQuery if (typeof jQuery !== 'undefined') { jQuery(document).ready(initializeCustomScrollbar); } else { initializeCustomScrollbar(); } }); } else { // DOM is already loaded if (typeof jQuery !== 'undefined') { jQuery(document).ready(initializeCustomScrollbar); } else { initializeCustomScrollbar(); } } } // Start initialization init(); // Expose cleanup function globally if needed window.dnxteCleanupScrollbar = function() { if (dnxteSimpleBarInstance) { dnxteSimpleBarInstance.unMount(); dnxteSimpleBarInstance = null; } }; setTimeout(() => { if (jQuery('.et_pb_scroll_top.dnxte-back-to-top-wrapper').length) { function getScrollPosition() { // Check if SimpleBar is active if (dnxteSimpleBarInstance) { const scrollableElement = dnxteSimpleBarInstance.getScrollElement(); return scrollableElement ? scrollableElement.scrollTop : 0; } // Fallback to window scroll for normal scrollbars return jQuery(window).scrollTop(); } function handleBackToTopVisibility() { const scrollPosition = getScrollPosition(); if (scrollPosition > 800) { jQuery('.et_pb_scroll_top.dnxte-back-to-top-wrapper').show().removeClass('et-hidden').addClass('et-visible') } else { jQuery('.et_pb_scroll_top.dnxte-back-to-top-wrapper').removeClass('et-visible').addClass('et-hidden') } } // Bind to window scroll (for normal scrollbar) jQuery(window).on('scroll', handleBackToTopVisibility); // Bind to SimpleBar scroll (when SimpleBar is active) if (dnxteSimpleBarInstance) { const scrollableElement = dnxteSimpleBarInstance.getScrollElement(); if (scrollableElement) { jQuery(scrollableElement).on('scroll', handleBackToTopVisibility); } } // Set initial state handleBackToTopVisibility(); } jQuery('.et_pb_scroll_top.et-pb-icon.dnxte-back-to-top-wrapper').on('click', function () { // Check if SimpleBar is active if (dnxteSimpleBarInstance) { const scrollableElement = dnxteSimpleBarInstance.getScrollElement(); if (scrollableElement) { // Animate SimpleBar scroll element jQuery(scrollableElement).animate({ scrollTop: 0 }, 800); } } else { // Animate normal html/body scroll jQuery('html, body').animate({ scrollTop: 0 }, 800); } }); }, 1000 ); })(window, document);
Save
Back