基于touch.js手勢(shì)庫(kù)+zepto.js插件開(kāi)發(fā)圖片查看器(滑動(dòng)、縮放、雙擊縮放)
最近由于公司項(xiàng)目需要圖片查看器,網(wǎng)上搜了一圈,感覺(jué)資料很少,所以決定基于百度的touch.js手勢(shì)庫(kù)+zepto.js自己寫(xiě)了一個(gè)小插件,實(shí)現(xiàn)了左右滑動(dòng),雙指縮放,雙擊縮放功能,基本使用還行,但是有時(shí)候還是不太順暢,后續(xù)會(huì)慢慢完善;寫(xiě)的不好的地方望各位能夠給出好的建議,謝謝!
源碼地址: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)畫(huà)事件
//顯示大圖
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)聽(tīng),監(jiān)聽(tīng)查看大圖
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)畫(huà)事件
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)畫(huà)事件
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)畫(huà)事件
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)畫(huà)事件
this.style.webkitTransform = 'scale(' + currentScale + ')';
}else{
currentScale=1;
swipeSelf.swipeContainer.css("-webkit-transition","all ease 0.1s");//設(shè)置動(dòng)畫(huà)事件
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)聽(tīng)
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)聽(tīng)
mySwipe.listen("listenShow",function(){
alert("打開(kāi)大圖");
});
//關(guān)閉大圖監(jiān)聽(tīng)
mySwipe.listen("listenHide",function(){
alert("關(guān)閉大圖");
});
});
</script>
</body>
</html>
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
微信小程序HTTP接口請(qǐng)求封裝代碼實(shí)例
這篇文章主要介紹了微信小程序HTTP接口請(qǐng)求封裝代碼實(shí)例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-09-09
JavaScript中最常用的10種代碼簡(jiǎn)寫(xiě)技巧總結(jié)
這篇文章主要總結(jié)了JavaScript中最常用的10種代碼簡(jiǎn)寫(xiě)技巧的相關(guān)資料,其中包括三元操作符、短路求值簡(jiǎn)寫(xiě)方式、聲明變量簡(jiǎn)寫(xiě)方法、if存在條件簡(jiǎn)寫(xiě)方法及JavaScript循環(huán)簡(jiǎn)寫(xiě)方法等等,分別給出了詳細(xì)的示例代碼供大家參考,需要的朋友們下面來(lái)一起看看吧。2017-06-06
IE8 引入跨站數(shù)據(jù)獲取功能說(shuō)明
今天看了一下msdn文檔,發(fā)現(xiàn)IE8打算增加 XDomainRequest (http://msdn.microsoft.com/en-us/library/cc288060(VS.85).aspx) 跨站數(shù)據(jù)獲取的接口2008-07-07
JS閉包可被利用的常見(jiàn)場(chǎng)景小結(jié)
閉包的一個(gè)通常的用法是為一個(gè)在某一函數(shù)執(zhí)行前先執(zhí)行的函數(shù)提供參數(shù)。例如,在web環(huán)境中,一個(gè)函數(shù)作為setTimeout函數(shù)調(diào)用的第一個(gè)參數(shù),是一種很常見(jiàn)的應(yīng)用2017-04-04
JavaScript實(shí)現(xiàn)QueryString獲取GET參數(shù)的方法
本文為大家詳細(xì)介紹下如何通過(guò)JavaScript實(shí)現(xiàn)QueryString獲取GET參數(shù),具體實(shí)現(xiàn)如下,感興趣的朋友可以參考下哈,希望對(duì)大家有所幫助2013-07-07
JavaScript 閉包機(jī)制詳解及實(shí)例代碼
這篇文章主要介紹了JavaScript 閉包機(jī)制詳解及實(shí)例代碼的相關(guān)資料,需要的朋友可以參考下2016-10-10

