1. 程式人生 > >css img 等比例自動縮放

css img 等比例自動縮放

  1. img{  
  2.     width: auto;  
  3.     height: auto;  
  4.     max-width: 100%;  
  5.     max-height: 100%;     
  1. }  

-------------------------------------------------------------------------------------------------------------

img.item-avatar {
    width: 100%;
    height: 100%;
    objec-fit: cover;

}-------------------------------------------------------------------------------------------------------------

1 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
  2 <html>
  3  <head>
  4   <title> ResizeImage </title>
  5   <meta name="Author" content="Ryan Shaw">
  6   <meta name="Keywords" content="Javascript,js,css,img,margin,onload,onchange"
> 7 <meta name="Description" content="image preview and autoresize"> 8 <style type="text/css"> 9 .form-txt { 10 height: 22px; 11 padding-left: 3px; 12 line-height: 22px; 13 background-color: #FFFFFF; 14 border: 1px solid #BBBBBB
; 15 vertical-align: middle; 16 width: 200px; 17 color: #666; 18 font-size: 12px; 19 } 20 .release-hd{ background:#FFFFFF; border:1px solid #e5e5e5; padding:15px;} 21 .item { margin:0 auto; padding-bottom: 10px; overflow: hidden; height:24px;} 22 .item-name { float: left; font-size: 12px; padding-right: 8px; padding-top: 0px; text-align: right; width: 100px; color:#617b24; font-weight:700;} 23 </style> 24 <script type="text/javascript"> 25 // 不喜歡定義全域性變數的同學可以不在這兒定義,我沒有使用,用起來的話要好些個人認為 26 var MAX_WIDTH = 300; // 最大寬度 27 var MAX_HEIGHT = 200; // 最大高度 28 29 // 預覽圖片 30 function previewImage(file,order) 31 { 32 var div = document.getElementById("preview"); 33 if (file.files && file.files[0]) 34 { 35 div.innerHTML = "<img id=\"imghead\">"; 36 var img = document.getElementById("imghead"); 37 var reader = new FileReader(); 38 reader.onload = function(evt){ 39 AutoResizeImage(300,200,img,evt.target.result); 40 } 41 reader.readAsDataURL(file.files[0]); 42 } 43 else 44 { 45 div.innerHTML = "<img id=\"imghead\">"; 46 var img = document.getElementById("imghead"); 47 AutoResizeImage(300,200,img,file.value); 48 } 49 } 50 51 // 縮放圖片,imgSrc使用者延遲載入圖片url 52 function AutoResizeImage(maxWidth,maxHeight,objImg,imgSrc){ 53 var img = new Image(); 54 img.src = imgSrc || objImg.src; 55 var hRatio; 56 var wRatio; 57 var Ratio = 1; 58 var w = img.width; 59 var h = img.height; 60 wRatio = maxWidth / w; 61 hRatio = maxHeight / h; 62 if (maxWidth ==0 && maxHeight==0){ 63 Ratio = 1; 64 }else if (maxWidth==0){ 65 if (hRatio<1) Ratio = hRatio; 66 }else if (maxHeight==0){ 67 if (wRatio<1) Ratio = wRatio; 68 }else if (wRatio<1 || hRatio<1){ 69 Ratio = (wRatio<=hRatio?wRatio:hRatio); 70 } 71 if (Ratio<1){ 72 w = w * Ratio; 73 h = h * Ratio; 74 } 75 objImg.style.height = Math.round(h) + "px"; 76 objImg.style.width = Math.round(w) + "px"; 77 78 if(h < maxHeight) { // 縱向有空餘空間 79 objImg.style.marginTop = Math.round((maxHeight - h) / 2) + "px"; 80 } 81 if(w < maxWidth) { // 橫向有空餘空間 82 objImg.style.marginLeft = Math.round((maxWidth - w) / 2) + "px"; 83 } 84 if(!!!objImg.src) 85 objImg.src = imgSrc; 86 } 87 </script> 88 </head> 89 90 <body> 91 <div class="release-hd"> 92 <table> 93 <tbody> 94 <tr> 95 <td> 96 <p class="item"> 97 <label class="item-name">圖片:</label> 98 <input class="form-txt" type="file" name="adPic" onchange=previewImage(this)> 99 </p> 100 <p class="item"> 101 <label class="item-name">連結:</label> 102 <input class="form-txt" type="text" name="adU