jquery动态加载图片数据练习代码


Posted in Javascript onAugust 04, 2011

这几天研究jquery,感受到了该库的强大,而且找到本不错的书 <<锋利的jquery>>
这里我只是随便做了下,上面是照片列表和两个按钮,单击小图片下面显示大图片,当点击按钮时可以查看下一页,上一页的图片。
思路:

1、首先建一个照片查看页面viewer.htm,简单布局,上面是小图片和两个按钮,下面是大图片。

2、建一个一般处理程序viewServer.ashx,用来处理照片查看页面的请求。

3、然后当然要用到数据库啦,包括图片的路径,描述等信息。每张小图片路径应该对应一张大图片,单击小图片时候再加载,这里我没做小图片所以都用大图片加载了。

4、数据传输使用json,建立一个加载图片的函数,当页面加载或者单击left、right按钮的时候,通过ajax加载图片,将请求图片的开始编号、结束编号传递到后台页面,

后台页面收到请求信息后,在数据库中查找所需图片信息。
效果如图:
jquery动态加载图片数据练习代码
实现代码:
viewer.htm

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head> 
<title>我的照片</title> 
<style type="text/css"> 
#top{width:1000px;height:100px;margin:auto;} 
#leftBtn{width: 18px; text-align: left;height: 100px; float:left; background-image: url(images/images/left.jpg);} 
#rightBtn{width: 18px; height: 100px; float:right;background-image: url(images/images/right.jpg);} 
#smallPhoto{ text-align: center; float:left;margin-left:10px;margin-right:5px; } 
#bigPhoto{width:1000px;height:600px;text-align:center;margin:auto;} 
.photo{width:100px; height:100px;cursor:pointer;} 
#bottom{width:1000px;height:50px;margin:auto;} 
</style> 
<script src="../js/jquery-1.4.2.js" type="text/javascript"></script> 
<script type="text/javascript"> 
$(function(){ 
loadPhoto(1,9);//页面加载函数,每次显示9张图片,加载时候显示1-9 
$("#count").text("1"); 
$("#leftBtn").click(function(){ 
var firstIndex=parseInt($("#smallTr :first-child image").attr("id"),10); 
if(firstIndex<=1){ //如果已经到第一页了 
return; 
} 
else{ 
var startId=firstIndex-9; 
var endId=startId+8; 
$("#count").text(startId); 
loadPhoto(startId,endId); 
} 
}); 
$("#rightBtn").click(function(){ 
var sum=$("#sum").text(); 
var lastIndex=parseInt($("#smallTr :last-child image").attr("id"),10); 
if(lastIndex>=parseInt(sum,10)){ //如果已经到最后一页了 
return; 
} 
else{ 
var startId=lastIndex+1; 
var endId=startId+8; 
$("#count").text(startId); 
loadPhoto(startId,endId); 
} 
}); 
}); 
//获取图片总数 
function getCountPhoto(){ 
$.post("viewServer.ashx",{"action":"countPhoto"},function(data,status){ 
if(status!="success"){ 
alert("图片总数加载失败!"); 
} 
else{ 
$("#sum").text(data); 
} 
}); 
}; 
//加载图片的公共函数,索引从startid到endId 
function loadPhoto(startId,endId){ 
$.post("viewServer.ashx",{"startId":startId,"endId":endId,"action":"getData"},function(data,status){ //告诉后台要哪几张图片 
if(status!="success"){ 
alert("小图片加载失败!"); 
} 
else{ 
getCountPhoto(); //获取图片总数 
$("#smallTr").empty(); 
var photos=$.parseJSON(data); //使用json传递数据,json用着就是爽啊 
for(var i=0;i<photos.length;i++){ 
var photo=photos[i]; 
var td=$("<td ><img id='"+photo.Rownum+"' class='photo' src='images/"+photo.ImageUrl+"'/></td>"); 
$("#smallTr").append(td); 
} 
$("#tmpimg").attr("src","images/"+photos[0].ImageUrl); 
} 
//为每个小图片设置mouseover和click事件 
$("#smallTr img").mouseenter(function(){ 
$(this).attr("cursor","pointer"); 
}) 
.click(function(){ 
$("#count").text($(this).attr("id")); 
$("#tmpimg").attr("src",$(this).attr("src")); 
}); 
}); 
}; 
//如果图片加载过慢,提示加载中。。。。 
$("#loading").ajaxStart(function(){ 
$(this).show(); 
}) 
.ajaxStop(function(){ 
$(this).hide(); 
}); 
</script> 
</head> 
<body style="text-align: center;"> 
<form id="form1" runat="server"> 
<div id="top" style="text-align: center"> 
<input id="leftBtn" type="button" /> 
<div id="smallPhoto"> 
<table> 
<tr id="smallTr"> 
</tr> 
</table> 
</div> 
<input id="rightBtn" type="button" /> 
</div> 
<div id="bigPhoto"> 
<span style="display:none;" id="loading">加载中.....</span> <br /> <img id="tmpimg" src="" style="position: relative; height: 600px; width: 800px;"/> 
</div> 
<br /> 
<div id="bottom"> 
共<span id="sum" style="visibility: visible;"><strong>0</strong></span>张, 当前第<span id="count" style="visibility:visible;"><strong>0</strong></span>张 
</div> 
</form> 
</body> 
</html>

