2017-03-08 90 views
0

我正在使用Angular來遍歷html元素並顯示圖像。但圖像不填充縮略圖容器。我已將最大高度和寬度設置爲 max-height: 100%; max-width: 100%;想用圖像填充縮略圖容器,使用ng-repeat Angularjs

但是圖像仍顯示在縮略圖容器的一部分上。

CSS

.row .col-md-12 .thumbnail{ 
    display: inline-block; 
    width: 350px; 
    height: 275px; 
    padding: 10%; 
    position:relative; 
} 
img { 
    max-height: 100%; 
    max-width: 100%; 
} 

HTML

<ul class="col-md-12" > 
       <li class="thumbnail" ng-repeat="course in courses" > 
        <a ui-sref="course({id:course.id})"> 
         <img ng-src="{{course.picture}}" alt="" /> 
        </a> 
       </li> 
     </ul> 
+0

嘗試用'最小高度:100%;' –

+0

@ sachila ranawaka我試過,但沒有奏效。謝謝 –

+1

從'.thumbnail'中刪除10%填充。另外,如果圖像尺寸不是350 x 275;會有填充'.thumbnail'容器的問題。 – ZimSystem

回答

1

我過去做的是使用一個div display: inline-block,這給你的IMG的一個背景網址。然後將背景大小設置爲cover。就像這樣:

.yourdiv { 
    display: inline-block; 
    height: 100px; 
    width: 100px; 
    background: url('yoururl.com'); 
    background-size: cover; 
} 

在角度,因爲你有可變IMG源,HTML會也許是這樣的:

<div class="yourdiv" style="background: url('{{course.picture}}')" ng-repeat"course in courses"> 
0

var app = angular.module('myApp', []); 
 
app.controller('myCtrl', function($scope) { 
 
    $scope.courses=[{name:"test1",picture: "https://cdn.sstatic.net/Sites/stackoverflow/img/[email protected]?v=73d79a89bded"},{name:"test2",picture: "http://hammerjs.github.io/assets/img/stackoverflow-icon.svg"}]; 
 
});
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"/> 
 
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script> 
 
<div class="container"> 
 
<div class="row"> 
 

 

 
<ul class="col-md-6" ng-app="myApp" ng-controller="myCtrl"> 
 
    <li class="thumbnail" ng-repeat="course in courses" > 
 
     <a> 
 
      <img ng-src="{{course.picture}}" alt="" /> 
 
     </a> 
 
    </li> 
 
</ul> 
 

 
</div></div> 
 
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>

添加類=「img-響應「您的img標籤,它應該工作正常

+0

我試過了,但它沒有工作,謝謝。 –