gpt4 book ai didi

javascript - 如何使用 spin.js 显示页面加载指示器

转载 作者:行者123 更新时间:2023-11-29 16:16:10 25 4
gpt4 key购买 nike

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

最佳答案

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

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 仓库上查看完整代码:

关于javascript - 如何使用 spin.js 显示页面加载指示器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15403492/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com