viewserver.ashx:
<%@ WebHandler Language="C#" Class="viewServer" %> 
using System; 
using System.Web; 
using System.Data.SqlClient; 
using System.Data; 
using System.Collections.Generic; 
public class viewServer : IHttpHandler 
{ 
public void ProcessRequest(HttpContext context) 
{ 
context.Response.ContentType = "text/plain"; 
string action = context.Request["action"].ToString(); 
if (action == "countPhoto") //统计共有多少图片 
{ 
string sql = "select count(*) from T_SmallPhotos"; 
DataTable dt = sqlHelper.GetTable(sql); 
int count = Convert.ToInt32(dt.Rows[0][0]); 
context.Response.Write(count.ToString()); 
} 
else if (action == "getData") //请求图片数据 
{ 
string startId = context.Request["startId"].ToString(); 
string endId = context.Request["endId"].ToString(); 
//string sqlStr = string sqlStr = "SELECT * FROM (SELECT id, [imageName], [imageUrl], [imageAlt], [notes], Row_Number() OVER (ORDER BY id) rownum FROM T_SmallPhotos) t WHERE t .rownum >= 1 AND t .rownum <=5" 
//这个查询语句有点小复杂,使用了开窗函数 
string sqlStr = "SELECT * FROM (SELECT id, [imageName], [imageUrl], [imageAlt], [notes], Row_Number() OVER (ORDER BY id) rownum FROM T_SmallPhotos) t WHERE t .rownum >= @startId AND t .rownum <= @endId"; 
//string sqlStr = "SELECT [id], [imageName], [imageUrl], [imageAlt], [notes] FROM [T_SmallPhotos] where id>1 and id<10"; 
SqlParameter[] param = new SqlParameter[] {new SqlParameter("@startId",startId), 
new SqlParameter("@endId",endId)}; 
DataTable dt = sqlHelper.GetTable(sqlStr, param); 
List<Photo> list = new List<Photo>(); 
for (int i = 0; i < dt.Rows.Count; i++) 
{ 
list.Add(new Photo(Convert.ToInt32(dt.Rows[i][0]), dt.Rows[i][1].ToString(), dt.Rows[i][2].ToString(), dt.Rows[i][3].ToString(), dt.Rows[i][4].ToString(), Convert.ToInt32(dt.Rows[i][5]))); 
} 
System.Web.Script.Serialization.JavaScriptSerializer jss = new System.Web.Script.Serialization.JavaScriptSerializer();//将数据序列化为json数据 
context.Response.Write(jss.Serialize(list)); 
} 
} 
public bool IsReusable 
{ 
get 
{ 
return false; 
} 
} 
//封装一个照片类,然后使用json传递 
public class Photo 
{ 
public Photo(int i, string name, string url, string alt, string notes, int rownum) 
{ 
id = i; 
imageName = name; 
imageUrl = url; 
imageAlt = alt; 
this.notes = notes; 
this.rownum = rownum; 
} 
private int id; //图片编号 
public int Id 
{ 
get { return id; } 
set { id = value; } 
} 
private string imageName;//图片名称 
public string ImageName 
{ 
get { return imageName; } 
set { imageName = value; } 
} 
private string imageUrl; //图片路径 
public string ImageUrl 
{ 
get { return imageUrl; } 
set { imageUrl = value; } 
} 
private string imageAlt; //图片描述 
public string ImageAlt 
{ 
get { return imageAlt; } 
set { imageAlt = value; } 
} 
private string notes; 
public string Notes 
{ 
get { return notes; } 
set { notes = value; } 
} 
private int rownum; 
public int Rownum 
{ 
get { return rownum; } 
set { rownum = value; } 
} 
} 
}

