發表日期:2019-09 文章編輯:小燈 瀏覽次數:29999
我的另一網站 CODE大全,使用的 bootstrap 搭建的。但是對于博客中引入的圖片總是不能隨著移動端的適配進行友好的展示。今天查閱了一些相關資料,進行了優化,在這里做個總結!
做法很簡單,只要設置對應圖片的width和htight的具體數值就可以了??聪旅娴睦樱?/p>
img{ width:auto; height:auto; max-width:100%; max-height:100%;}
這樣設置圖片的話,可以使圖片在指定的空間內縮放。4行的意思是:
max-width 和 max-height,優先對max-width進行縮放,當圖片在max-width縮放后的尺寸仍然超出容器大小時。max-height就發揮了作用:對圖片再次進行縮放!舉例:
<div style="width:50px;height:40px"><img src="a.jpg"><!--這里我們假如圖片的實際尺寸是320X320--></div>
圖片被縮放后在div的尺寸是:
width:50px;(因為圖片的width:100%)
height:50px(這里height是有width:100%被縮放后的尺寸。)
我們可以發現這個50px的高度仍然超出了div的40px的高度,不符合max-height
這個時候,max-height:100%;就會發揮作用,
在max-height:100%;的作用下,圖片被縮放后在div的尺寸是:
width:40px;
height:40px;
這個尺寸符合max-height和max-width
所以,當div在不斷變化時,圖片在div中也會跟著布斷發生變化,
這里不介意給圖片設置width:100%;height:100%;會造成圖片變形。
<!DOCTYPE html><html><head><meta charset="utf-8"><title></title><style>img{width: auto;height:auto;max-height: 100%;max-width: 100%;}</style></head><body><div id="div" style="width: 50px;height: 50px;"><img src="../5100036_080649087294_2.jpg" /></div><script>window.onload=function(){setInterval(function(){move();},1000);} function move(){var _width=document.getElementById('div').style.width.replace('px','');if(parseInt(_width)>400){document.getElementById('div').style.width=parseInt(_width)-5+'px';document.getElementById('div').style.height=parseInt(_width)-5+'px';}else{document.getElementById('div').style.width=parseInt(_width)+5+'px';document.getElementById('div').style.height=parseInt(_width)+5+'px';}}</script></body></html>
日期:2019-09 瀏覽次數:7099
日期:2019-09 瀏覽次數:4918
日期:2019-09 瀏覽次數:5713
日期:2019-09 瀏覽次數:10983
日期:2019-09 瀏覽次數:11005
日期:2019-09 瀏覽次數:4697
日期:2019-09 瀏覽次數:4288
日期:2019-09 瀏覽次數:4111
日期:2019-09 瀏覽次數:3629
日期:2019-09 瀏覽次數:4451
日期:2019-09 瀏覽次數:8151
日期:2019-09 瀏覽次數:3944
日期:2019-09 瀏覽次數:4442
日期:2019-09 瀏覽次數:3914
日期:2019-09 瀏覽次數:4160
日期:2019-09 瀏覽次數:4405
日期:2019-09 瀏覽次數:5971
日期:2019-09 瀏覽次數:3666
日期:2019-09 瀏覽次數:4701
日期:2019-09 瀏覽次數:4150
日期:2019-09 瀏覽次數:4452
日期:2019-09 瀏覽次數:4463
日期:2019-09 瀏覽次數:3740
日期:2019-09 瀏覽次數:4575
日期:2019-09 瀏覽次數:5681
日期:2019-09 瀏覽次數:5510
日期:2019-09 瀏覽次數:3882
日期:2019-09 瀏覽次數:9232
日期:2019-09 瀏覽次數:4767
日期:2019-09 瀏覽次數:4134
Copyright ? 2013-2018 Tadeng NetWork Technology Co., LTD. All Rights Reserved.