DIV CSS 佈局教程網

 DIV+CSS佈局教程網 >> 網頁腳本 >> JavaScript入門知識 >> JavaScript綜合知識 >> js判斷設備是否為PC並調整圖片大小
js判斷設備是否為PC並調整圖片大小
編輯:JavaScript綜合知識     

 這篇文章主要介紹了js判斷設備是否為PC並調整圖片大小,需要的朋友可以參考下

 代碼如下: <html>  <head>  <script type="text/javascript">  /* 判斷設備是否為PC */  function isPC() {  var userAgentInfo = navigator.userAgent;  var Agents = new Array("Android", "iPhone", "SymbianOS","Windows Phone", "iPad", "iPod");  var flag = true;  for ( var v = 0; v < Agents.length; v++) {  if (userAgentInfo.indexOf(Agents[v]) > 0) {  flag = false;  break;  }  }  return flag;  }    /* 調整圖片大小 */  function AutoResizeImage(maxWidth, maxHeight, objImg) {  var img = new Image();  img.src = objImg.src;  var hRatio;  var wRatio;  var Ratio = 1;  var w = img.width;  var h = img.height;  wRatio = maxWidth / w;  hRatio = maxHeight / h;  if (maxWidth == 0 && maxHeight == 0) {  Ratio = 1;  } else if (maxWidth == 0) { //  if (hRatio < 1) Ratio = hRatio;  } else if (maxHeight == 0) {  if (wRatio < 1) Ratio = wRatio;  } else if (wRatio < 1 || hRatio < 1) {  Ratio = (wRatio <= hRatio ? wRatio : hRatio);  }  if (Ratio < 1) {  w = w * Ratio;  h = h * Ratio;  }  objImg.height = h;  objImg.width = w;  }    /* 設置不同設備的縮放策略 */  function setImg(tagid,pcWidth,pcHeight,appWidth,appHeight){  var tag=document.getElementById(tagid);  var images=tag.getElementsByTagName("img");  for(var i=0;i<images.length;i++){  if(isPC){  AutoResizeImage(pcWidth, pcHeight, images[i]);  }else{  AutoResizeImage(appWidth, appHeight, images[i]);  }  }  }  window.onload=function(){  setImg('imgDIV',300,0,300,0);  }  </script>  </head>  <body>  <div id="imgDIV">  <img alt="" src="http://192.168.1.116:9999/ffzx/news/20140205/015212022_1.jpg" />  <div>  <img alt="" src="http://192.168.1.116:9999/ffzx/news/20140208/1386835169183.jpg" />  </div>  </div>  <br>  </body>  </html>   
XML學習教程| jQuery入門知識| AJAX入門| Dreamweaver教程| Fireworks入門知識| SEO技巧| SEO優化集錦|
Copyright © DIV+CSS佈局教程網 All Rights Reserved