基于zepto.js实现手机相册功能
程序员文章站
2024-01-30 14:39:52
看完老师的视频做的一个手机相册,对我这种菜鸟来说还是直接上代码吧!里面用到17张小图,17张大图,还有animate.css和zepto.min.js ,都可以在网上找到。...
看完老师的视频做的一个手机相册,对我这种菜鸟来说还是直接上代码吧!里面用到17张小图,17张大图,还有animate.css和zepto.min.js ,都可以在网上找到。
<!doctype html> <html lang="zh-cn"> <head> <meta charset="utf-8"> <!-- <meta name="viewport" content="width=device-width,initial-scale=1.0,user-scalable=no"/> --> <meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1.0"/> <title>photo</title> <link rel="stylesheet" href="animate.css"/> <script src="zepto.min.js"></script> <style type="text/css"> blockquote,body,button,dd,dl,dt,fieldset,form,h1,h2,h3,h4,h5,h6,hr,input,legend,li,ol,p,pre,td,textarea,ul,li,img{ margin: 0; padding: 0; } .clearfix::before,.clearfix::after{ content:""; height: 0; line-height: 0; display: block; visibility: hidden; clear: both; } body{ background-color: black;overflow: hidden; } ul{ list-style: none; } .container li{ float: left; overflow: hidden; } .large{ height: 100%; width: 100%; position: absolute; left: 0; top: 0; background-color: black; } </style> </head> <body> <ul class="container clearfix" id="container"> </ul> <div class="large animated fadeindown" id="large_container" style="display: none;"> <img id="large_img" /> </div> <script> var num=17; var zwin=$(window); var render=function(){ var padding=2; var winwidth=zwin.width(); var picwidth=math.floor((winwidth-padding*3)/4); var tmpl=""; for (var i = 1; i <=num; i++) { var p = padding; var imgsrc='img/'+i+'.jpg'; if (i%4==1) { p=0; } tmpl+='<li class="animated bouncein" data-id="'+i+'" style=" width:'+picwidth+'px;height:'+picwidth+'px;padding-left:'+p+'px;padding-top:'+padding+'px "><canvas id="cvs_'+i+'"></canvas></li>' var imageobj=new image(); imageobj.index=i; imageobj.onload=function () { var cvs = $('#cvs_'+this.index)[0].getcontext('2d'); cvs.width=this.width; cvs.height=this.height; cvs.drawimage(this,0,0); } imageobj.src=imgsrc; } $("#container").html(tmpl); } render(); var wimage = $('#large_img'); var domimage = wimage[0]; //image对象的dom应用 var loadimg = function (id,callback) { $('#container').css({height:zwin.height(),'overflow':'hidden'}) // $('#container').css({'display':'none'}) $('#large_container').css({ width:zwin.width(), height:zwin.height() }).show(); //加载大图 var imgsrc = 'img/'+id+'.large.jpg'; var imageobj = new image(); imageobj.onload = function () { var w = this.width; //图片的宽高 var h = this.height; var winwidth = zwin.width(); //window的宽高 var winhidth = zwin.height(); var realw = winhidth*w/h; var paddingleft = parseint((winwidth - realw)/2); var realh = winwidth*h/w; var paddingtop = parseint((winhidth - realh)/2); //横图和竖图切换时需要重置大图的css样式 wimage.css('width','auto').css('height','auto'); wimage.css('padding-left','0px').css('padding-top','0px'); //计算图片的宽高比,判断是横图还是竖图 if (h/w>1.2) { //图片显示出来 wimage.attr('src',imgsrc).css('height',winhidth).css('padding-left',paddingleft) }else{ wimage.attr('src',imgsrc).css('width',winwidth).css('padding-top',paddingtop) } callback&&callback(); } imageobj.src = imgsrc; } var cid; //给li做事件绑定; $('#container').delegate('li','tap',function(){ var _id = cid = $(this).attr('data-id') loadimg(_id) }); //点击大图,返回相册 $('#large_container').tap(function() { $('#container').css({height:'auto','overflow':'auto'}) // $('#container').css({'display':'block'}) $(this).hide(); }).swipeleft(function(){ cid++; if(cid>num){ cid = num; }else{ loadimg(cid,function(){ //事件监听 domimage.addeventlistener('webkitanimationend',function(){ //动画结束之后执行函数 wimage.removeclass('animated bounceinright'); domimage.removeeventlistener('webkitanimationend') },false) //控制事件是否冒泡用false参数 wimage.addclass('animated bounceinright'); }); } }).swiperight(function(){ cid--; if(cid<1){ cid = 1; }else{ loadimg(cid,function(){ domimage.addeventlistener('webkitanimationend',function(){ wimage.removeclass('animated bounceinleft'); domimage.removeeventlistener('webkitanimationend') },false) wimage.addclass('animated bounceinleft'); }); } }) </script> </body> </html>
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
上一篇: 微软披露SQL Server 2008 R2预览版
下一篇: 教你如何利用PS制作逼真的公章