為您解碼網(wǎng)站建設(shè)的點(diǎn)點(diǎn)滴滴
發(fā)表日期:2019-09 文章編輯:小燈 瀏覽次數(shù):29928
我的另一網(wǎng)站 CODE大全,使用的 bootstrap 搭建的。但是對(duì)于博客中引入的圖片總是不能隨著移動(dòng)端的適配進(jìn)行友好的展示。今天查閱了一些相關(guān)資料,進(jìn)行了優(yōu)化,在這里做個(gè)總結(jié)!
做法很簡(jiǎn)單,只要設(shè)置對(duì)應(yīng)圖片的width和htight的具體數(shù)值就可以了??聪旅娴睦樱?/p>
img{ width:auto; height:auto; max-width:100%; max-height:100%;}
這樣設(shè)置圖片的話,可以使圖片在指定的空間內(nèi)縮放。4行的意思是:
max-width 和 max-height,優(yōu)先對(duì)max-width進(jìn)行縮放,當(dāng)圖片在max-width縮放后的尺寸仍然超出容器大小時(shí)。max-height就發(fā)揮了作用:對(duì)圖片再次進(jìn)行縮放!舉例:
<div style="width:50px;height:40px"><img src="a.jpg"><!--這里我們假如圖片的實(shí)際尺寸是320X320--></div>
圖片被縮放后在div的尺寸是:
width:50px;(因?yàn)閳D片的width:100%)
height:50px(這里height是有width:100%被縮放后的尺寸。)
我們可以發(fā)現(xiàn)這個(gè)50px的高度仍然超出了div的40px的高度,不符合max-height
這個(gè)時(shí)候,max-height:100%;就會(huì)發(fā)揮作用,
在max-height:100%;的作用下,圖片被縮放后在div的尺寸是:
width:40px;
height:40px;
這個(gè)尺寸符合max-height和max-width
所以,當(dāng)div在不斷變化時(shí),圖片在div中也會(huì)跟著布斷發(fā)生變化,
這里不介意給圖片設(shè)置width:100%;height:100%;會(huì)造成圖片變形。
<!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 瀏覽次數(shù):7047
日期:2019-09 瀏覽次數(shù):4869
日期:2019-09 瀏覽次數(shù):5668
日期:2019-09 瀏覽次數(shù):10927
日期:2019-09 瀏覽次數(shù):10943
日期:2019-09 瀏覽次數(shù):4647
日期:2019-09 瀏覽次數(shù):4232
日期:2019-09 瀏覽次數(shù):4065
日期:2019-09 瀏覽次數(shù):3592
日期:2019-09 瀏覽次數(shù):4386
日期:2019-09 瀏覽次數(shù):8087
日期:2019-09 瀏覽次數(shù):3888
日期:2019-09 瀏覽次數(shù):4394
日期:2019-09 瀏覽次數(shù):3879
日期:2019-09 瀏覽次數(shù):4095
日期:2019-09 瀏覽次數(shù):4357
日期:2019-09 瀏覽次數(shù):5921
日期:2019-09 瀏覽次數(shù):3613
日期:2019-09 瀏覽次數(shù):4651
日期:2019-09 瀏覽次數(shù):4107
日期:2019-09 瀏覽次數(shù):4381
日期:2019-09 瀏覽次數(shù):4399
日期:2019-09 瀏覽次數(shù):3701
日期:2019-09 瀏覽次數(shù):4530
日期:2019-09 瀏覽次數(shù):5638
日期:2019-09 瀏覽次數(shù):5470
日期:2019-09 瀏覽次數(shù):3821
日期:2019-09 瀏覽次數(shù):9176
日期:2019-09 瀏覽次數(shù):4717
日期:2019-09 瀏覽次數(shù):4086
Copyright ? 2013-2018 Tadeng NetWork Technology Co., LTD. All Rights Reserved.