复制代码 代码如下: <BR>/* 判断设备是否为PC */ <BR>function isPC() { <BR>var userAgentInfo = navigator.userAgent; <BR>var Agents = new Array("Android", "iPhone", "SymbianOS","Windows Phone", "iPad", "iPod"); <BR>var flag = true; <BR>for ( var v = 0; v < Agents.length; v++) { <BR>if (userAgentInfo.indexOf(Agents[v]) > 0) { <BR>flag = false; <BR>break; <BR>} <BR>} <BR>return flag; <BR>} <br><br>/* 调整图片大小 */ <BR>function AutoResizeImage(maxWidth, maxHeight, objImg) { <BR>var img = new Image(); <BR>img.src = objImg.src; <BR>var hRatio; <BR>var wRatio; <BR>var Ratio = 1; <BR>var w = img.width; <BR>var h = img.height; <BR>wRatio = maxWidth / w; <BR>hRatio = maxHeight / h; <BR>if (maxWidth == 0 && maxHeight == 0) { <BR>Ratio = 1; <BR>} else if (maxWidth == 0) { // <BR>if (hRatio < 1) Ratio = hRatio; <BR>} else if (maxHeight == 0) { <BR>if (wRatio < 1) Ratio = wRatio; <BR>} else if (wRatio < 1 || hRatio < 1) { <BR>Ratio = (wRatio <= hRatio ? wRatio : hRatio); <BR>} <BR>if (Ratio < 1) { <BR>w = w * Ratio; <BR>h = h * Ratio; <BR>} <BR>objImg.height = h; <BR>objImg.width = w; <BR>} <br><br>/* 设置不同设备的缩放策略 */ <BR>function setImg(tagid,pcWidth,pcHeight,appWidth,appHeight){ <BR>var tag=document.getElementById(tagid); <BR>var images=tag.getElementsByTagName("img"); <BR>for(var i=0;i<images.length;i++){ <BR>if(isPC){ <BR>AutoResizeImage(pcWidth, pcHeight, images[i]); <BR>}else{ <BR>AutoResizeImage(appWidth, appHeight, images[i]); <BR>} <BR>} <BR>} <BR>window.onload=function(){ <BR>setImg('imgDIV',300,0,300,0); <BR>} <BR>