由於公司項目需要一個圖片查看器,上網搜了一圈發現可用資料不多,於是決定基於百度的 touch.js 手勢庫搭配 zepto.js 自己寫一個小插件,實現左右滑動、雙指縮放與雙擊縮放。目前基本可用,偶爾還是不夠順暢,後續會持續完善,寫得不好的地方歡迎指出。
源碼:https://github.com/GLwen/molong_photoSwipe.git
演示:https://runjs.cn/detail/iceaaogh
*{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;}
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, currentScale = 1;
var pinchSelf, 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 bigImgString = "";
bigImgLength = imgs.length;
bigImgOffset = -screenWidth * bigIndex;
for (var i = 0; i < bigImgLength; i++) {
var bigImgUrl = $(imgs[i]).attr("big-url");
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).width(screenWidth);
$(".img-div").height(screenHeight).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) {
if (swipeSelf.options.swipeRigth) {
swipeSelf.dx = 0; swipeSelf.dy = 0;
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) {
if (swipeSelf.options.swipeLeft) {
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) {
if (swipeSelf.options.pinch) {
pinchSelf = this;
currentScale = initialScale + (ev.scale - 1);
currentScale = currentScale > 2 ? 2 : currentScale;
currentScale = currentScale < 1 ? 1 : currentScale;
swipeSelf.swipeContainer.css("-webkit-transition", "all ease 0.1s");
this.style.webkitTransform = 'scale(' + currentScale + ')';
}
});
};
// 雙擊放大/還原
swipeSelf.doubletap = function () {
touch.on(swipeSelf.swipeContainer, 'doubletap', ".img-div", function (ev) {
pinchSelf = this;
currentScale = currentScale > 1 ? 2 : 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) {
dragSelf = this;
swipeSelf.options.swipeLeft = false;
swipeSelf.options.swipeRigth = false;
swipeSelf.dx = swipeSelf.dx || 0;
swipeSelf.dy = swipeSelf.dy || 0;
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) {
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; };
};
<!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="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>
<script src="js/zepto.min.js"></script>
<script src="js/touch.min.js"></script>
<script src="js/molong.js"></script>
<script>
$(function () {
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 () {
var addImg = '<li big-url="imges/4.jpg"><div class="img-bg" style="background-image:url(imges/4.jpg);"></div></li>';
mySwipe.listContainer.append(addImg);
mySwipe.listContainer.find(".img-bg").width(minImageWidth).height(minImageWidth);
});
mySwipe.listen("listenShow", function () { alert("打開大圖"); });
mySwipe.listen("listenHide", function () { alert("關閉大圖"); });
});
</script>
</body>
</html>
縮略圖與大圖的地址是分開存的:li 上的 big-url 放大圖,CSS 背景圖放小圖,這樣列表渲染時不必等待大圖下載。滑動與縮放衝突的解決方式是在 drag 判斷 currentScale > 1,並臨時關掉左右滑動開關,dragend 時再恢復。此外 touchstart 上調用 preventDefault 是避免頁面跟手滾動的關鍵。
待改進之處:目前用 -webkit-transform 硬切,未做邊界橡皮筋效果;縮放中心點固定為元素中心而非手指位置,體驗上還可以再打磨。
以上就是本文的全部內容,希望對大家的學習有所幫助。