2

我正在学习如何使用Spin.js,以便在网页加载时显示加载指示器(微调器)。

我让它工作了,但我不确定我是否在正确的页面生命周期中调用旋转/停止。之前可以显示微调器$(window).ready吗?

<script type="text/javascript">
var spinner;

$(window).ready(function loadingAnimation() {
    var opts = {
      lines: 13, // The number of lines to draw
      length: 7, // The length of each line
      width: 4, // The line thickness
      radius: 10, // The radius of the inner circle
      corners: 1, // 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: 'auto', // Top position relative to parent in px
      left: 'auto' // Left position relative to parent in px
    };
    var target = $("body")[0];
    spinner = new Spinner(opts).spin(target);
});

window.onload = function() {
    spinner.stop();
};

有关工作示例,请参阅http://sgratrace.appspot.com/industry.html

4

1 回答 1

6

我创建了一个对象来控制旋转:

Rats.UI.LoadAnimation = {
    "start" : function(){
        var opts = {
            lines : 13, // The number of lines to draw
            length : 7, // The length of each line
            width : 4, // The line thickness
            radius : 10, // The radius of the inner circle
            corners : 1, // 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 : $(window).height()/2.5, // Manual positioning in viewport
            left : "auto"
        };
        var target = $("body")[0];
        return new Spinner(opts).spin(target);
     },
     "stop" : function(spinner){
        spinner.stop();
     }
};

加载 DOM 后,我开始旋转:

$(document).ready(function(){
        // Once the DOM is loaded, start spinning
        spinner = Rats.UI.LoadAnimation.start();

        pageUI();

});

加载整个页面后,我停止旋转:

$(window).load(function(){
        // Once the page is fully loaded, stop spinning
        Rats.UI.LoadAnimation.stop(spinner);
});

window.onload 与 $(document).ready()有什么区别

请参阅我的 github 存储库中的完整代码:

于 2013-08-08T02:11:20.273 回答