

在从数据库中检索数据时显示加载动画的最有效方法是什么?
发布于 2015-03-02 14:35:03
首先,最简单的解决方案是使用ajax调用来检索由php填充的表行。
JSFiddle
SIMPLE:
main.html / main.php
/*This makes the timeout variable global so all functions can access it.*/
var timeout;
/*This is an example function and can be disregarded
This function sets the loading div to a given string.*/
function loaded() {
$('#loading').html('The Ajax Call Data');
}
function startLoad() {
/*This is the loading gif, It will popup as soon as startLoad is called*/
$('#loading').html('<img src="http://rpg.drivethrustuff.com/shared_images/ajax-loader.gif"/>');
/*
This is an example of the ajax get method,
You would retrieve the html then use the results
to populate the container.
$.get('example.php', function (results) {
$('#loading').html(results);
});
*/
/*This is an example and can be disregarded
The clearTimeout makes sure you don't overload the timeout variable
with multiple timout sessions.*/
clearTimeout(timeout);
/*Set timeout delays a given function for given milliseconds*/
timeout = setTimeout(loaded, 1500);
}
/*This binds a click event to the refresh button*/
$('#start_call').click(startLoad);
/*This starts the load on page load, so you don't have to click the button*/
startLoad();img {
width: 100px;
}<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button id='start_call'>Refresh</button>
<div id='loading'></div>
php的示例如下所示
example.php
/*Database call to get list*/
foreach($list as $li){
echo "<tr><td>$li[var1]</td><td>$li[var2]</td></tr>";
}高级:
加载内容的一种更高级的方法是将网络工作者和多个数据库调用隔离成小块。
您将设置web工作者来执行小的100行结果,并在sql语句中使用LIMIT将结果页面划分为小块。然后,当其他结果正在加载时,您可以分页查看前100个结果。
这个过程比较困难,需要更长的时间来实现,但是会导致无缝和快速的加载。
编辑
如果要更改加载动画,只需更改URL即可。如果您希望在页面加载上加载URL,请将其放在div本身中。
/*This will make the img load on page load rather than DOM execution.*/
<div id='loading'>
<img src="http://rpg.drivethrustuff.com/shared_images/ajax-loader.gif"/>
</div>图像不一定是图像。它可以是你想要的任何加载图标。礼物又快又脏。你可以用像字体-可怕的旋转器这样的东西
发布于 2015-03-02 16:02:19
data.php来签出DB并构建表的HTML
function getData(){
//div contains the loader
$('#loader').show();
$.get('clients/data.php', function(data) {
//
$('#content').html(data);
//hide the loader
$('#loader').hide();
//build DataTable
$('#content table').dataTable();
});
}
getData();发布于 2015-02-27 21:41:48
这取决于您使用的语言,但基本原理是相同的。在查询完成时,只使用动画加载页面,然后用内容替换动画。
在jQuery中,这可能意味着用普通的HTML链接动画,然后使用AJAX单独调用数据库。当得到结果时,可以使用jQuery附加定位内容区域,并实时写入内容区域。
我包括PHP,因为您说不使用AJAX,但是在PHP中,结构是相同的:您需要链接图像,刷新页面,以便它显示,然后执行查询。在搜索结果中用否定的CSS margin-top覆盖动画,而Bob是您的叔叔。
https://stackoverflow.com/questions/28774634
复制相似问题