在jquery中显示加载器/微调器时,GIF图片不会动画化
GIF picture won't animate when displaying a loader/spinner in jquery
我只是想问我的问题。我在jquery中创建了一个函数,该函数在搜索后在我的页面中显示加载器。但我的问题是图像没有动画。
这是我的代码。我正在使用Codeigniter和jquery作为我的框架。
$('#search-btn').on('click',function(){
var query = $("#keyword").val();
var image = "<?php echo base_url()."/resources/loading/loading43.gif"; ?>";
$('#loading').html("<img src='"+image+"' class='loeader' align='center' />");
var query_url = "<?php echo site_url('item_controller/searchItem'); ?>";
$.ajax({
type:'POST',
url: query_url,
data:{query: $("#keyword").val()},
dataType:'json',
async: false,
success:function(d){
$('#loading').html("").hide();
//$('.display').dataTable().fnDestroy( true );
$("#example tbody").html("");
for(i in d){
$("#example tbody").append("<tr><td style='text-align: center; color:' data-code='TRUE'>" + d[i]['item_code'] + "</td><td style='text-align: left' data-name='TRUE'>" + d[i]['item_name'] + "</td><td><div style='text-align: center'><input type='button' value='ADD' class='k-button' id='" + d[i]['item_code'] + "' data-item=TRUE /></div></td></tr>");
}
//$("#search_result").show('blind');
$("[data-item]").on('click',function(){
var code = $(this).parents('tr').find('[data-code]').html();
var name = $(this).parents('tr').find('[data-name]').html();
// console.log(code,name);
$("#grid1 tbody").append("<tr><td style='text-align: center; width: 20%'><input type='text' name='code[]' value=" + code + " readonly style='width:50px; background-color: transparent; border-style: none' id=code" + counter_code++ +" /></td><td style='text-align: center; width: 40%'><input type='text' style='width: 90%; background-color: transparent; border-style: none' name='item_name[]' value='" + name + "' id=item"+ counter_item++ +" readonly /></td><td style='text-align: center'><input type='text' name='qty[]' id=qty"+ counter_qty++ +" style='text-align: center; width: 50px;' /></td><td style='text-align: center'><div align='center'><select style='width:100px; display: block' name='unit[]' id=unit"+ counter_unit++ +" ><option value=''>----</option><option value='pc/pcs'>PC/PCS</option><option value='BOX/BOXES'>BOX/BOXES</option></select></div></td><td style='text-align: center'><input type='text' name='price[]' id=price"+ counter_price++ +" style='text-align: right; width: 100px;' onblur='' /></td><td style='text-align: center'><input type='text' name='total[]' id=total"+ counter_total++ +" style='font-family: courier; text-align: right; background-color: lightgray; color: red; width: 100px;' readonly='readonly' value='' /></td></tr>");
ComputeTotal();
});
$('.display').dataTable({
"bPaginate": true,
"bLengthChange": true,
"bFilter": true,
"bSort": true,
"bInfo": true,
"bAutoWidth": false,
"bDestroy": true,
"bJQueryUI": false,
"bRetrieve": true,
"sPaginationType": "full_numbers",
"iDisplayLength": 25,
"destroy": true
});
//$('.display').dataTable().fnDestroy(true);
//$('.display').children().remove()
},
});
....
<div id="loading"></div>
<div id="search_result" class="k-content">
<div class="k-header" id="item-view-list" align="center">
<table border="0" style="width: 80%; align: left" cellpadding="10" cellspacing="10" align="left">
<tr>
<td colspan="3">
<h5>SEARCH ITEM</h5>
</td>
</tr>
<tr>
<td style="width: 3%">
<label>Enter Item Code</label>
</td>
<td style="width: 40%">
<input type="text" name="keyword" id="keyword" style="width: 80%" /> <input type="button" value="SEARCH" id="search-btn" class="k-button" style="font-size: 12px" />
</td>
</tr>
</table>
<hr />
<table cellpadding="0" cellspacing="0" border="0" class="display" id="example" style="font-size:small; width: 100%">
<thead>
<tr>
</tr>
<tr>
<th>CODE</th>
<th>NAME/DESCRIPTION</th>
<th></th>
</tr>
</thead>
<tbody>
</tbody>
</table>
<br />
</div>
</div>
发生这种情况是因为图像加载是异步的,并且在完成加载之前,您开始 ajax 加载(这也是异步的)。现在,他们正在争夺在事件队列中获得第一名。
您可以尝试先给图像一个加载的机会 - 最好的方法是使用图像的onload
回调调用 ajax 调用。
你也可以尝试延迟 ajax 调用这样做,只需像这样包装你的 $.ajax:
setTimeout(function() {$.ajax({
type:'POST',
url: query_url,
data:{query: $("#keyword").val()},
dataType:'json',
async: false,
success:function(d){ ...
...}}, 250); // delays 250 milliseconds
根据图像加载所需的时间,您可能需要增加或减少超时。
这就是为什么应该改用onload
事件的原因:
$('#loading').html("<img src='"+image+"' class='loeader'
align='center' onload='start()' />");
function start() {
$.ajax({
type:'POST',
url: query_url,
data:{query: $("#keyword").val()},
dataType:'json',
async: false,
success:function(d){ ...
...})
};
相关文章:
- 带有帧控制的画布GIF动画
- 单击时重播 GIF 动画/重新加载 GIF
- 在不重新加载文件的情况下重新启动 gif 动画
- 仅限 Firefox/IE - 元素 show() jquery 后冻结的 gif 动画
- 确定 GIF 动画状态
- mac safari中的Gif动画冻结
- 在网页上设置GIF动画-任何重新启动它们的方法
- 插入DOM时IE冻结GIF动画
- 如何在gif动画完成后激活页面滚动
- 用自定义间隔将Three.Js场景渲染为GIF动画文件
- 如何在Flash加载之前加载gif动画
- 在所有主流浏览器中,GIF动画和javascript在同一个线程上运行,这是真的吗?
- 在Firefox中显示页面请求后的gif动画
- 如何保持gif动画在网站上播放在指定的部分/锚
- 用JS控制GIF动画.播放,停止,倒放
- 在Node.js中使用GM生成GIF动画
- 在显示GIF动画后显示消息
- 我怎样才能制作一个“;循环一次”;在每次刷新/加载页面时播放GIF动画背景
- 如何使用javascript停止gif动画以防止高内存和cpu消耗
- Facebook JavaScript sdk 共享 gif 动画对话框轻拂