Conseguir ancho de la imagen en la carga de la imagen falla en IE
-
11-10-2019 - |
Pregunta
I tiene una función de cambiar el tamaño de la imagen que las imágenes de cambio de tamaño proporcional. En cada imagen cargar una llamada a esta función con la imagen y cambiar el tamaño si su anchura o la altura es más grande que mi ancho máximo y la altura máx. Puedo conseguir img.width y img.height en FF Chrome Opera Safari pero IE falla. ¿Cómo puedo manejar esto?
Me explico con un trozo de código.
<img src="images/img01.png" onload="window.onImageLoad(this, 120, 120)" /> function onImageLoad(img, maxWidth, maxHeight) { var width = img.width; // Problem is in here var height = img.height // Problem is in here }
En mis líneas highligted img.width no trabajo en la serie IE.
Cualquier sugerencia?
Gracias.
Solución
No utilice width
y height
. Utilice naturalWidth
y naturalHeight
lugar. Estos proporcionan las dimensiones en píxeles de imagen sin escala desde el archivo de imagen y trabajarán en todos los navegadores.
Otros consejos
El hombre, que estaba buscando esto durante 2 días. Gracias.
Estoy usando jQuery, pero no importa. Problema está relacionado con JavaScript en IE.
Mi código anterior:
var parentItem = $('<div/>')
.hide(); // sets display to 'none'
var childImage = $('<img/>')
.attr("src", src)
.appendTo(parentItem) // sets parent to image
.load(function(){
alert(this.width); // at this point image is not displayed, because parents display parameter is set to 'none' - IE gives you value '0'
});
Esto está trabajando en FF, Opera y Safari, pero no IE. Me estaba '0' en el IE.
Solución para mí:
var parentItem = $('<div/>')
.hide();
var childImage = $('<img/>')
.attr("src", src)
.load(function(){
alert(this.width); // at this point image css display is NOT 'none' - IE gives you correct value
childImage.appendTo(parentItem); // sets parent to image
});
Así es como lo solucioné (porque es el único js en el sitio que no quería utilizar una biblioteca).
var imageElement = document.createElement('img');
imageElement.src = el.href; // taken from a link cuz I want it to work even with no script
imageElement.style.display = 'none';
var imageLoader = new Image();
imageLoader.src = el.href;
imageLoader.onload = function() {
loaderElement.parentElement.removeChild(loaderElement);
imageElement.style.position = 'absolute';
imageElement.style.top = '50%';
imageElement.style.left = '50%';
// here using the imageLoaders size instead of the imageElement..
imageElement.style.marginTop = '-' + (parseInt(imageLoader.height) / 2) + 'px';
imageElement.style.marginLeft = '-' + (parseInt(imageLoader.width) / 2) + 'px';
imageElement.style.display = 'block';
}
Es porque el IE no puede anchura y la altura de imágenes display: none
calcular. Uso visibility: hidden
lugar.
Trate
function onImageLoad(img, maxWidth, maxHeight) {
var width = img.width; // Problem is in here
var height = img.height // Problem is in here
if (height==0 && img.complete){
setTimeOut(function(){onImageLoad(img, maxWidth, maxHeight);},50);
}
}
var screenW = screen.width;
var screenH = screen.height;
//alert( screenW );
function checkFotoWidth( img, maxw )
{
if( maxw==undefined)
maxw = 200;
var imgW = GetImageWidth(img.src);
var imgH = GetImageHeight(img.src);
//alert(GetImageWidth(img.src).toString()); // img.width); // objToString(img));
if (imgW > maxw || (img.style.cursor == "hand" && imgW == maxw))
{
if (imgW > screenW) winW = screenW;
else winW = imgW;
if (imgH > screenH) winH = screenH;
else winH = imgH;
img.width=maxw;
img.style.cursor = "pointer";
img.WinW = winW;
img.WinH = winH;
//alert("winW : " + img.WinW);
img.onclick = function() { openCenteredWindow("Dialogs/ZoomWin.aspx?img=" + this.src, this.WinW, this.WinH, '', 'resizable=1'); }
img.alt = "Klik voor een uitvergroting :: click to enlarge :: klicken Sie um das Bild zu vergrössern";
//alert("adding onclick);
}
}
function GetImageWidth(imgSrc)
{
var img = new Image();
img.src = imgSrc;
return img.width;
}
function GetImageHeight(imgSrc)
{
var img = new Image();
img.src = imgSrc;
return img.height;
}
Me gustaría probar este:
function onImageLoad(img, maxWidth, maxHeight) {
var width, height;
if ('currentStyle' in img) {
width = img.currentStyle.width;
height = img.currentStyle.height;
}
else {
width = img.width;
height = img.height;
}
// whatever
}
editar - y al parecer si tuviera que probar que yo iba a aprender que no funciona :-) OK, bueno "ancho" y "alto" parece definitivamente ser atributos de los elementos <img>
en lo que se refiere a IE. Tal vez el problema es que el evento "carga" está disparando para el elemento en el momento equivocado. Para comprobar si ese es el caso, entonces me gustaría probar esto:
function onImageLoad(img, maxWidth, maxHeight) {
var width, height;
var i = new Image();
i.onload = function() {
width = i.width; height = i.height;
// ... stuff you want to do ...
};
i.src = img.href;
}
getDisplay().getImage().setUrl(imgURL);
final Image img = new Image(imgURL);
int w=img.getWidth();
int h=img.getHeight();