-
Notifications
You must be signed in to change notification settings - Fork 12
/
image-comparison-slider.js
66 lines (58 loc) · 2.65 KB
/
image-comparison-slider.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
(function() {
var elsH = document.querySelectorAll(".image-spliter .mover");
var i = elsH.length;
while (i--) {
var moverWidth = elsH[i].getBoundingClientRect().width;
var imgLeft = elsH[i].nextElementSibling;
var width = imgLeft.getBoundingClientRect().width;
var height = imgLeft.getBoundingClientRect().height;
elsH[i].style.left = width / 2 - moverWidth / 2 + 'px';
//imgLeft.style.clip = "rect(0px, " + width / 2 + "px, " + height + "px, 0px)";
imgLeft.style.clip = "rect(0px, " + width / 2 + "px, 999px, 0px)";
var mouseDownX = 0;
var X;
elsH[i].addEventListener("mousedown", function(e) {
X = e.clientX;
mouseDownX = 1;
});
elsH[i].addEventListener("mouseup", function(e) {
mouseDownX = 0;
});
elsH[i].addEventListener("mouseout", function(e) {
mouseDownX = 0;
});
elsH[i].addEventListener("touchstart", function(e) {
X = e.touches[0].clientX;
mouseDownX = 1;
});
elsH[i].addEventListener("touchend", function(e) {
mouseDownX = 0;
});
elsH[i].addEventListener("mousemove", function(e) {
if (mouseDownX) {
this.style.left = parseInt(this.style.left) + (event.clientX - X) + "px";
X = event.clientX;
this.nextElementSibling.style.clip = "rect(0px, " + (this.getBoundingClientRect().width / 2 + parseInt(this.style.left)) + "px, " + this.getBoundingClientRect().height + "px, 0px)";
}
});
elsH[i].addEventListener("touchmove", function(e) {
if (mouseDownX) {
this.style.left = parseInt(this.style.left) + (e.touches[0].clientX - X) + "px";
X = e.touches[0].clientX;
this.nextElementSibling.style.clip = "rect(0px, " + (this.getBoundingClientRect().width / 2 + parseInt(this.style.left)) + "px, " + this.getBoundingClientRect().height + "px, 0px)";
}
});
}
window.addEventListener("resize", function(f) {
var elsHre = document.querySelectorAll(".image-spliter .mover");
var ii = elsHre.length;
while (ii--) {
var moverWidth = elsHre[ii].getBoundingClientRect().width;
var imgLeft = elsHre[ii].nextElementSibling;
var width = imgLeft.getBoundingClientRect().width;
var height = imgLeft.getBoundingClientRect().height;
elsHre[ii].style.left = width / 2 - moverWidth / 2 + 'px';
imgLeft.style.clip = "rect(0px, " + width / 2 + "px, " + height + "px, 0px)";
}
});
})();