class="article_content clearfix">
class="htmledit_views">
最近在学习JQuery,想从原理上模拟一下无刷新的相册浏览。
最先想到的思路是利用缓存,也就是先显示提示消息,然后get图片,在get完的时候回调,将img标签的src改掉,由于刚刚get过,有了缓存,所以图片会立刻显示出来。
NextPic内容: {
在CHROME和FF下显示正常,IE6下异常,IE7、8未测试。
后来在ASPRAIN的开发者 稽山草 的帮助下,思路改成了先改src,然后绑定onload事件,在onload中回调。
核心代码:
中间想到会不会是事件绑定的问题,因为onclick事件的绑定是$(Element).bind("click",class="tags" href="/tags/CALLBACK.html" title=callback>callback),可以简写成$(Element).click(class="tags" href="/tags/CALLBACK.html" title=callback>callback),想到$(Element).bind("load",class="tags" href="/tags/CALLBACK.html" title=callback>callback)和$(Element).load(url,class="tags" href="/tags/CALLBACK.html" title=callback>callback)会不会是一样,查资料写得都不怎么清楚,改了试一下,还是不一样,不过在chrome和ff下依然能工作,但是数据不太正常,IE下报错。
再次检查 稽山草 给的代码,发现问题在哪了。
load事件的绑定的是一个匿名函数,而当按钮按下时会再次绑定,所以会重复执行。于是将绑定bind改成one搞定。最终完整代码如下:
最先想到的思路是利用缓存,也就是先显示提示消息,然后get图片,在get完的时候回调,将img标签的src改掉,由于刚刚get过,有了缓存,所以图片会立刻显示出来。
页面元素:
class="language-html"><input class="picbtn" type="button" value="Next" />
<div class="tip">正在加载……</div>
<div class="notice">
<img />
</div>
按钮的事件绑定:
class="language-html">$(".picbtn").click(class="tags" href="/tags/FUNCTION.html" title=function>function(){NextPic();});
定义了一个数组PicArr用来记录所有图片
NextPic内容: {
class="language-html">$(".tip").slideDown(200); //显示提示
$.get(PicArr[CurrPic],class="tags" href="/tags/FUNCTION.html" title=function>function(){
$("img").attr("src",PicArr[CurrPic]);
$(".tip").slideUp(200);
CurrPic++;
if(CurrPic>4)
CurrPic=0;
});}
class="language-html">
在CHROME和FF下显示正常,IE6下异常,IE7、8未测试。
后来在ASPRAIN的开发者 稽山草 的帮助下,思路改成了先改src,然后绑定onload事件,在onload中回调。
核心代码:
class="language-javascript">$("img").attr("src",PicArr[CurrPic])
.bind('load',class="tags" href="/tags/FUNCTION.html" title=function>function(){$(".tip").slideUp(200);CurrPic++;if(CurrPic>4)CurrPic=0;});
后来一看基本正常了,但是仔细看还是不正常,图片顺序开始乱跳,跟踪了好久发现回调函数会多次运行。
中间想到会不会是事件绑定的问题,因为onclick事件的绑定是$(Element).bind("click",class="tags" href="/tags/CALLBACK.html" title=callback>callback),可以简写成$(Element).click(class="tags" href="/tags/CALLBACK.html" title=callback>callback),想到$(Element).bind("load",class="tags" href="/tags/CALLBACK.html" title=callback>callback)和$(Element).load(url,class="tags" href="/tags/CALLBACK.html" title=callback>callback)会不会是一样,查资料写得都不怎么清楚,改了试一下,还是不一样,不过在chrome和ff下依然能工作,但是数据不太正常,IE下报错。
再次检查 稽山草 给的代码,发现问题在哪了。
load事件的绑定的是一个匿名函数,而当按钮按下时会再次绑定,所以会重复执行。于是将绑定bind改成one搞定。最终完整代码如下:
class="language-html"><!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>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>JQUERY动态加载图片</title>
<script type="text/javascript" src="class="tags" href="/tags/JQUERY.html" title=jquery>jquery/class="tags" href="/tags/JQUERY.html" title=jquery>jquery-1.3.2.min.js"></script>
<script type="text/javascript">
(class="tags" href="/tags/FUNCTION.html" title=function>function($){
$.NextPic=class="tags" href="/tags/FUNCTION.html" title=function>function()
{
$(".tip").slideDown(200);
$("img").attr("src",PicArr[CurrPic]).one('load',class="tags" href="/tags/FUNCTION.html" title=function>function(){$(".tip").slideUp(200);CurrPic++;if(CurrPic>4)CurrPic=0;});
//$("img").load(PicArr[CurrPic],class="tags" href="/tags/FUNCTION.html" title=function>function(){$(this).attr("src",PicArr[CurrPic]);$(".tip").slideUp(200);alert(CurrPic);CurrPic++;if(CurrPic>4)CurrPic=0;});
}
})(jQuery);
$(document).ready(class="tags" href="/tags/FUNCTION.html" title=function>function(){
PicArr = new Array("1.jpg","2.jpg","3.jpg","4.jpg","5.jpg");
CurrPic=0;
$(".tip").css({"position":"absolute","top":"100px","left":"50px"});
$(".tip").hide();
$(".scoll").click(class="tags" href="/tags/FUNCTION.html" title=function>function(){$.NextPic();});
})
</script>
</head>
<body>
<input class="picbtn" type="button" value="Next" />
<div class="tip">正在加载……</div>
<div class="notice">
<img id="img"/>
</div>
</body>
</html>