1

我正在使用这个 JavaScript Spinner/loader 项目http://fgnass.github.io/spin.js/

我在这里的 JSFiddle 上有一些代码http://jsfiddle.net/jasondavis/9pBsr/显示了我的进度,它可能看起来对我拥有的所有功能有点矫枉过正,但我​​已经删除了这篇文章的所有非相关内容. 因此,如果您能帮助我,请保持所有结构不变。

现在我的问题。我正在使用的库有这个代码来显示微调器

var spinner = new Spinner(opts).spin(target);

文档说要杀死并隐藏微调器以在其上运行stop函数,Spinner但是我的代码的结构方式,我不确定如何访问它,因为我不断收到类似的错误

TypeError: Cannot call method 'stop' of undefined

我的最终结果是能够调用它并让它停止/杀死微调器......

zPanel.loader.hideLoader()

这是我的 JavaScript,但所有 JS 和 HTML 都在这个 JSFiddle http://jsfiddle.net/jasondavis/9pBsr/

请帮我获取zPanel.loader.hideLoader()调用函数的zPanel.loader.buildSpinner()函数Spinner.stop()

var zPanel = {

    init: function() {
        $(document).ready(function() {
            zPanel.loader.init();
        });
    },



    loader: {

        init: function() {
            //Bind zloader to button click
            $('#button').click(function() {
                zPanel.loader.showLoader();
            });

            $('#hidebutton').click(function() {
                zPanel.loader.hideLoader();
            });
        },

        showLoader: function() {
            //Show Spinning Loader
            $('#zloader_overlay').fadeIn('fast', function() {
                $("#zloader").show();
                zPanel.loader.buildSpinner();
            });
        },

        hideLoader: function() {
            //Hide  Spinning Loader
            $('#zloader_overlay').fadeIn('fast', function() {
                $("#zloader").hide();

                // This is the function that is not working yet
                //zPanel.loader.spinner('stop');
                zPanel.loader.buildSpinner.spinner.stop();
            });
        },

        buildSpinner: function() {

            var opts = {
              lines: 9, // The number of lines to draw
              length: 11, // The length of each line
              width: 13, // The line thickness
              radius: 40, // The radius of the inner circle
              corners: 0.4, // Corner roundness (0..1)
              rotate: 0, // The rotation offset
              color: '#000', // #rgb or #rrggbb
              speed: 1, // Rounds per second
              trail: 60, // Afterglow percentage
              shadow: false, // Whether to render a shadow
              hwaccel: false, // Whether to use hardware acceleration
              className: 'spinner', // The CSS class to assign to the spinner
              zIndex: 2e9, // The z-index (defaults to 2000000000)
              top: 200, // Top position relative to parent in px
              left: 'auto' // Left position relative to parent in px
            };

            var target = document.getElementById('zloader_content');
            var spinner = new Spinner(opts).spin(target);

            // I need to call spinner.stop() some how from my function above name hideLoader()

        },

    }

};

zPanel.init();
4

3 回答 3

1

让您的微调器成为您的 zPanel 的成员。

var zPanel = {

    spinner:null,

    init: function() {
        $(document).ready(function() {
            zPanel.loader.init();
        });
    },



    loader: {

        init: function() {
            //Bind zloader to button click
            $('#button').click(function() {
                zPanel.loader.showLoader();
            });

            $('#hidebutton').click(function() {
                zPanel.loader.hideLoader();
            });
        },

        showLoader: function() {
            //Show Spinning Loader
            $('#zloader_overlay').fadeIn('fast', function() {
                $("#zloader").show();
                zPanel.loader.buildSpinner();
            });
        },

        hideLoader: function() {
            //Hide  Spinning Loader
            $('#zloader_overlay').fadeIn('fast', function() {
                $("#zloader").hide();
                zPanel.spinner.stop();
            });
        },

        buildSpinner: function() {

            var opts = {
              lines: 9, // The number of lines to draw
              length: 11, // The length of each line
              width: 13, // The line thickness
              radius: 40, // The radius of the inner circle
              corners: 0.4, // Corner roundness (0..1)
              rotate: 0, // The rotation offset
              color: '#000', // #rgb or #rrggbb
              speed: 1, // Rounds per second
              trail: 60, // Afterglow percentage
              shadow: false, // Whether to render a shadow
              hwaccel: false, // Whether to use hardware acceleration
              className: 'spinner', // The CSS class to assign to the spinner
              zIndex: 2e9, // The z-index (defaults to 2000000000)
              top: 200, // Top position relative to parent in px
              left: 'auto' // Left position relative to parent in px
            };

            var target = document.getElementById('zloader_content');
            zPanel.spinner = new Spinner(opts).spin(target);

            // I need to call spinner.stop() some how from my function above name hideLoader()

        },

    }

};

