基于touch.js手势库+zepto.js插件开发图片查看器(滑动、缩放、双击缩放)

 更新时间:2016年11月17日 15:34:58   作者:菜鸟阿文  
这篇文章主要为大家详细介绍了touch.js手势库结合zepto.js插件开发图片查看器,图片可以实现滑动、缩放、双击缩放等效果,

最近由于公司项目需要图片查看器,网上搜了一圈,感觉资料很少,所以决定基于百度的touch.js手势库+zepto.js自己写了一个小插件,实现了左右滑动,双指缩放,双击缩放功能,基本使用还行,但是有时候还是不太顺畅,后续会慢慢完善;写的不好的地方望各位能够给出好的建议,谢谢!

源码地址:https://github.com/GLwen/molong_photoSwipe.git

演示:http://runjs.cn/detail/iceaaogh

molong.css

*{padding:0;margin: 0;list-style: none;}
.syswin-swipe-show{display: block;}
.syswin-swipe-hide{display: none;}
/***大图****/
.molong-swiper{ position: fixed; top:0; left: 0; border: 1px solid #777e8c; overflow: hidden; z-index: 999; }
.molong-swiper-item{ float: left; overflow: scroll; background: #333333; text-align: center; }
.molong-swiper-item .img-div{ background-size: contain; background-position: center; background-repeat: no-repeat; }

.molong-img-list { list-style: none; padding: 0; margin: 0;}
.molong-img-list li { float: left; position: relative;margin-right: 10px;}
.molong-img-list li .img-bg { display: block; width: 100%; height: 100%;border: none;background-size:cover;background-position: center;background-repeat: no-repeat;}

molong.js

var molong=molong?molong:{};
molong.photoSwipe=function(options){
  //给大图查看器添加一个独立的容器
  var bigContainerString="<div class=\"molong-swiper syswin-swipe-hide\">"+
    "<ul id=\"bigImg\"></ul>"+
    "</div>";
  $("body").append(bigContainerString);
  var swipeSelf=this;
  var screenHeight=window.innerHeight;
  var screenWidth=window.innerWidth;
  var minImageWidth=screenWidth*0.25;//显示小图的宽高
  var bigIndex=0;     //大图索引
  var bigImgOffset=0;    //大图滑动的位置
  var bigImgLength=0;  //大图数量
  //缩放设置
  var initialScale = 1;  //初始缩放比例
  var currentScale=1;   //当前缩放比例
  var pinchSelf;     //当前缩放比例的对象
  var dragSelf;     //当前拖拽的对象
  //解析参数
  swipeSelf.options=$.extend({
    listContainer:$("ul"),
    swipeRigth:true,
    swipeLeft:true,
    pinch:true
  },options);
  //容器
  swipeSelf.listContainer=options.listContainer; //小图容器

  swipeSelf.swipeContainer=$("#bigImg"); //大图容器
  //阻止touchstart默认事件
  touch.on(this.swipeContainer, 'touchstart', function(ev){
    ev.preventDefault();
  });
  swipeSelf.swipeContainer.css("-webkit-transition","all ease 0.3s");//设置动画事件
  //显示大图
  swipeSelf.showBigImg=function(){
    var imgs=swipeSelf.listContainer.find("li");
    var bigImgsUrl=[];
    var bigImgString="";
    bigImgLength=imgs.length;
    bigImgOffset=-screenWidth*bigIndex;
    for(var i=0;i<bigImgLength;i++){
      var bigImgUrl=$(imgs[i]).attr("big-url");
      bigImgsUrl.push(bigImgUrl);
      bigImgString+='<li class="molong-swiper-item"><div class="img-div" style="background-image: url('+bigImgUrl+')"></div></li>';
    }
    swipeSelf.swipeContainer.html(bigImgString);
    swipeSelf.swipeContainer.height(screenHeight);
    swipeSelf.swipeContainer.width(screenWidth*bigImgLength);
    $(".molong-swiper-item").height(screenHeight);
    $(".molong-swiper-item").width(screenWidth);
    $(".img-div").height(screenHeight);
    $(".img-div").width(screenWidth);
    swipeSelf.swipeContainer.css("-webkit-transform","translate3d("+bigImgOffset+"px,0,0)");
    $(".molong-swiper").show();
    //添加事件监听,监听查看大图
    if(swipeSelf.listenShow){
      swipeSelf.listenShow();
    }
  }
  //隐藏大图
  swipeSelf.hideBigImg=function() {
    $(".molong-swiper").hide();
    swipeSelf.swipeContainer.html("");
    if(swipeSelf.listenHide){
      swipeSelf.listenHide();
    }
  }
  //右滑动
  swipeSelf.swipeRight=function(){
    touch.on(swipeSelf.swipeContainer, 'swiperight',"li", function(ev){
      console.log("swiperight");
      if(swipeSelf.options.swipeRigth){
        //$(".img-div").css("-webkit-transform","translate3d(0px, 0px, 0px)");//元素移动复位
        swipeSelf.dx=0;
        swipeSelf.dy=0;
        console.log("向右滑动.");
        if(pinchSelf){
          pinchSelf.style.webkitTransform = 'scale(1)';
          currentScale=1;
        }
        bigImgOffset+=screenWidth;
        bigImgOffset=bigImgOffset>=0?0:bigImgOffset;
        swipeSelf.swipeContainer.css("-webkit-transition","all ease 0.5s");//设置动画事件
        swipeSelf.swipeContainer.css("-webkit-transform","translate3d("+bigImgOffset+"px,0,0)");
      }
    });
  }
  //左滑动
  swipeSelf.swipeLeft=function(){
    touch.on(swipeSelf.swipeContainer, 'swipeleft','li', function(ev){
      console.log("swipeleft");
      if(swipeSelf.options.swipeLeft){
        console.log("向左滑动.");
        // $(".img-div").css("-webkit-transform","translate3d(0px, 0px, 0px)");//元素移动复位
        swipeSelf.dx=0;
        swipeSelf.dy=0;
        if(pinchSelf){
          pinchSelf.style.webkitTransform = 'scale(1)';
          currentScale=1;
        }
        bigImgOffset-=screenWidth;
        bigImgOffset=Math.abs(bigImgOffset)>=(screenWidth*bigImgLength)?(-screenWidth*(bigImgLength-1)):bigImgOffset;
        swipeSelf.swipeContainer.css("-webkit-transition","all ease 0.5s");//设置动画事件
        swipeSelf.swipeContainer.css("-webkit-transform","translate3d("+bigImgOffset+"px,0,0)");
      }
    });
  }
  //缩放
  swipeSelf.pinche=function(){
    touch.on(swipeSelf.swipeContainer, 'pinchend',".img-div", function(ev){
      console.log("pinchend");
      if(swipeSelf.options.pinch){
        pinchSelf=this;
        currentScale = ev.scale - 1;
        currentScale = initialScale + currentScale;
        currentScale = currentScale > 2 ? 2 : currentScale;
        currentScale = currentScale < 1 ? 1 : currentScale;
        swipeSelf.swipeContainer.css("-webkit-transition","all ease 0.1s");//设置动画事件
        this.style.webkitTransform = 'scale(' + currentScale + ')';
        console.log("当前缩放比例为:" + currentScale + ".");
      }
    });
  }
  //双击放大缩小
  swipeSelf.doubletap=function(){
    touch.on(swipeSelf.swipeContainer, 'doubletap','.img-div', function(ev){
      //console.log(ev.type);
      pinchSelf=this;
      currentScale=currentScale>1?2:1;
      if(currentScale==1){
        currentScale=2;
        swipeSelf.swipeContainer.css("-webkit-transition","all ease 0.1s");//设置动画事件
        this.style.webkitTransform = 'scale(' + currentScale + ')';
      }else{
        currentScale=1;
        swipeSelf.swipeContainer.css("-webkit-transition","all ease 0.1s");//设置动画事件
        this.style.webkitTransform = 'scale(' + currentScale + ')';
      }
    });
  }
  //拖拽
  swipeSelf.dx=0;
  swipeSelf.dy=0;
  swipeSelf.drag=function(){
    touch.on(swipeSelf.swipeContainer, 'drag','.img-div', function(ev){
      if(currentScale>1){
        console.log("drag");
        dragSelf=this;
        swipeSelf.options.swipeLeft=false;
        swipeSelf.options.swipeRigth=false;
        swipeSelf.dx = swipeSelf.dx || 0;
        swipeSelf.dy = swipeSelf.dy || 0;
        console.log("当前x值为:" + swipeSelf.dx + ", 当前y值为:" + swipeSelf.dy +".");
        var offx = swipeSelf.dx + ev.x + "px";
        var offy = swipeSelf.dy + ev.y + "px";
        this.style.webkitTransform = "translate3d(" + offx + "," + offy + ",0)"+" scale(" +currentScale +")";
      }
    });
    touch.on(swipeSelf.swipeContainer, 'dragend','.img-div', function(ev){
      console.log("dragend");
      swipeSelf.dx += ev.x;
      swipeSelf.dy += ev.y;
      swipeSelf.options.swipeLeft=true;
      swipeSelf.options.swipeRigth=true;
    });
  }
  //触发,查看大图
  swipeSelf.init=function(){
    //设置小图
    swipeSelf.listContainer.find(".img-bg").width(minImageWidth);
    swipeSelf.listContainer.find(".img-bg").height(minImageWidth);
    //添加绑定查看大图事件
    swipeSelf.listContainer.on("tap","li",function(){
      bigIndex=$(this).index();
      swipeSelf.showBigImg();
    });
    swipeSelf.swipeRight();//右滑动
    swipeSelf.swipeLeft();//左滑动
    swipeSelf.pinche();//缩放
    swipeSelf.drag();//拖拽
    swipeSelf.doubletap();//双击放大缩小
  }
  //事件监听
  swipeSelf.listen=function(type,callback){
    swipeSelf[type]=callback;
  }
}

index.html

<!DOCTYPE html>
<html>
<head lang="en">
  <meta charset="UTF-8">
  <title>图片查看器</title>
  <meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no"/>
  <meta name="apple-mobile-web-app-capable" content="yes" />
  <meta name="apple-mobile-web-app-status-bar-style" content="black" />
  <meta name="format-detection" content="telephone=no,email=no,adress=no">
  <link rel="stylesheet" href="css/molong.css">
</head>
<body>
<ul id="imgList" class="molong-img-list">
  <li big-url="imges/1.jpg"><div class='img-bg' style="background-image:url(imges/1.jpg);"></div></li>
  <li big-url="imges/2.jpg"><div class='img-bg' style="background-image:url(imges/2.jpg);"></div></li>
  <li big-url="imges/3.jpg"><div class='img-bg' style="background-image:url(imges/3.jpg);"></div></li>
</ul>
<ul style="position: absolute;top:300px;left:0;z-index: 9999999;">
  <li><input style="height: 40px;width: 90px;text-align: center;" type="button" value="addImg" id="addBtn"/></li>
  <li style="margin-top: 30px;"><input style="height: 40px;width: 90px;text-align: center;" type="button" value="closeImg" id="addClose"/></li>
</ul>
<script src="js/zepto.min.js"></script>
<script src="js/touch.min.js"></script>
<script src="js/molong.js"></script>
<script>
  $(function(){
    //添加大图容器
    var screenHeight=window.innerHeight;
    var screenWidth=window.innerWidth;
    var minImageWidth=screenWidth*0.25;//显示小图的宽高
    var mySwipe=new molong.photoSwipe({listContainer:$("#imgList")});
    mySwipe.init();
    //关闭图片查看器
    $("#addClose").on("tap",function(){
      mySwipe.hideBigImg();
    });
    $("#addBtn").on("click",function(){
      console.log(this);
      var addImg1='<li big-url="imges/4.jpg"><div class="img-bg" style="background-image:url(imges/4.jpg);"></div></li>';
      mySwipe.listContainer.append(addImg1);
      mySwipe.listContainer.find(".img-bg").width(minImageWidth);
      mySwipe.listContainer.find(".img-bg").height(minImageWidth);
    })
    //显示大图监听
    mySwipe.listen("listenShow",function(){
      alert("打开大图");
    });
    //关闭大图监听
    mySwipe.listen("listenHide",function(){
      alert("关闭大图");
    });
  });
</script>
</body>
</html>

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

相关文章

  • js获取当前年月日时分秒的方法实例(new Date()/moment.js)

    js获取当前年月日时分秒的方法实例(new Date()/moment.js)

    JavaScript是一种流行的编程语言,它可以用来获取当前年月日,这篇文章主要给大家介绍了关于js获取当前年月日时分秒的相关资料,分别使用的是new Date()/moment.js,需要的朋友可以参考下
    2024-07-07
  • js常用DOM方法详解

    js常用DOM方法详解

    本文主要介绍了js常用的DOM方法,具有很好的参考价值,下面跟着小编一起来看下吧
    2017-02-02
  • 解决微信小程序防止无法回到主页的问题

    解决微信小程序防止无法回到主页的问题

    这篇文章主要介绍了解决微信小程序防止无法回到主页的问题,非常不错,具有一定的参考借鉴价值,需要的朋友可以参考下
    2018-09-09
  • js实现电梯导航效果的示例代码

    js实现电梯导航效果的示例代码

    这篇文章主要介绍了JavaScript实现电梯导航效果的相关知识,文中通过示例代码介绍的很详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习吧
    2023-12-12
  • javascript实现延时显示提示框特效代码

    javascript实现延时显示提示框特效代码

    本文给大家分享的是javascript通过setTimeout实现延时显示提示框的特效代码,效果非常棒,这里推荐给大家
    2016-04-04
  • 利用JavaScript编写一个简单的1024小游戏

    利用JavaScript编写一个简单的1024小游戏

    在每年的10月24日,我们都会庆祝程序员节,这是一个向所有辛勤工作、创造出无数令人惊叹应用和系统的程序员们致敬的日子,为了纪念这个特殊的日子,我们将通过编写一个简单的1024小游戏来向所有程序员们表示敬意,本文将详细解释如何使用JavaScript编写这个小游戏
    2023-10-10
  • 微信企业号开发之微信考勤百度地图定位

    微信企业号开发之微信考勤百度地图定位

    本文给大家介绍微信企业号开发之微信考勤百度地图定位,有需要的朋友参考下本篇文章
    2015-09-09
  • JS中Location使用详解

    JS中Location使用详解

    javascript中 location用于获取或设置窗体的URL,并且可以用于解析URL,是BOM中最重要的对象之一,下面我们就来详细探讨下Location对象的使用。
    2015-05-05
  • 如何在uniapp项目中使用mqtt

    如何在uniapp项目中使用mqtt

    本文给大家分享uniapp中引入mqtt的全过程,在uniapp项目根目录下分别运行安装mqtt和uuid的命令行,因为后面会用uuid生成mqtt的clientId,文中给大家介绍mqtt和uuid安装过程,感兴趣的朋友跟随小编一起学习下吧
    2021-05-05
  • iview date-picker options只可选当前日期之前的(当前之后的禁用)

    iview date-picker options只可选当前日期之前的(当前之后的禁用)

    如果日期对象date的时间戳大于当前时间的时间戳,则该日期在当前日期之后,会被禁用,下面通过实例代码给大家介绍iview date-picker options只可选当前日期之前的,感兴趣的朋友跟随小编一起看看吧
    2024-12-12

最新评论