Search code examples
javascriptstellar.js

Turn Stellar.js on and off runtime


Is there a way to turn stellar.js on and off in code? I've tried to call "stellar" method with different parameters but seems like it only works once:

    $(document).ready(function() {
        $.stellar({
                verticalScrolling: true,
                verticalOffset: 0,
            });

        $.stellar({
                verticalScrolling: false, // is not turning scrolling off
                verticalOffset: 0,
            });

    });

Solution

  • If you want to reload the plugin, reset it before calling initialization function.

    Check the function code in stellar.js. If the options === 'destroy', plugin stellar is reset.

    $.fn[pluginName] = function (options) {
        var args = arguments;
        if (options === undefined || typeof options === 'object') {
            return this.each(function () {
                if (!$.data(this, 'plugin_' + pluginName)) {
                    $.data(this, 'plugin_' + pluginName, new Plugin(this, options));
                }
            });
        } else if (typeof options === 'string' && options[0] !== '_' && options !== 'init') {
            return this.each(function () {
                var instance = $.data(this, 'plugin_' + pluginName);
                if (instance instanceof Plugin && typeof instance[options] === 'function') {
                    instance[options].apply(instance, Array.prototype.slice.call(args, 1));
                }
                if (options === 'destroy') {
                    $.data(this, 'plugin_' + pluginName, null);
                }
            });
        }
    };
    

    So your code could be as following:

    $(document).ready(function() {
        $.stellar({
                verticalScrolling: true,
                verticalOffset: 0,
            });
    
        $.stellar("destroy");
    
        $.stellar({
                verticalScrolling: false, // is not turning scrolling off
                verticalOffset: 0,
            });
    
    });