最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

基于touch.js手勢(shì)庫+zepto.js插件開發(fā)圖片查看器(滑動(dòng)、縮放、雙擊縮放)

 更新時(shí)間:2016年11月17日 15:34:58   作者:菜鳥阿文  
這篇文章主要為大家詳細(xì)介紹了touch.js手勢(shì)庫結(jié)合zepto.js插件開發(fā)圖片查看器,圖片可以實(shí)現(xiàn)滑動(dòng)、縮放、雙擊縮放等效果,

最近由于公司項(xiàng)目需要圖片查看器,網(wǎng)上搜了一圈,感覺資料很少,所以決定基于百度的touch.js手勢(shì)庫+zepto.js自己寫了一個(gè)小插件,實(shí)現(xiàn)了左右滑動(dòng),雙指縮放,雙擊縮放功能,基本使用還行,但是有時(shí)候還是不太順暢,后續(xù)會(huì)慢慢完善;寫的不好的地方望各位能夠給出好的建議,謝謝!

源碼地址: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){
  //給大圖查看器添加一個(gè)獨(dú)立的容器
  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;    //大圖滑動(dòng)的位置
  var bigImgLength=0;  //大圖數(shù)量
  //縮放設(shè)置
  var initialScale = 1;  //初始縮放比例
  var currentScale=1;   //當(dāng)前縮放比例
  var pinchSelf;     //當(dāng)前縮放比例的對(duì)象
  var dragSelf;     //當(dāng)前拖拽的對(duì)象
  //解析參數(shù)
  swipeSelf.options=$.extend({
    listContainer:$("ul"),
    swipeRigth:true,
    swipeLeft:true,
    pinch:true
  },options);
  //容器
  swipeSelf.listContainer=options.listContainer; //小圖容器

  swipeSelf.swipeContainer=$("#bigImg"); //大圖容器
  //阻止touchstart默認(rèn)事件
  touch.on(this.swipeContainer, 'touchstart', function(ev){
    ev.preventDefault();
  });
  swipeSelf.swipeContainer.css("-webkit-transition","all ease 0.3s");//設(shè)置動(dòng)畫事件
  //顯示大圖
  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();
    //添加事件監(jiān)聽,監(jiān)聽查看大圖
    if(swipeSelf.listenShow){
      swipeSelf.listenShow();
    }
  }
  //隱藏大圖
  swipeSelf.hideBigImg=function() {
    $(".molong-swiper").hide();
    swipeSelf.swipeContainer.html("");
    if(swipeSelf.listenHide){
      swipeSelf.listenHide();
    }
  }
  //右滑動(dòng)
  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)");//元素移動(dòng)復(fù)位
        swipeSelf.dx=0;
        swipeSelf.dy=0;
        console.log("向右滑動(dòng).");
        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");//設(shè)置動(dòng)畫事件
        swipeSelf.swipeContainer.css("-webkit-transform","translate3d("+bigImgOffset+"px,0,0)");
      }
    });
  }
  //左滑動(dòng)
  swipeSelf.swipeLeft=function(){
    touch.on(swipeSelf.swipeContainer, 'swipeleft','li', function(ev){
      console.log("swipeleft");
      if(swipeSelf.options.swipeLeft){
        console.log("向左滑動(dòng).");
        // $(".img-div").css("-webkit-transform","translate3d(0px, 0px, 0px)");//元素移動(dòng)復(fù)位
        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");//設(shè)置動(dòng)畫事件
        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");//設(shè)置動(dòng)畫事件
        this.style.webkitTransform = 'scale(' + currentScale + ')';
        console.log("當(dāng)前縮放比例為:" + 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");//設(shè)置動(dòng)畫事件
        this.style.webkitTransform = 'scale(' + currentScale + ')';
      }else{
        currentScale=1;
        swipeSelf.swipeContainer.css("-webkit-transition","all ease 0.1s");//設(shè)置動(dòng)畫事件
        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("當(dāng)前x值為:" + swipeSelf.dx + ", 當(dāng)前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;
    });
  }
  //觸發(fā),查看大圖
  swipeSelf.init=function(){
    //設(shè)置小圖
    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();//右滑動(dòng)
    swipeSelf.swipeLeft();//左滑動(dòng)
    swipeSelf.pinche();//縮放
    swipeSelf.drag();//拖拽
    swipeSelf.doubletap();//雙擊放大縮小
  }
  //事件監(jiān)聽
  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();
    //關(guān)閉圖片查看器
    $("#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);
    })
    //顯示大圖監(jiān)聽
    mySwipe.listen("listenShow",function(){
      alert("打開大圖");
    });
    //關(guān)閉大圖監(jiān)聽
    mySwipe.listen("listenHide",function(){
      alert("關(guān)閉大圖");
    });
  });
