固定的div中圖片的適配顯示

在很多平臺的新聞,博客列表中都能見到有圖文顯示,因為新聞,博客等列表都也許不是平臺發(fā)布的,無法控制很多圖片的大小尺寸,如何在雜亂無章的圖片尺寸中讓列表的圖片看起來整潔呢?如下情況:

---文章列表---

WeChat_1493876450.jpeg

---文章詳情---

WeChat_1493876463.jpeg

看出些什么來了嗎?列表的圖片和詳情的圖片大小是不一樣的;

下面是我的一種實現(xiàn)方法和網(wǎng)絡中的一種實現(xiàn)方式的比較,之前都是總感覺網(wǎng)絡中的某些方式不太準確,我視乎也覺得我的方式還不太準確吧。希望大家有好的方法能交流。

<!DOCTYPE html>
<html>
<head>
    <title></title>
    <style type="text/css">
        .imgcontent{
            width: 190px;
            height: 100px;
            overflow: hidden;
            background-color: yellow;
        }
    </style>
    <script type="text/javascript" src="./jquery-3.2.1.min.js"></script>
    <script type="text/javascript">
        function getImageWidth(url,callback){
            var img = new Image();
            img.src = url;
            
            // 如果圖片被緩存,則直接返回緩存數(shù)據(jù)
            if(img.complete){
                callback(img.width, img.height);
            }else{
                    // 完全加載完畢的事件
                img.onload = function(){
                callback(img.width, img.height);
                }
            }
        }
        $(document).ready(function() {
            var img=$(".imgcontent img");
            var imgWith=img.width();//圖片的實際高度
            var imgHeight=img.height();//圖片的實際高度
            var imgSrc = img.attr("src");
            getImageWidth(imgSrc,function(w,h){
                imgWith=w;
                imgHeight=h;
            var parentWidth=parseInt(img.parent().width());//父視圖寬度
            var parentHeight=parseInt(img.parent().height());//父視圖高度
            var imgRotio=imgWith/imgHeight;
            var parentRotio=parentWidth/parentHeight;
            if (imgRotio>parentRotio) {//寬圖片
                var heightRotio=imgHeight/parentHeight;//寬的比例
                var width02=parentWidth/parentWidth;//求高
                img.css({"height":parentHeight+"px","margin-left":(parseInt(parentWidth)-width02)/2+"px"});
            }else{//高圖片
                var widthRotio=imgWith/parentWidth;//寬的比例
                var imgHeight02=imgHeight/widthRotio;//求高
                img.css({"width":parentWidth+"px","height":imgHeight02+"px","margin-top":(parseInt(parentHeight)-imgHeight02)/2+"px"});
            }
            });
            
        });
    </script>
</head>
<body>
<h2>這是原圖大小</h2>
<img src="./me.jpg" style="vertical-align: middle;"
       />
<h2>我的方式</h2>
<div class="imgcontent">
      <img src="./me.jpg" style="vertical-align: middle;"
       />
</div>
<h2>網(wǎng)絡方式一</h2>
<div style="width: 190px; height: 100px; overflow: hidden;">
    <a href="">
      <img src="./me.jpg"  style="vertical-align: middle;"
       onload="this.style.marginTop = (parseInt(this.parentNode.parentNode.style.height) - this.height)/2 + 'px';this.style.marginLeft = (parseInt(this.parentNode.parentNode.style.width) - this.width) /2 + 'px'" />
       </a>
</div>
</body>
</html>

效果如下:

4.pic.jpg

你更贊成上面那種方式呢?有更好的方法請分享!

最后編輯于
?著作權歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容