显示和隐藏加载gif的最佳实践
Best practice for display and hiding of loading gifs
我有一个页面,在其中单击一个按钮将另一个页面的内容加载到一个div中。我有一个加载gif,我想显示,当我点击按钮,当然,我想消失时,另一个页面的内容已经完全加载到div"maincontent"。下面是我目前的代码-这是实现这种效果的最佳实践-不知怎的,我认为一定有更好的方法来做到这一点?
index . php:
<html>
<head>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function() {
$('#loading').hide();
$(document).on('click', '.menu1', function(){
$('#loading').show();
$('.maincontent').load("1.php");
});
});
</script>
<style>
#loading {
width: 100%;
height: 100%;
top: 0;
left: 0;
position: fixed;
display: block;
opacity: 0.7;
background-color: #fff;
z-index: 99;
text-align: center;
}
#loading-image {
position: absolute;
top: 25%;
left: 50%;
z-index: 100;
}
</style>
</head>
<body>
<button class="menu1">Click</button>
<div class="maincontent">
<div id="loading">
<img id="loading-image" src="images/ajax-loader.gif" alt="Loading..." />
</div>
</div>
</body>
</html>
您可以使用.ajaxStart
和.ajaxStop
事件分别显示和隐藏加载器。请检查下面的代码片段。
$(document).ready(function() {
$('#loading').hide().ajaxStart( function() {
$(this).show(); // show Loading Div
} ).ajaxStop ( function(){
$(this).hide(); // hide loading div
});
$(document).on('click', '.menu1', function(){
$('.maincontent').load("1.php");
});
});
相关文章:
- 在localhost Dev Box上测试JSONP请求的最佳方式
- 有条件更新d3.js力图中节点的最佳方法
- 为react组件传递道具的最佳方式
- 与运行长作业(javascript,node.js)的第三方API同步的最佳实践
- 让Webpack管理Quirky AMD定义的最佳方式
- 在承诺链中处理早期回报的最佳方式
- 先预加载动画gif
- 将jQuery.ech()方法转换为本地JavaScript抽象的最佳方法是什么
- Angularjs 1.5.x本地化最佳实践
- 处理浮点错误的最佳方法是什么
- javascript导入的最佳实践是什么
- MobileFirst:在客户端运行计时器作业-最佳选项
- 在ng重复循环中显示条件内容的最佳方式是什么
- 在phonegap中为android调用onload函数的最佳方式
- 实现比较方法的最佳实践是什么;s的比较类型是在运行时选择的
- 从数组中删除元素的最佳方法是:javascript/jquery
- 链接两个网页或网络应用程序的最佳方式
- 什么's是连接供应商js文件的最佳方式
- 如果用户点击浏览器取消按钮,隐藏正在加载的gif的最佳方式
- 显示和隐藏加载gif的最佳实践