Skip to Content
Menu

scrollToListeners()

Listen for events to use scrollTo() so they are not bundled with the action itself.

JavaScript February 8, 2021

Usage

This function runs automatically, so it is not called manually. Is this incorrect?

Was this page helpful? Yes No


    A feedback message is required to submit this form.


    Please check that you have entered a valid email address.

    Enter your email address if you would like a response.

    Thank you for your feedback!

    Source File

    Located in /assets/js/modules/helpers.js on line 239.

    No Hooks

    This function does not have any filters or actions available. Request one?
    JavaScript
    nebula.scrollToListeners = function(){
        if ( nebula.dom.html.css('scroll-behavior') !== 'smooth' ){ //If the html has smooth scroll-behavior, use that instead of this
            //An href starts with a hash ID but is not only a hash ("#content" but not "#"). Do not use *="#" to prevent conflicts with other libraries who are linking to separate pages with an anchor on the destination.
            nebula.dom.document.on('click keyup', 'a[href^="#"]:not([href="#"])', function(e){
                if ( e.type === 'click' || (e.type === 'keyup' && (e.key === ' ' || e.key === 'Enter')) ){ //Spacebar or Enter
                    let avoid = '.no-scroll, .mm-menu, .carousel, .tab-content, .modal, [data-bs-toggle], #wpadminbar, #query-monitor';
                    if ( !jQuery(this).is(avoid) && !jQuery(this).parents(avoid).length ){
                        if ( location.pathname.replace(/^\//, '') === this.pathname.replace(/^\//, '') && location.hostname === this.hostname ){ //Ensure the link does not have a protocol and is internal
                            let thisHash = this.hash; //Defined here because scope of "this" changes later
                            let scrollElement = jQuery.find(thisHash) || jQuery('[name=' + thisHash.slice(1) +']'); //Determine the target
    
                            if ( scrollElement.length ){ //If target exists
                                let pOffset = ( jQuery(this).attr('data-offset') )? parseFloat(jQuery(this).attr('data-offset')) : nebula.scroll.offset; //Determine the offset
                                let speed = nebula.scroll.speed || 500;
    
                                nebula.scrollTo(scrollElement, pOffset, speed, false, function(){
                                    history.replaceState({}, '', thisHash); //Add the hash to the URL so it can be refreshed, copied, links, etc. ReplaceState does this without affecting the back button.
                                });
    
                                return false;
                            }
                        }
                    }
                }
            });
        }
    
        //Using the nebula-scrollto class with data-scrollto attribute
        //Note: Unlike the above click listener, this method ignores the browser "smooth scroll" setting and always handles the scroll
        nebula.dom.document.on('click keyup', '.nebula-scrollto', function(e){
            if ( e.type === 'click' || (e.type === 'keyup' && (e.key === ' ' || e.key === 'Enter')) ){ //Spacebar or Enter
                let pOffset = ( jQuery(this).attr('data-offset') )? parseFloat(jQuery(this).attr('data-offset')) : nebula.scroll.offset;
    
                if ( jQuery(this).attr('data-scrollto') ){
                    let scrollElement = jQuery.find(jQuery(this).attr('data-scrollto'));
    
                    if ( scrollElement !== '' ){
                        let scrollSpeed = nebula.scroll.speed || 500;
                        nebula.scrollTo(scrollElement, pOffset, scrollSpeed);
                    }
                }
    
                return false;
            }
        });
    };
    

    Override

    To override or disable this JavaScript function, simply redeclare it with the exact same function name. Remember: Some functionality is conditionally loaded via dynamic imports, so if your function is not overriding properly, try listening for a DOM event (described below).

    JavaScript

    For non-module import functions:

    nebula.scrollToListeners = function(){
        //Write your own code here, leave it blank, or return false.
    }


    For dynamically imported module function overrides:

    jQuery(window).on('load', function(){
        nebula.scrollToListeners = function(){
            //Write your own code here, leave it blank, or return false.
        }
    });


    Custom Nebula DOM events do also exist, so you could also try the following if the Window Load listener does not work:

    jQuery(document).on('nebula_module_loaded', function(module){
        //Note that the module variable is also available to know which module specifically was imported
        if ( typeof nebula.scrollToListeners === 'function' ){
            nebula.scrollToListeners = function(){
                //Write your own code here, leave it blank, or return false.
            }
    	}
    });