Created
October 9, 2020 20:40
-
-
Save puentejose/84471cb237e4caa8a1ec7b29399dbe17 to your computer and use it in GitHub Desktop.
Script for grabbing images and make them responsive, change dimensions accordingly.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const IMAGES = document.querySelectorAll("img"); | |
const SIZES = { | |
showcase: "100vw", | |
reason: "(max-width: 799px) 100vw, 372px", | |
feature: "(max-width: 799px) 100vw, 558px", | |
story: "(max-width: 799px) 100vw, 670px" | |
}; | |
function makeSrcset(imgSrc) { | |
let markup = []; | |
let width = 400; | |
for (let i = 0; i < 5; i++) { | |
markup[i] = imgSrc + "-" + width + ".jpg " + width + "w"; | |
width += 400; | |
} | |
return markup.join(); | |
} | |
for (let i = 0; i < IMAGES.length; i++) { | |
let imgSrc = IMAGES[i].getAttribute("src"); | |
imgSrc = imgSrc.slice(0, -8); | |
let srcset = makeSrcset(imgSrc); | |
IMAGES[i].setAttribute("srcset", srcset) | |
let type = IMAGES[i].getAttribute("data-type"); | |
let sizes = SIZES[type]; | |
IMAGES[i].setAttribute("sizes", sizes); | |
console.log(type); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment