Usé esta función para obtener la relación ancho/alto de una foto para asignar valores automáticos de retrato o paisaje:
var newimage = new Image(); newimage.src = ImageDir; //Directory of my image newimage.onload = function ratio() { var width = this.naturalWidth; var height = this.naturalHeight; var divi = (width/height); return divi.value; } alert(divi)Devolverá el valor correcto, pero quiero en corporativo este código:
if (divi > 1) { node.getElementById("myImage"+"-"+nump).dataset.size = ("1600x1200"); } else { node.getElementById("myImage"+"-"+nump).dataset.size = ("1200x1600"); }Intenté algo como esto:
var newimage = new Image(); newimage.src = ImageDir; //Directory of my image newimage.onload = function ratio() { var width = this.naturalWidth; var height = this.naturalHeight; var divi = (width/height); if (divi > 1) { node.getElementById("myImage"+"-"+nump).dataset.size = ("1600x1200"); //nump is the number of repetitions the While loop as been in (for increments) } else { node.getElementById("myImage"+"-"+nump).dataset.size = ("1200x1600"); } }Pero los valores de if y else no darán salida... Lo hacen si los saco de la primera función. Todo esto está en un bucle While, por lo que la variable divi cambiará cada vez.
¿Puede alguien ayudarme a obtener ese valor Divi para mi condición If? ¡Gracias!