/*
data-ezoom="pc" 仅pc端有放大
data-ezoom="wap" 仅wap端有放大
data-ezoom="pc&wap"
*/
if ($('.e-zoom-v2').length > 0) {
function e_zoomFun($target) {
var e_zoomObj = {
init: function () {
this.win = $(window);
this.moduleObj = $target;
this.e_zoom_full = '
';
this.e_zoom_viewbox = '';
this.moduleObj.append(this.e_zoom_full);
this.clickEvent();
},
clickEvent: function () {
var _this = this;
// 获取对象
var viewBox = _this.moduleObj.find('.e-zoom-viewbox');
var closeFull = _this.moduleObj.find('.e-zoom-viewbox .close-full');
// 点击放大按钮,弹框
_this.moduleObj.find('.e-zoom-full').on('click', function () {
// 删除当前结构
$(".e-zoom-viewbox").remove();
// 添加到html结构
$("body").append(_this.e_zoom_viewbox);
_this.winw = _this.win.width();
if ($(this).parent('.e-zoom-v2').attr('data-ezoom') == 'pc' && _this.winw > 992) {
$(".e-zoom-viewbox").addClass('z_pc');
} else {
$(".e-zoom-viewbox").removeClass('z_pc');
};
var oImg = $(this).prev().find('img').attr('data-original');
$(".e-zoom-viewbox").find('img').attr('src', oImg).attr("style", "bottom: 0px");
$(".e-zoom-viewbox").fadeIn(400);
$('body').css('overflow', 'hidden');
_this.e_Zoom($(".e-zoom-viewbox").find('img'));
});
// 点击关闭按钮关闭弹框
$(document).on('click', '.close-full', function () {
$(".e-zoom-viewbox").fadeOut(400);
$('body').css('overflow', 'auto');
});
},
e_Zoom: function (option) {
var startX, endX, scale, x1, x2, y1, y2, imgLeft, imgTop, imgWidth, imgHeight, one = false,
$touch = option, //选择放大缩小的元素
originalWidth = $touch.width(),
originalHeight = $touch.height(),
baseScale = parseFloat(originalWidth / originalHeight),
imgData = [],
bgTop = parseInt(option.css('top'));
function siteData(name) {
imgLeft = parseInt(name.css('left'));
imgTop = parseInt(name.css('top'));
imgWidth = name.width();
imgHeight = name.height();
}
$(document).on('touchstart touchmove touchend', option, function (event) {
var $me = option,
touch1 = event.originalEvent.targetTouches[0], // 第一根手指touch事件
touch2 = event.originalEvent.targetTouches[1], // 第二根手指touch事件
fingers = event.originalEvent.touches.length; // 屏幕上手指数量
//手指放到屏幕上的时候,还没有进行其他操作
if (event.type == 'touchstart') {
if (fingers == 2) {
// 缩放图片的时候X坐标起始值
startX = Math.abs(touch1.pageX - touch2.pageX);
one = false;
} else if (fingers == 1) {
x1 = touch1.pageX;
y1 = touch1.pageY;
one = true;
}
//siteData($me);
}
//手指在屏幕上滑动
else if (event.type == 'touchmove') {
if (fingers == 2) {
// 缩放图片的时候X坐标滑动变化值
endX = Math.abs(touch1.pageX - touch2.pageX);
scale = endX - startX;
$me.css({
'width': originalWidth + scale,
//'height': originalWidth + scale //如果图片被拉伸了可以把这句去掉,让图片自适应
});
} else if (fingers == 1) {
x2 = touch1.pageX;
y2 = touch1.pageY;
if (one) {
$me.css({
'left': imgLeft + (x2 - x1),
'top': imgTop + (y2 - y1)
});
};
};
}
//手指移开屏幕
else if (event.type == 'touchend') {
// 手指移开后保存图片的宽
originalWidth = $touch.width(),
siteData($me);
imgData = [
[imgLeft, imgTop - bgTop, imgWidth, imgHeight],
[0, 0, 640, 640]
];
};
});
var getData = function () {
return imgData;
};
return {
imgData: getData
}
}
};
e_zoomObj.init();
}
// 初始化
$('.e-zoom-v2').each(function () {
// 防止js文件被执行2次。
var _this = $(this);
if (_this.data('init')) {
return;
}
_this.data('init', true);
e_zoomFun($(this));
})
}