JS实现简单的手机相册(从略缩图到大图)

1、先准备好animate.css文件和zetpo.js
2、结构层HTML代码:

<ul id="smallImg"></ul>
    <div class="mark"><!--遮罩层-->
        <img class="largeImg" src="" />
    </div>
</body>

3、样式层CSS,不要忘了链接animate

*{padding:0;margin:0;}
    ul{list-style:none;}
    ul li{float:left;}
    .mark{
        width: 100%;
        height: 100%;
        left: 0;
        top: 0;
        position: absolute;
        background-color: #000000;
        display: none;
    }

4、(重点来啦)行为层JavaScript代码,不要忘了链接zepto轻量级框架

    $(function(){
    //渲染页面
    var num=14,
        marginL=2,
        smallImg=$("#smallImg"),
        largeImg=$('.largeImg'),
        index=0;
    renderSmallImg();

    function renderSmallImg(){
        var str="";
        var m=4;//一行显示几张
        var winW=$(window).width();
        var smallImgW=(winW-m*marginL)/m;
        for(var i=1;i<=num;i++){
            str+="<li data-id='"+i+"' class='animated zoomInDown' style='margin-left:"+marginL+"px';>"+
            "<canvas data-id='"+i+"' id='cvs"+i+"' width='"+smallImgW+"' height='"+smallImgW+"'>此浏览器不支持canvas</canvas>"+
                 "</li>";                       
            var img=new Image();
            img.k=i;
            //加载图片
            img.onload=function(){
                //获取canvas元素
                //zepto转换为js(1、[0];2、get(0))
                var cvs=$("#cvs"+this.k).get(0);
                var ctx=cvs.getContext('2d');
                ctx.drawImage(this,0,0,smallImgW,smallImgW)
            }
            //图片的路径
            img.src="img/"+i+".jpg";
        }
        //把图片放到canvas里面
        smallImg.html(str);         
    }

    //窗口尺寸改变,重新渲染图片
    $(window).resize(function(){
        renderSmallImg()
    })

    smallImg.on('tap','li',function(){
        var id=$(this).data('id');
        index=id;
        renderBidImg(id);


    })
    //渲染大图函数
    function renderBidImg(id,callBack){
        //点击小图,显示遮罩层
        $('.mark').show();
        //获取大图片路径
        var largeImgSrc="img/"+id+".large.jpg";
        //实例化图片
        var imgL=new Image();
        //加载图片
        imgL.onload=function(){
            largeImg.attr('src',largeImgSrc);
            var winW=$(window).width();
            var winH=$(window).height();
            imgW=this.width;
            imgH=this.height;
            //图片的宽度大于高度时,水平显示
            if(imgW>imgH){
                bl=winW/imgW;
                largeImg.css({
                    'width':imgW*bl+'px',
                    'height':imgH*bl+'px',
                    'margin-top':(winH-imgH*bl)/2+'px',
                    'margin-left':'0'
                })
            }else{
                blH=winH/imgH;                      
                blW=winW/imgW;
                largeImg.css({
                    'width':imgW*blW+'px',
                    'height':imgH*blH+'px',
                    'margin-left':(winW-imgW*blW)/2+'px',
                    'margin-top':'0'
                })
            }
        }
        //图片路径
        imgL.src=largeImgSrc;
        if(callBack){
            callBack();
        }

    }
    //点击遮罩层,隐藏遮罩层
    $('.mark').tap(function(){
        $(this).hide();
    }).swipeLeft(function(){
        //向左滑动显示下一张
        index++;
        if(index>num){//显示到最后一张图片时,回到第一张
            index=1;
        }
        renderBidImg(index,function(){
            largeImg.addClass('animated bounceInRight').on('webkitAnimationEnd',function(){
                $(this).removeClass().off('webkitAnimationEnd');
            })
        });
    }).swipeRight(function(){
        //向左滑动显示上一张
        index--;
        if(index<1){//显示到第一张图片时,回到最后一张
            index=num;
        }
        renderBidImg(index,function(){
            largeImg.addClass('animated bounceInLeft').on('webkitAnimationEnd',function(){
                $(this).removeClass().off('webkitAnimationEnd');
            })
        });
    })
})
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
程序员给女朋友做的3D精美相册,让你震撼! #icon { background: rgb(255, 144, 0); border-radius: 6px 0px 0px; transition:0.6s ease-in-out; left: 65px; top: 6px; width: 40px; height: 40px; overflow: hidden; position: relative; cursor: pointer; -moz-transition: all 0.6s ease-in-out 0s; -webkit-transition: all 0.6s ease-in-out 0s; -o-transition: all 0.6s ease-in-out 0s; } #icon div { background: none; position: absolute; } #icon div:nth-child(1) { border-width: 7px 0px 7px 8px; border-style: solid; border-color: transparent rgb(255, 255, 255); left: 20px; top: 50%; width: 0px; height: 0px; margin-top: -7px; position: absolute; } #icon div:nth-child(2) { background: rgb(255, 255, 255); left: 12px; top: 50%; width: 8px; height: 6px; margin-top: -3px; position: absolute; } #nav { top: 10px; width: 100px; height: 0px; position: absolute; -ms-user-select: none; -webkit-user-select: none; -moz-user-select: none; } #nav-switch { display: none; } #nav .label { display: block; cursor: pointer; } #nav .container { transition:left 0.3s ease-in-out; left: -100px; width: 100%; position: absolute; -moz-transition: left 0.3s ease-in-out 0s; -webkit-transition: left 0.3s ease-in-out 0s; -o-transition: left 0.3s ease-in-out 0s; } #nav .container > div { padding: 0px; width: 50%; float: left; } #nav .container .nav-on { color: rgb(51, 51, 51); padding-left: 0px; } #nav .container .nav-off { width: 40px; height: 40px; padding-right: 10px; } :checked#nav-switch + .label .container { left: 10px; } :checked#nav-switch + .label #icon { background: rgb(0, 101, 203); border-radius: 0px 0px 6px; transform: rotate(-180deg); -webkit-transform: rotate(-180deg); -moz-transform: rotate(-180deg); -o-transform: rotate(-180deg); } .title { margin: 0px; padding: 0px; left: 170px; top: 2px; height: 40px; color: rgb(51, 51, 51); line-height: 40px; font-family: "Segoe UI Light", "Segoe UI", Frutiger, "Frutiger Linotype", "Dejavu Sans", "Helvetica Neue", Arial, sans-serif; font-size: 24px; font-weight: bold; white-

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值