/******************************************************************************/ /* */ /* recorta todas las imágenes que tengas la clase image-crop */ /* El contenedor de la imagen debe tener: */ /* width:xxxx px: */ /* overflow:hidden */ /* */ /******************************************************************************/ $(window).load(function() { $('.image-crop').each(function() { var imageWidth = parseFloat($(this).width()); while(isNaN(imageWidth)) { imageWidth = parseFloat($(this).width()); } var imageHeight = parseFloat($(this).height()); while(isNaN(imageHeight)) { imageHeight = parseFloat($(this).height()); } var imageRatio = imageWidth / imageHeight; var containerWidth = parseFloat($(this).parent().width()); while(isNaN(containerWidth)) { containerWidth = parseFloat($(this).parent().width()); } var containerHeight = parseFloat($(this).parent().height()); while(isNaN(containerHeight)) { containerHeight = parseFloat($(this).parent().height()); } var containerRatio = containerWidth / containerHeight; if(imageRatio <= 1.0 && containerRatio >= 1.0) { //expandir horizontalmente $(this).css('width','100%'); } else if(imageRatio >= 1.0 && containerRatio <= 1.0) { //expandir verticalmente $(this).css('height','100%'); } else if(imageRatio >= 1.0 && containerRatio >= 1.0 && imageRatio >= containerRatio) { /*las 2 imágenes son rectangulares horizontalmente, pero la imagen es más ancha que el contenedor */ //expandir verticalmente $(this).css('height','100%'); } else if(imageRatio >= 1.0 && containerRatio >= 1.0 && imageRatio < containerRatio) { /*las 2 imágenes son rectangulares horizontalmente, pero el contenedor es más ancho que la imagen */ //espandir horizontalmente $(this).css('width','100%'); } else if(imageRatio <= 1.0 && containerRatio <= 1.0 && imageRatio >= containerRatio) { /*las 2 imágenes son rectangulares verticales, pero el contenedor es más alto que la imagen */ //espandir horizontalmente $(this).css('height','100%'); } else if(imageRatio <= 1.0 && containerRatio <= 1.0 && imageRatio < containerRatio) { /*las 2 imágenes son rectangulares verticales, pero la imagen es más alta que el contenedor */ //espandir horizontalmente $(this).css('width','100%'); } else { /* alert("no cargó " + $(this).attr('id')); alert("image width " + imageWidth); alert("container width " + containerWidth); alert("image height " + imageHeight); alert("container height " + containerHeight); alert("image ratio " + imageRatio); alert("container ratio " + containerRatio); */ //$(this).css('height','none'); //$(this).css('width','100%'); } $(this).fadeIn("fast"); }); });