一行JS代码实现页面图片延迟加载[jquery lazyload]

    提高页面加载速度的方法有很多种,这里推荐一种在页面图片较多的时,提高页面整体加载速度的方法–延迟加载(lazyload)。

    当一个页面图片较多时,页面整体的数据就会比较大,一次load完再展示,页面打开会显得比较慢,延迟加载就是在浏览器在解析HTML时不对图片进行加载,当页面打开后,监控滚动条对本屏图片进行逐个载入,这样可以将一个页面数据请求分散为多个,大大减少一次请求load数据的时间,从而提高页面的加载速度。
    借助 jquery lazyload插件可以轻松实现延迟加载,调用方式如下:
    引入jquery和jquery lazyload插件: 

<script src="jquery.js" type="text/javascript"></script>
<script src="jquery.lazyload.js" type="text/javascript"></script>

图片标签处理:

<img data-original="img/example.jpg" width="640" height="480">
<!--
<img data-original="图片路径" width="640" height="480">
即把img标签src属性改为data-original
-->
    在页面加载完成后调用: 

<script>
 $(function() {
  $("img").lazyload();
  //加上淡出效果
  //$("img").lazyload({ effect : "fadeIn" });
 });
</script>
    下载地址:下载 
     转载请注明出处:怡然之乐 – FineYi

在ThinkPHP框架下用AjaxFileUploader插件实现ajax文件上传

在ThinkPHP框架下用AjaxFileUploader插件实现ajax文件上传,支持多种文件格式,页面无刷新上传。

在Lib/Action/目录下创建upAction.class.php文件,代码如下:

display();
}

/*
*@文件上传
*@author	FineYi
*@date		2013-01-23
*/
public function upLoadFile(){
	$error = "";
	$msg = "";
	$fileElementName = 'fileToUpload';
	if(!empty($_FILES[$fileElementName]['error'])){
		switch($_FILES[$fileElementName]['error']){
			case '1':
				$error = 'The uploaded file exceeds the upload_max_filesize directive in php.ini';
				break;
			case '2':
				$error = 'The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form';
				break;
			case '3':
				$error = 'The uploaded file was only partially uploaded';
				break;
			case '4':
				$error = 'No file was uploaded.';
				break;

			case '6':
				$error = 'Missing a temporary folder';
				break;
			case '7':
				$error = 'Failed to write file to disk';
				break;
			case '8':
				$error = 'File upload stopped by extension';
				break;
			case '999':
			default:
				$error = 'No error code avaiable';
		}
	}elseif(empty($_FILES['fileToUpload']['tmp_name']) || $_FILES['fileToUpload']['tmp_name'] == 'none'){
		$error = 'No file was uploaded..';
	}else{
			$re = $this->up();
			if(!$re){
				$error = 'Up file fail';
			}
			$msg = $re['savename'];	//文件名
			$path = '/upload/bizcoop/'.$msg;	//文件路径
			$size = $re['size'];	//文件大小
	}		
	echo json_encode(array('error'=>$error,'msg'=>$msg,'path'=>$path,'size'=>$size));exit;
}

private function up(){ 
	import('@.Org.UploadFile');//将上传类UploadFile.class.php拷到Lib/Org文件夹下
	$upload=new UploadFile();

	$upload->maxSize='-1';//默认为-1,不限制上传大小
	$upload->savePath= ICTSPACE_DIST_ROOT_PATH.'/www/upload/bizcoop/';//保存路径
	$upload->saveRule=uniqid;//上传文件的文件名保存规则
	$upload->uploadReplace=true;//如果存在同名文件是否进行覆盖
	$upload->allowExts=array('jpg','jpeg','png','gif');//准许上传的文件类型
	if($upload->upload()){
		$info=$upload->getUploadFileInfo();
		return $info[0];
	}else{
		return false;
		exit;
	} 
}

}
?>

在/Tpl/default/Up/目录下创建index.tpl文件,代码如下:

<div id="content">
<h1>Ajax File Upload Demo</h1>
<img id="loading" style="display: none;" alt="" src="__APP____PUBLIC__/style/img/loading.gif" />

<form action="" enctype="multipart/form-data" method="POST" name="form">
<table class="tableForm" cellspacing="0" cellpadding="0">
<thead>
<tr>
<th></th>
</tr>
</thead>
<tbody>
<tr>
<td><input class="input" id="fileToUpload" type="file" name="fileToUpload" size="45" /></td>
</tr>
<tr>
<td><button class="button" id="buttonUpload" onclick="return ajaxFileUpload();">Upload</button></td>
</tr>
</tbody>
<tbody>
<tr>
<td><span>已上传的附件:</span></td>
</tr>
</tbody>
<tfoot></tfoot>
</table>
</form></div>

在/Lib/Org/目录下放入ThinkPHP文件上传类:
下载ThinkPHP扩展包 下载
解压缩  类文件路径  /Extend/Library/ORG/Net/UploadFile.class.php

上传中loading文件:loading

demo上传效果:
0307201135
Jquery Ajax File Uploader插件下载地址 下载