/*! @FileOverview Get picture size @date 2010/11/29 @param the CFG configuration item, cfg.img as a reference to a picture object or a url,cfg.oncomplete of a picture for subsequent operations to get the dimension, that is, a callback function. See examples for specific applications @return None */ function getimagesize (CFG) { var img=document.createelement (' img '), Callback=cfg.oncomplete; Img.src = typeof Cfg.img = = ' string '? Cfg.img:cfg.img.src; Img.setattribute (' style ', ' position:absolute;visibility:hidden; '); Document.body.appendChild (IMG); Img.onload=img.onerror=img.onreadystatechange=function () { if (img&&img.readystate&&img.readystate!= ' loaded ' &&img.readystate!= ' complete ') Return Img.onload = Img.onreadystatechange = Img.onerror = null; Callback.call ({"width": img.width, "height": img.height},null); Img.parentNode.removeChild (IMG); Img=null; }; } /* Example 1, get the size of the remote picture * * GetImageSize ({ IMG: ' Http://www.qiqiboy.com/wp-content/uploads/2010/11/bing.png ', Oncomplete:function () { Alert (' Width: ' +this.width+ ', ' + ' height: ' +this.height ') } }); /* Example 2, limit class to myimg picture size is less than 600 * * var imgs=getelementsbyclassname (' myimg ', ' img ', document),//getelementsbyclassname for a write-point acquisition method, not JS native support OH maxwidth=600;//Maximum width of 600 for (Var i=0;i GetImageSize ({ Img:imgs[i], Oncomplete:function () { if (this.width>maxwidth) { Imgs[i].height=math.round (This.height/this.width*maxwidth); Imgs[i].width=maxwidth; } } }); } |