Last active
August 29, 2015 14:25
-
-
Save RollForCode/448a977856ec77db1c10 to your computer and use it in GitHub Desktop.
Img list resizer
This file contains hidden or 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
/* Resizes a series of files (assign parent image container on line 5). | |
image ratio's are compared to always fill the screen without streching the image. | |
*/ | |
function setImgSize(){ | |
var list = document.getElementById("ASSIGN_IMAGE_CONTAINER"); | |
var elementChildren = list.children; | |
for (var i = 0; i < list.children.length; i++) { | |
var windowWidth = Math.max(document.documentElement.clientWidth, window.innerWidth || 0); | |
var windowHeight = Math.max(document.documentElement.clientHeight, window.innerHeight || 0); | |
var windowHeightString = windowHeight.toString() + "px"; | |
var windowWidthString = windowWidth.toString() + "px"; | |
var imgNaturalHeight = list.children[i].naturalHeight; | |
var imgNaturalWidth = list.children[i].naturalWidth; | |
var imgNaturalRatio = imgNaturalHeight/imgNaturalWidth; | |
var widthCompare = windowWidth / imgNaturalWidth; | |
var heightCompare = windowHeight / imgNaturalHeight; | |
if (widthCompare >= heightCompare) { | |
var imgNewHeight = imgNaturalRatio * windowWidth; | |
imgNewHeight = Math.round(imgNewHeight); | |
imgNewHeight = imgNewHeight.toString() + "px"; | |
list.children[i].style.width = windowWidthString; | |
list.children[i].style.height = imgNewHeight; | |
} | |
else if (heightCompare > widthCompare) { | |
var imgNewWidth = windowHeight / imgNaturalRatio; | |
imgNewWidth = Math.round(imgNewWidth); | |
imgNewWidth = imgNewWidth.toString() + "px"; | |
list.children[i].style.height = windowHeightString; | |
list.children[i].style.width = imgNewWidth; | |
} | |
else { | |
console.log("imgInsert failed"); | |
} | |
}; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment