-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathindex.js
163 lines (142 loc) · 5.01 KB
/
index.js
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
/* global document */
import offset from 'global-offset';
import insertAfter from 'insert-after';
import isPointerInside from 'is-pointer-inside';
export default class Magnifier {
props = {
height: 150,
width: 150,
backgroundColor: '#fff',
borderColor: '#eee',
borderRadius: 75,
borderWidth: 2
};
constructor(el) {
this.el = typeof el === 'string' ? document.querySelector(el) : el;
this.lens = document.createElement('div');
this.lens.className = 'magnifier';
this.lens.style.position = 'absolute';
this.lens.style.backgroundRepeat = 'no-repeat';
this.lens.style.borderStyle = 'solid';
this.lens.style.overflow = 'hidden';
this.lens.style.visibility = 'hidden';
this.lens.style.boxShadow = '0 1px 5px rgba(0, 0, 0, .25)';
this.handleLoad = this.handleLoad.bind(this);
this.handleTouchMove = this.handleTouchMove.bind(this);
this.handleTouchEnd = this.handleTouchEnd.bind(this);
Object.keys(this.props).forEach(prop => this.setStyle(prop, this.props[prop]));
insertAfter(this.lens, this.el);
this.show();
this.calcImageSize();
this.bind();
}
calcImageSize() {
const orig = document.createElement('img');
orig.style.position = 'absolute';
orig.style.width = 'auto';
orig.style.visibility = 'hidden';
orig.src = this.el.src;
orig.onload = this.handleLoad;
this.lens.appendChild(orig);
}
bind() {
this.el.addEventListener('mousemove', this.handleTouchMove, false);
this.el.addEventListener('mouseleave', this.handleTouchEnd, false);
this.el.addEventListener('touchstart', this.handleTouchMove, false);
this.el.addEventListener('touchmove', this.handleTouchMove, false);
this.el.addEventListener('touchend', this.handleTouchEnd, false);
this.lens.addEventListener('mousemove', this.handleTouchMove, false);
this.lens.addEventListener('mouseleave', this.handleTouchEnd, false);
this.lens.addEventListener('touchmove', this.handleTouchMove, false);
this.lens.addEventListener('touchend', this.handleTouchEnd, false);
return this;
}
unbind() {
this.el.removeEventListener('mousemove', this.handleTouchMove, false);
this.el.removeEventListener('mouseleave', this.handleTouchEnd, false);
this.el.removeEventListener('touchstart', this.handleTouchMove, false);
this.el.removeEventListener('touchmove', this.handleTouchMove, false);
this.el.removeEventListener('touchend', this.handleTouchEnd, false);
this.lens.removeEventListener('mousemove', this.handleTouchMove, false);
this.lens.removeEventListener('mouseleave', this.handleTouchEnd, false);
this.lens.removeEventListener('touchmove', this.handleTouchMove, false);
this.lens.removeEventListener('touchend', this.handleTouchEnd, false);
return this;
}
handleLoad() {
const orig = this.lens.getElementsByTagName('img')[0];
this.imageWidth = orig.offsetWidth;
this.imageHeight = orig.offsetHeight;
this.hide();
this.lens.style.visibility = 'visible';
this.lens.style.backgroundImage = `url(${this.el.src})`;
this.lens.removeChild(orig);
}
handleTouchMove(event) {
event.preventDefault();
const touch = event.type.indexOf('touch') === 0 ? event.changedTouches[0] : event;
if (isPointerInside(this.el, touch)) {
this.show();
const { pageX, pageY } = touch;
const { left, top } = offset(this.el);
const { offsetLeft, offsetTop, offsetWidth, offsetHeight } = this.el;
const { width, height, borderWidth } = this.props;
const ratioX = this.imageWidth / offsetWidth;
const ratioY = this.imageHeight / offsetHeight;
const imageX = ((left - pageX) * ratioX) + ((width / 2) - borderWidth);
const imageY = ((top - pageY) * ratioY) + ((height / 2) - borderWidth);
const x = pageX - (width / 2) - (left !== offsetLeft ? left - offsetLeft : 0);
const y = pageY - (height / 2) - (top !== offsetTop ? top - offsetTop : 0);
this.lens.style.left = `${x}px`;
this.lens.style.top = `${y}px`;
this.lens.style.backgroundPosition = `${imageX}px ${imageY}px`;
} else {
this.hide();
}
}
handleTouchEnd() {
this.hide();
}
height(n) {
return this.setProp('height', n);
}
width(n) {
return this.setProp('width', n);
}
backgroundColor(color) {
return this.setProp('backgroundColor', color);
}
borderColor(color) {
return this.setProp('borderColor', color);
}
borderRadius(n) {
return this.setProp('borderRadius', n);
}
borderWidth(n) {
return this.setProp('borderWidth', n);
}
setProp(prop, value) {
this.props[prop] = value;
this.setStyle(prop, value);
return this;
}
setStyle(prop, value) {
this.lens.style[prop] = typeof value === 'number' ? `${value}px` : value;
}
className(name) {
this.lens.className = name;
return this;
}
show() {
this.lens.style.display = 'block';
return this;
}
hide() {
this.lens.style.display = 'none';
return this;
}
destroy() {
this.unbind();
this.lens.remove();
}
}