-
Notifications
You must be signed in to change notification settings - Fork 0
/
zim.js
93 lines (77 loc) · 1.81 KB
/
zim.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
var Zim = function(selector) {
var padding = 50
var p = {}
var w = window
var d = document
var zimClass = "zim"
var zimClassSelector = "." + zimClass
var clev = "click"
function scale(ow, oh, mw, mh){
var scale = Math.min(mw / ow, mh / oh);
if (scale > 1) scale = 1;
return {
width : ow * scale,
height : oh * scale
};
};
function loadImg(src,cb) {
var img = new Image();
img.onload = function() {
cb(img);
}
img.src = src
}
function dem(img) {
rb = targetSize(img.width,img.height)
img.style.width = rb.w + "px"
img.style.height = rb.h + "px"
img.style.top = rb.y + "px"
img.style.left = rb.x + "px"
img.style.position = "fixed"
img.classList.add(zimClass);
d.body.appendChild(img);
off(qS(selector),clev,zoomListener)
on([d],clev,rm)
}
function rm(e) {
e.preventDefault();
qS(zimClassSelector)[0].remove()
off([d],clev,rm)
on(qS(selector),clev,zoomListener)
}
function targetSize(iw,ih){
var vp = {}
vp.width = w.innerWidth
vp.height = w.innerHeight
var target = scale(iw, ih, vp.width - padding, vp.height - padding);
var left = (vp.width / 2) - (target.width / 2);
var top = (vp.height / 2) - (target.height / 2);
var res = {
x : left,
y: top,
w: target.width,
h: target.height
};
return res;
};
function zoomListener(e) {
e.preventDefault()
var el = this
src = el.href
loadImg(src, dem)
}
function on(nodes, event, fn) {
[].forEach.call(nodes, function(div) {
div.addEventListener(event, fn)
});
}
function off(nodes, event, fn) {
[].forEach.call(nodes, function(div) {
div.removeEventListener(event, fn)
});
}
function qS(s) {
return d.querySelectorAll(s)
}
on(qS(selector),clev,zoomListener)
}