-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathresizeable.func.js
49 lines (44 loc) · 1.2 KB
/
resizeable.func.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
//This is a resizeable script, used to make responsive image
//Just change the id variable in image
let ratioSize = [
{
"ratio":"4:3",
"size": 3
},
{
"ratio":"16:9",
"size": 9
}
]
function resizeable(id, aspect=true, ratio="4:3", size=0){
if(aspect != true){
ratio = "";
}
else{
ratioSize.forEach((element) => {
if (element.ratio == ratio){
size = element.size;
}
else{
size = 0;
console.log("invalid ratio, reverting to default size");
}
});
}
//On Load
let image = document.getElementById(id);
window.addEventListener("load", () => {
let imageWidth = image.offsetWidth;
let CalcHeight = parseInt(imageWidth) * size;
let imageHeight = CalcHeight + "px";
image.style.height = imageHeight;
});
//On Resize window
window.addEventListener("resize", () => {
let imageWidth = image.offsetWidth;
let CalcHeight = parseInt(imageWidth) * size;
let imageHeight = CalcHeight + "px";
image.style.height = imageHeight;
});
}
export default resizeable;