纸上得来终觉浅,绝知此事要躬行
前言
之前放大镜效果都是用jquery的插件,具体实现也没认真想过,看到一个demo感觉不错,mark下来,原来地址猛戳
由项目需要,原生写了个详情页图片放大镜的效果,扔上代码供学习分享,也作为日常笔记…
效果如图(例子中偷偷链了张天猫的图片,希望没啥事 -。-):
实现过程教简单css
1 |
|
上面css中需要注意的就是几个position和缩放比例,注意调整下即可
来看看布局-html
1 |
|
主要的js代码
1 |
|
出来在平常开发中比较实用的知识
- Element.getBoundingClientRect()
Element.getBoundingClientRect()方法返回元素的大小及其相对于视口的位置 - event.target 和 event.currentTarget
- target:指向触发事件的元素
- currentTarget:指向被绑定事件句柄的元素,只有当绑定的事件处理程序与触发该事件处理程序都为同一个对象的时候,两者相同
- event.preventDefault() & event.stopPropagation()
- preventDefault:如果事件可取消,则取消该事件,而不停止事件的进一步传播
- stopPropagation:阻止捕获和冒泡阶段中当前事件的进一步传播
- event.stopPropagation() &event.stopImmediatePropagation()
- stopPropagation:阻止捕获和冒泡阶段中当前事件的进一步传播
- stopImmediatePropagation:阻止元素上调用相同事件的其他事件监听并阻止冒泡
虽然都是些简单的知识点,在平常开发中也是很实用的,希望能从细节出发,没事多复习复习 -。-~
后来一时兴起将放大镜写的更傻瓜式配置的插件了… 点我看代码(github地址)
imageZoom.js
最后我把作者封装的插件代码写在下面
只需要使用者写一个标签和写图片大小即可的了 0.0 , 只需要配置imgContainer的大小和必须的position:relative,然后写个div ,写上对应class和 图片地址data-src,然后js里调用下imageZoom就行了1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
(function(window){
function imageZoom(elem,options){
var settings = {
imgZoom : options && options.imgZoom || 2,
marginLeft: options && options.marginLeft || 0,
marginTop: options && options.marginTop || 0,
}
// 获取图片节点
var imgContainer = elem;
// 获取图片url
var imgUrl = imgContainer.getAttribute('data-src')
// you need to write your own RegExp to replace my RegExp . This is important !!!
// 用自己的正则代替这里的 因为对图片处理的参数不尽相同,比如我们公司就是?imageView/w/300/h/300
var originalUrl = imgUrl.replace(/\.(jpg|jpeg|png|gif)(_)(\d+)(x)(\d+)(q90)?/g,'');
// 获取图片节点位置
var elemClientRect = imgContainer.getBoundingClientRect();
// 生成小图html
var minImgHtml = '<img style="width:100%;height:100%" src="' + imgUrl + '" />';
if(!settings.marginLeft && !settings.marginTop){
settings.marginLeft = elemClientRect.width+10;
}
// 生成遮罩html
var glassWidth = elemClientRect.width/settings.imgZoom;
var glassHeight = elemClientRect.height/settings.imgZoom;
var glassStyle = 'width:' + glassWidth + 'px;height:' + glassHeight + 'px;'+'position:absolute;top: -9999px;left: -9999px;cursor: move;background: rgba(0,0,180,0.5);'
var glassHtml = '<div class="J_glass" style="' + glassStyle + '" ></div>';
// 生成大图
var maxImgStyle = 'position:absolute;width:' + (elemClientRect.width * settings.imgZoom) + 'px;height:' + (elemClientRect.height * settings.imgZoom) + 'px;';
var maxImgHtml = '<img class="J_maxImg" src="' + originalUrl + '" style="' + maxImgStyle + '" />';
// 生成大图容器
var maxContainerStyle = 'position:absolute;left:' + settings.marginLeft + 'px;top:'+ settings.marginTop +'px;width:' + elemClientRect.width + ';height:' + elemClientRect.height + ';overflow:hidden;display:none';
var maxContainerHtml = '<div class="J_imgMax" style="' + maxContainerStyle + '">' + maxImgHtml + '</div>';
var contentHtml = minImgHtml + glassHtml + maxContainerHtml;
imgContainer.innerHTML = contentHtml;
var maxImgContainer = imgContainer.querySelector('.J_imgMax');
var glassBlock = imgContainer.querySelector('.J_glass');
var maxImg = imgContainer.querySelector('.J_maxImg');
// 隐藏大图&遮罩
var hideMaxImg = function(){
glassBlock.style.top = '-9999px';
glassBlock.style.left = '-9999px';
maxImgContainer.style.display = 'none';
}
imgContainer.onmouseover = function(event){
event.stopPropagation();
maxImgContainer.style.display = 'block';
};
imgContainer.onmouseout = function(event){
event.stopPropagation();
hideMaxImg();
};
imgContainer.onmousemove = function(event) {
event.stopPropagation();
var clientRect = event.currentTarget.getBoundingClientRect();
// 获取左右上下坐标
var leftX = event.clientX - clientRect.left;
var leftY = event.clientY - clientRect.top;
// 动态设置遮罩范围值
var pointerLeft = leftX - glassWidth/2;
var pointerTop = leftY - glassHeight/2;
if((pointerLeft+glassWidth/2) > clientRect.width || pointerLeft < 0 - glassWidth/2 || (pointerTop+glassHeight/2) > clientRect.height || pointerTop < 0 - glassHeight/2){
hideMaxImg();
return !1;
};
if(pointerLeft < 0){
pointerLeft = 0;
};
if(pointerLeft > clientRect.width - glassWidth){
pointerLeft = clientRect.width - glassWidth;
};
if(pointerTop < 0){
pointerTop = 0;
};
if(pointerTop > clientRect.height - glassHeight){
pointerTop = clientRect.height - glassHeight;
};
glassBlock.style.left = pointerLeft;
glassBlock.style.top = pointerTop;
var percentLeft = pointerLeft/clientRect.width;
var percentHeight = pointerTop/clientRect.height;
maxImg.style.left = -(percentLeft*maxImg.clientWidth)+'px';
maxImg.style.top = -(percentHeight*maxImg.clientHeight)+'px';
};
}
window.imageZoom = imageZoom;//挂载到全局对象
})(window)