zPanel.init();
于 2013-04-05T23:50:36.143 回答
1

将微调器保存到附加到 zPanel 的变量中,然后使用该引用停止微调器,这个怎么样:

var zPanel = {

    init: function() {
        $(document).ready(function() {
            zPanel.loader.init();
        });
    },



    loader: {

        init: function() {
            //Bind zloader to button click
            $('#button').click(function() {
                zPanel.loader.showLoader();
            });

            $('#hidebutton').click(function() {
                zPanel.loader.hideLoader();
            });
        },

        showLoader: function() {
            //Show Spinning Loader
            $('#zloader_overlay').fadeIn('fast', function() {
                $("#zloader").show();
                //showDiv();
                zPanel.spinner = zPanel.loader.buildSpinner();
            });
        },

        hideLoader: function() {
            //Hide  Spinning Loader
            $('#zloader_overlay').fadeIn('fast', function() {
                $("#zloader").hide();
                //showDiv();
                //zPanel.loader.spinner('stop');
                zPanel.spinner.stop();
            });
        },

        buildSpinner: function() {

            var opts = {
              lines: 9, // The number of lines to draw
              length: 11, // The length of each line
              width: 13, // The line thickness
              radius: 40, // The radius of the inner circle
              corners: 0.4, // Corner roundness (0..1)
              rotate: 0, // The rotation offset
              color: '#000', // #rgb or #rrggbb
              speed: 1, // Rounds per second
              trail: 60, // Afterglow percentage
              shadow: false, // Whether to render a shadow
              hwaccel: false, // Whether to use hardware acceleration
              className: 'spinner', // The CSS class to assign to the spinner
              zIndex: 2e9, // The z-index (defaults to 2000000000)
              top: 200, // Top position relative to parent in px
              left: 'auto' // Left position relative to parent in px
            };
            //var target = document.getElementById('zloader');
            var target = document.getElementById('zloader_content');
            var spinner = new Spinner(opts).spin(target);

            return spinner;
        },

    }

};

zPanel.init();
于 2013-04-05T23:51:05.270 回答
1

zPanel 是一个对象。zPanel 中的函数只使用它们自己的变量。为了能够调用 spinner 对象,只需在 zPanel 中创建一个 spinner 属性并让所有函数都使用此属性:

var zPanel = {

    spinner: null,  //Notice the property!

    init: function() {
        $(document).ready(function() {
            zPanel.loader.init();
        });
    },



    loader: {

        init: function() {
            //Bind zloader to button click
            $('#button').click(function() {
                zPanel.loader.showLoader();
            });

            $('#hidebutton').click(function() {
                zPanel.loader.hideLoader();
            });
        },

        showLoader: function() {
            //Show Spinning Loader
            $('#zloader_overlay').fadeIn('fast', function() {
                $("#zloader").show();
                //showDiv();
                zPanel.loader.buildSpinner();
            });
        },

        hideLoader: function() {
            var self = this; //Create a variable that is accesable within the fadeIn
            //Hide  Spinning Loader
            $('#zloader_overlay').fadeIn('fast', function() {
                $("#zloader").hide();
                //showDiv();
                //Below code has changed!!
                self.spinner('stop');
                zPanel.loader.buildSpinner.spinner.stop();
            });
        },

        buildSpinner: function() {

            var opts = {
              lines: 9, // The number of lines to draw
              length: 11, // The length of each line
              width: 13, // The line thickness
              radius: 40, // The radius of the inner circle
              corners: 0.4, // Corner roundness (0..1)
              rotate: 0, // The rotation offset
              color: '#000', // #rgb or #rrggbb
              speed: 1, // Rounds per second
              trail: 60, // Afterglow percentage
              shadow: false, // Whether to render a shadow
              hwaccel: false, // Whether to use hardware acceleration
              className: 'spinner', // The CSS class to assign to the spinner
              zIndex: 2e9, // The z-index (defaults to 2000000000)
              top: 200, // Top position relative to parent in px
              left: 'auto' // Left position relative to parent in px
            };
            //var target = document.getElementById('zloader');
            var target = document.getElementById('zloader_content');

            //Below line has changed!!!
            this.spinner = new Spinner(opts).spin(target);

            //if(spinStart == 'stop'){
            //    zPanel.loader.spinner.spinner.stop();
            //}

        },

    }
于 2013-04-05T23:53:00.583 回答