</script>
</body>
</html>

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • js獲取當(dāng)前年月日時(shí)分秒的方法實(shí)例(new?Date()/moment.js)

    js獲取當(dāng)前年月日時(shí)分秒的方法實(shí)例(new?Date()/moment.js)

    JavaScript是一種流行的編程語言,它可以用來獲取當(dāng)前年月日,這篇文章主要給大家介紹了關(guān)于js獲取當(dāng)前年月日時(shí)分秒的相關(guān)資料,分別使用的是new?Date()/moment.js,需要的朋友可以參考下
    2024-07-07
  • js常用DOM方法詳解

    js常用DOM方法詳解

    本文主要介紹了js常用的DOM方法,具有很好的參考價(jià)值,下面跟著小編一起來看下吧
    2017-02-02
  • 解決微信小程序防止無法回到主頁的問題

    解決微信小程序防止無法回到主頁的問題

    這篇文章主要介紹了解決微信小程序防止無法回到主頁的問題,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2018-09-09
  • js實(shí)現(xiàn)電梯導(dǎo)航效果的示例代碼

    js實(shí)現(xiàn)電梯導(dǎo)航效果的示例代碼

    這篇文章主要介紹了JavaScript實(shí)現(xiàn)電梯導(dǎo)航效果的相關(guān)知識(shí),文中通過示例代碼介紹的很詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧
    2023-12-12
  • javascript實(shí)現(xiàn)延時(shí)顯示提示框特效代碼

    javascript實(shí)現(xiàn)延時(shí)顯示提示框特效代碼

    本文給大家分享的是javascript通過setTimeout實(shí)現(xiàn)延時(shí)顯示提示框的特效代碼,效果非常棒,這里推薦給大家
    2016-04-04
  • 利用JavaScript編寫一個(gè)簡(jiǎn)單的1024小游戲

    利用JavaScript編寫一個(gè)簡(jiǎn)單的1024小游戲

    在每年的10月24日,我們都會(huì)慶祝程序員節(jié),這是一個(gè)向所有辛勤工作、創(chuàng)造出無數(shù)令人驚嘆應(yīng)用和系統(tǒng)的程序員們致敬的日子,為了紀(jì)念這個(gè)特殊的日子,我們將通過編寫一個(gè)簡(jiǎn)單的1024小游戲來向所有程序員們表示敬意,本文將詳細(xì)解釋如何使用JavaScript編寫這個(gè)小游戲
    2023-10-10
  • 微信企業(yè)號(hào)開發(fā)之微信考勤百度地圖定位

    微信企業(yè)號(hào)開發(fā)之微信考勤百度地圖定位

    本文給大家介紹微信企業(yè)號(hào)開發(fā)之微信考勤百度地圖定位,有需要的朋友參考下本篇文章
    2015-09-09
  • JS中Location使用詳解

    JS中Location使用詳解

    javascript中 location用于獲取或設(shè)置窗體的URL,并且可以用于解析URL,是BOM中最重要的對(duì)象之一,下面我們就來詳細(xì)探討下Location對(duì)象的使用。
    2015-05-05
  • 如何在uniapp項(xiàng)目中使用mqtt

    如何在uniapp項(xiàng)目中使用mqtt

    本文給大家分享uniapp中引入mqtt的全過程,在uniapp項(xiàng)目根目錄下分別運(yùn)行安裝mqtt和uuid的命令行,因?yàn)楹竺鏁?huì)用uuid生成mqtt的clientId,文中給大家介紹mqtt和uuid安裝過程,感興趣的朋友跟隨小編一起學(xué)習(xí)下吧
    2021-05-05
  • iview?date-picker?options只可選當(dāng)前日期之前的(當(dāng)前之后的禁用)

    iview?date-picker?options只可選當(dāng)前日期之前的(當(dāng)前之后的禁用)

    如果日期對(duì)象date的時(shí)間戳大于當(dāng)前時(shí)間的時(shí)間戳,則該日期在當(dāng)前日期之后,會(huì)被禁用,下面通過實(shí)例代碼給大家介紹iview?date-picker?options只可選當(dāng)前日期之前的,感興趣的朋友跟隨小編一起看看吧
    2024-12-12

最新評(píng)論

栖霞市| 德化县| 长沙县| 文水县| 镇平县| 旬阳县| 万全县| 望奎县| 双桥区| 兴义市| 康马县| 巴南区| 三河市| 沙湾县| 满城县| 东辽县| 东港市| 镇宁| 宁陕县| 琼结县| 乌海市| 景泰县| 甘泉县| 禄丰县| 仙桃市| 新民市| 加查县| 连云港市| 牙克石市| 疏附县| 建德市| 仁布县| 牙克石市| 扎囊县| 武强县| 邓州市| 连南| 靖宇县| 韶关市| 晋宁县| 博乐市|