图片处理函数
01 //图片加载完成后按最大宽高等比缩放 02 function resize(o,mw,mh){ 03 var ow=o.width,oh=o.height,os=ow/oh,w=mw||100,h=mh||100,s=w/h; 04 o.setAttribute('oldheight',oh) 05 if(os>s){ 06 if(ow>w){ 07 o.width=w;o.height=w/os; 08 } 09 }else{ 10 if(oh>h){ 11 o.height=h;o.width=h*os 12 } 13 } 14 if(o.height<h){ 15 o.style.marginTop=(h-o.height)/2+'px' 16 } 17 } 18 //图片加载出错时替换成默认图片,同时也解决了ie6的over stack问题 19 function errorload(o,src,mw,mh){ 20 var w=mw||100,h=mh||100; 21 var img=new Image(); 22 img.src=src; 23 img.width=w; 24 img.height=h; 25 o.parentNode.replaceChild(img, o) 26 }
//使用时直接在img标签内写上
<img src="原始图片路径" onload="resize(this,最大宽,最大高)" onerror="errorload(this,'默认图片路径',最大宽,最大高)" alt="*" />
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步