-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
41 lines (34 loc) · 1.22 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
let slider = document.querySelector("input[type='range']");
let beforeImage = document.querySelector(".image-before");
let afterImage = document.querySelector(".image-after");
let beforeSelector = document.querySelector("#before-selector");
let afterSelector = document.querySelector("#after-selector");
let beforeImageWidth = getComputedStyle(beforeImage).width.slice(
0,
getComputedStyle(beforeImage).width.indexOf("p")
);
slider.addEventListener("input", (e) => {
adjustImage(e.target);
});
function adjustImage(target) {
beforeImage.style.width = `${(target.value / 100) * beforeImageWidth}px`;
}
function updateImage() {
if (beforeSelector.files && beforeSelector.files[0]) {
const reader = new FileReader();
reader.onload = function (e) {
// Display the selected image
beforeImage.style.background = `url("${e.target.result}")`;
};
reader.readAsDataURL(beforeSelector.files[0]);
}
if (afterSelector.files && afterSelector.files[0]) {
const reader = new FileReader();
reader.onload = function (e) {
// Display the selected image
afterImage.style.background = `url("${e.target.result}")`;
};
reader.readAsDataURL(afterSelector.files[0]);
}
}
adjustImage(slider);