解决IE中img.onload失效的方法

本文探讨了在Internet Explorer浏览器中使用JavaScript获取图片尺寸时遇到的img.onload事件不触发的问题。通过调整代码顺序,先定义onload事件处理函数再设置图片源的方式解决了此问题。

解决IE中img.onload失效的方法

    最近在做web开发,其中有一个需求:利用Javascript获取要加载的图片的尺寸,所以很自然的,我就想到了img的onload方法,在firefox下开发完成后,我到IE下调试,发现img的onload事件很多情况下都不被调用。

我最初的代码如下:

var  img  =   new  Image;
img.src 
=   "test .gif " ;
img.onload  =   function (){
    alert ( img.width );
};


    这段代码看着没什么问题,但是为什么onload没有被IE调用呢?因为IE会缓存图片,第2次加载的图片,不是从服务器上传过来的,而是从缓冲区里加载的。是不是从缓冲区里加载的图片就不触发onload事件呢?我于是我测试了以下代码,成功了~

var  img  =   new  Image;
img.onload 
=   function (){
    alert ( img.width );
};
img.src 
=   " test.gif " ;

    我把onload写到前面去,先告诉浏览器如何处理这张图片,再指定这张图片的源,这样就正常了。所以,不是IE没有触发onload事件,而是因为加载缓冲区的速度太快,以至于没有运行到img.onload的时候,onload事件已经触发了。这让我想到了Ajax,我们在写xmlhttp的时候,都是先指定onstatechange的回调函数,然后再send数据的,道理是一样的。

<script> document.addEventListener('DOMContentLoaded', (event) => { const images = document.querySelectorAll('img'); images.forEach((image) => { image.addEventListener('click', (event) => { var original_image = document.getElementById("new_image"); if (image.alt != 'no'){ reset_image(); original_image.src = image.src; document.getElementById('image-background').style.cssText='display:block;display: flex;'; } }); }); }); </script> <style> #image-background{ top: 0px; left: 0px; width: 100%; z-index: 95; height: 100%; display: none; height: 100vh; position: fixed; overflow: hidden; align-items: center; justify-content: center; background-color: rgba(0,0,0,0.75); -webkit-transition-duration: 0.2s; transition-duration: 0.2s; } #image-background img { cursor: grab; max-width: 100%; max-height: 100%; border-radius: 8px; -webkit-transition-duration: 0.2s; transition-duration: 0.2s; } #image-background button { border: none; color: black; font-size: 16px; margin: 4px 2px; cursor: pointer; font-size: 18px; overflow: hidden; padding: 15px 26px; text-align: center; align-items: center; border-radius: 10px; text-decoration: none; justify-content: center; display: inline-block; backdrop-filter: blur(6px); font-family: 'Microsoft Yahei'; background-color: rgba(255, 255, 255, 0.5); -webkit-transition-duration: 0.2s; transition-duration: 0.2s; } #image-background button:hover { box-shadow: 0 0 8px rgba(0, 0, 255, 0.6),0 0 16px rgba(0, 0, 255, 0.4), 0 0 32px rgba(0, 0, 255, 0.2); -webkit-transition-duration: 0.2s; transition-duration: 0.2s; } .image-background_close{ top: 10px; right: 12.5px; font-size: 40px; position: fixed; font-weight: bold; padding: 2px 20px; background-color: none; color: rgba(255,255,255,0.75); -webkit-transition-duration: 0.2s; transition-duration: 0.2s; } .image-background_close:hover{ cursor: pointer; border-radius: 6px; text-decoration: none; color: rgba(255,255,255,1); background-color:rgba(0,0,0,0.5); -webkit-transition-duration: 0.2s; transition-duration: 0.2s; } </style> <script> function demo(e){ if(e.wheelDelta){ if(e.wheelDelta > 0){ zoomIn(); } if(e.wheelDelta < 0){ zoomOut(); } } } window.addEventListener("mousewheel",demo) </script> <div id="image-background"> <span style="font-size:22px;font-family:'Microsoft Yahei';background-color:rgba(0,0,0,0.2);padding:10px 20px;border-radius:4px;color:white;top:10px;left:12.5px;position:fixed;z-index:9999;"> 图片预览 </span> <img style="position: absolute;"id="new_image"> </br> <div align="center"style="position:fixed;bottom:0;width:100%;"> <span class="image-background_close"onclick="document.getElementById('image-background').style.cssText='display:none';"> × </span> <button onclick="zoomIn()"> 放大图片 </button> <button onclick="zoomOut()"> 缩小图片 </button> <button onclick="rotateClockwise()"> 顺时针旋转90° </button> <button onclick="counterclockwiseRotation()"> 逆时针旋转90° </button> <button onclick="reset_image()"> 1:1 </button> <button onclick="window.open(new_image.src);"> 在新窗口打开图片 </button> <button onclick="information()"> 查看图片信息 </button> </div> </div> <script> function reset_image(){ scale = 1; rotateAngle = 0; var image_number = document.getElementsByTagName('img'); var image_number = image_number.length - 1; var img = document.getElementById('new_image'); document.getElementById('new_image').style.transform = `scale(${scale})`; document.getElementById('new_image').style.transform = `rotate(${rotateAngle}deg) scale(${scale})`; img.style.top = '50%'; img.style.left = '50%'; img.style.transform = 'translate(-50%, -50%)'; } function zoomIn() { scale += 0.1; document.getElementById('new_image').style.transform = `rotate(${rotateAngle}deg) scale(${scale})`; } function zoomOut() { scale -= 0.1; if(scale < 0.1){ scale = 0.1; return false; } else{ document.getElementById('new_image').style.transform = `rotate(${rotateAngle}deg) scale(${scale})`; } } function rotateClockwise() { rotateAngle += 90; document.getElementById('new_image').style.transform = `rotate(${rotateAngle}deg) scale(${scale})`; } function counterclockwiseRotation() { rotateAngle -= 90; document.getElementById('new_image').style.transform = `rotate(${rotateAngle}deg) scale(${scale})`; } function information(){ alert( "<span style='color:lightblue;'><b>["+new_image.width+"×"+new_image.height+"]</b></span>图片<b>信息</b></span>", "<img src="+new_image.src+"></br></br><p style='font-size:18px;color:black'>图片宽度:<b>"+new_image.width+"</b></br>图片高度:<b>"+new_image.height+"</b></br>图片格式:<b>"+new_image.src.substr(new_image.src.lastIndexOf(".")+1)+"</b></br>路径:<a href='"+new_image.src+"'>"+new_image.src+"</a></br><span>"+new_image.title+"</span>", "关闭<b>图片信息</b>" ) } </script> <img src="80cf53f3-168e-4.png"> <script type="text/javascript"> window.onload = function(){ var new_image = document.getElementById("new_image"); move(new_image); }; function move(obj){ obj.onmousedown = function(event){ obj.setCapture && obj.setCapture(); event = event ||window.event var ol = event.clientX -obj.offsetLeft; var ot = event.clientY - obj.offsetTop; document.onmousemove = function(event){ event = event ||window.event var left = event.clientX-ol; var top = event.clientY-ot; obj.style.left = left+"px"; obj.style.top = top+"px"; }; document.onmouseup = function(){ document.onmousemove = null; document.onmouseup = null; obj.releaseCapture && obj.releaseCapture(); }; return false; }; }; </script> 以上代码中,拖拽功能有bug,拖动之后会恢复到中心位置,而且不能支持放大、旋转后拖动,修复上面的代码存在的bug
最新发布
07-03
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值