其中sqlHelper是我自定义的数据库访问类,比较简单就不贴出来了。
在实现过程中遇到一个ajax方面的问题,现在还是没搞太明白:
我的js代码中有两个请求函数,一个是获取图片总数getCountPhoto(),一个是加载图片的公共函数loadPhoto(startId,endId),我想在页面加载的时候同时调用这两个函数,分别显示出页码信息和具体图片列表,
$(function(){ 
loadPhoto(1,9); 

getCountPhoto(); 
}

这样的话发现页面内容总是错误,经过调试发现原来两个ajax请求是交叉执行,并不是一个执行完成执行另一个的。
这就是前几天做的了。
Javascript 相关文章推荐
JS中style属性
Oct 11 Javascript
jquery默认校验规则整理
Mar 24 Javascript
使用jQuery实现更改默认alert框体
Apr 13 Javascript
Jquery效果大全之制作电脑健康体检得分特效附源码下载
Nov 02 Javascript
基于jQuery和hwSlider实现内容左右滑动切换效果附源码下载(一)
Jun 22 Javascript
详解vue2 $watch要注意的问题
Sep 08 Javascript
JS实现的文字间歇循环滚动效果完整示例
Feb 13 Javascript
Vue 项目中遇到的跨域问题及解决方法(后台php)
Mar 28 Javascript
详解Angular5/Angular6项目如何添加热更新(HMR)功能
Oct 10 Javascript
微信小程序实现弹出菜单动画
Jun 21 Javascript
解决vuex刷新状态初始化的方法实现
Aug 15 Javascript
Vue路由权限控制解析
Nov 09 Javascript
jquery里的正则表达式说明
Aug 03 #Javascript
基于jQuery的图片剪切插件
Aug 03 #Javascript
jQuery + Flex 通过拖拽方式动态改变图片的代码
Aug 03 #Javascript
JavaScript 一道字符串分解的题目
Aug 03 #Javascript
JavaScript中去掉数组中的重复值的实现方法
Aug 03 #Javascript
JavaScript 大数据相加的问题
Aug 03 #Javascript
推荐11款jQuery开发的复选框和单选框美化插件
Aug 02 #Javascript
You might like
无线电波是什么?它是怎样传输的?
2021/03/01 无线电
php GD绘制24小时柱状图
2008/06/28 PHP
php自定义函数实现JS的escape的方法示例
2016/07/07 PHP
visual studio code 调试php方法(图文详解)
2017/09/15 PHP
PHP单例模式模拟Java Bean实现方法示例
2018/12/07 PHP
PNG背景在不同浏览器下的应用
2009/06/22 Javascript
jquery 回车事件实现代码
2011/08/23 Javascript
javascript eval(func())使用示例
2013/12/05 Javascript
文本框水印提示效果的简单实现代码
2014/02/22 Javascript
jQuery中filter()方法用法实例
2015/01/06 Javascript
JavaScript实现选择框按比例拖拉缩放的方法
2015/08/04 Javascript
鼠标悬停小图标显示大图标
2016/01/22 Javascript
微信小程序 下拉菜单简单实例
2017/04/13 Javascript
什么是Vue.js框架 为什么选择它?
2017/10/17 Javascript
浅析Visual Studio Code断点调试Vue
2018/02/27 Javascript
微信小程序:数据存储、传值、取值详解
2019/05/07 Javascript
vue开发中遇到的问题总结
2020/04/07 Javascript
js 获取扫码枪输入数据的方法
2020/06/10 Javascript
JS实现移动端可折叠导航菜单(现代都市风)
2020/07/07 Javascript
python中urllib模块用法实例详解
2014/11/19 Python
Python3使用turtle绘制超立方体图形示例
2018/06/19 Python
用Python中的turtle模块画图两只小羊方法
2019/04/09 Python
弄懂这56个Python使用技巧(轻松掌握Python高效开发)
2019/09/18 Python
手把手教你Python yLab的绘制折线图的画法
2019/10/23 Python
使用TensorFlow直接获取处理MNIST数据方式
2020/02/10 Python
django-orm F对象的使用 按照两个字段的和,乘积排序实例
2020/05/18 Python
python3.6使用SMTP协议发送邮件
2020/05/20 Python
用canvas画心电图的示例代码
2018/09/10 HTML / CSS
电子商务毕业生求职信
2013/11/10 职场文书
简历中求职的个人自我评价
2013/12/03 职场文书
领导班子三严三实心得体会
2014/10/13 职场文书
导游词怎么写
2015/02/04 职场文书
交通事故被告答辩状
2015/05/22 职场文书
2016年法制宣传月活动总结
2016/04/01 职场文书
python中opencv实现图片文本倾斜校正
2021/06/11 Python
浅谈resultMap的用法及关联结果集映射
2021/06/30 Java/Android