【问题标题】:How to display page loading indicator using spin.js如何使用 spin.js 显示页面加载指示器
【发布时间】:2013-08-08 10:11:20
【问题描述】:

我正在学习如何使用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

【问题讨论】:

    标签: javascript jquery spinner usability


    【解决方案1】:

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

    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 vs $(document).ready()有什么区别

    在我的 github repo 上查看完整代码:

    【讨论】:

    • 你能告诉我如何获取 Rats.js 文件,我正在尝试在 MVC 中做同样的事情,你能否让我知道是否需要做其他事情来添加东西可见。
    • 嗨 Shivam,你可以在这里得到它github.com/seahrh/sgratrace/blob/master/war/js/rats.js
    • @oneleggednule- 你有 MVC 的解决方案吗,我正在研究 MVC5 并寻找同样的解决方案。谢谢。
    猜你喜欢
    • 2016-05-24
    • 2020-06-17
    • 1970-01-01
    • 2021-01-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多