【发布时间】:2016-12-07 19:49:02
【问题描述】:
这个问题很简单也很常见。我有几个带有图像和文本的框,我希望它们都具有相同的大小,而不管图片尺寸如何,并且在响应时表现得很好。我怎样才能使它变得更好,我已经做到了?在我的本地机器上,javascript 正在运行,但由于我无法理解的原因,这里没有。 javascript 代码正在检查图片是否处于纵向模式并添加另一个类。谢谢。
$(document).ready( function() {
function getImgDim(pic) {
var image = new Image(),
dim = [];
image.src = $(pic).attr("src");
dim = [image.naturalWidth , image.naturalHeight];
return dim;
};
function getContDim(pic) {
return $(pic).parent().width();
};
function portrait (picture) {
$(picture).each(function() {
var dim = getImgDim(this);
// console.log(dim);
if( dim[0] < dim[1] || dim[0] < getContDim(this)) {
$(this).parent().removeClass('u-center-landscape');
$(this).parent().addClass('u-center-portrait');
}
});
};
portrait('.js-box');
});
* {
box-sizing: border-box;
}
figure {
margin: 0;
padding: 0;
}
.boxes {
max-width: 1180px;
margin-left: auto;
margin-right: auto;
padding-left: 24px;
padding-right: 24px;
box-sizing: content-box;
text-align: center;
padding-top: 40px;
}
.boxes:after, .boxes:before {
display: table;
content: "";
}
.boxes:after {
clear: both;
}
.boxes figure {
float: left;
width: 25%;
padding-left: 10px;
padding-right: 10px;
padding-bottom: 30px;
}
.boxes__content {
cursor: pointer;
}
.boxes__content:hover {
-webkit-box-shadow: 0px 0px 20px 5px rgba(102, 102, 102, 0.25);
-moz-box-shadow: 0px 0px 20px 5px rgba(102, 102, 102, 0.25);
box-shadow: 0px 0px 20px 5px rgba(102, 102, 102, 0.25);
border-radius: 10px;
}
.boxes__content:hover .u-center-portrait img {
width: 110%;
transition: width 0.3s ease;
}
.boxes__content:hover .u-center-landscape img {
height: 110%;
transition: height 0.3s ease;
}
.boxes figcaption {
line-height: 60px;
}
.boxes__title {
font-size: 18px;
color: #333;
}
.boxes__img {
height: 220px;
overflow: hidden;
border-radius: 10px 10px 0 0;
}
.u-center-landscape img {
position: relative;
top: 50%;
left: 50%;
height: 100%;
width: auto;
-webkit-transform: translate(-50%, -50%);
-ms-transform: translate(-50%, -50%);
transform: translate(-50%, -50%);
}
.u-center-portrait img {
position: relative;
top: 50%;
left: 50%;
height: auto;
width: 100%;
-webkit-transform: translate(-50%, -50%);
-ms-transform: translate(-50%, -50%);
transform: translate(-50%, -50%);
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<section class="boxes">
<figure>
<div class="boxes__content">
<div class="boxes__img u-center-landscape">
<img class="js-box" src="http://lorempixel.com/700/500/" alt="">
</div>
<figcaption>
<a href="javascript:void(0)" class="boxes__title">Mitteilungen</a>
</figcaption>
</div>
</figure>
<figure>
<div class="boxes__content">
<div class="boxes__img u-center-landscape">
<img class="js-box" src="http://lorempixel.com/400/700/" alt="">
</div>
<figcaption>
<a href="javascript:void(0)" class="boxes__title">Jobbörse</a>
</figcaption>
</div>
</figure>
<figure>
<div class="boxes__content">
<div class="boxes__img u-center-landscape">
<img class="js-box" src="http://lorempixel.com/650/600/" alt="">
</div>
<figcaption>
<a href="javascript:void(0)" class="boxes__title">Ferienjob</a>
</figcaption>
</div>
</figure>
<figure>
<div class="boxes__content">
<div class="boxes__img u-center-landscape">
<img class="js-box" src="http://lorempixel.com/700/400/" alt="">
</div>
<figcaption>
<a href="javascript:void(0)" class="boxes__title">Marktplatz</a>
</figcaption>
</div>
</figure>
<figure>
<div class="boxes__content">
<div class="boxes__img u-center-landscape">
<img class="js-box" src="http://lorempixel.com/700/1200/" alt="">
</div>
<figcaption>
<a href="javascript:void(0)" class="boxes__title">Veranstaltungen</a>
</figcaption>
</div>
</figure>
<figure>
<div class="boxes__content">
<div class="boxes__img u-center-landscape">
<img class="js-box" src="http://lorempixel.com/600/600" alt="">
</div>
<figcaption>
<a href="javascript:void(0)" class="boxes__title">Gruppen</a>
</figcaption>
</div>
</figure>
<figure>
<div class="boxes__content">
<div class="boxes__img u-center-landscape">
<img class="js-box" src="http://lorempixel.com/1400/1920/" alt="">
</div>
<figcaption>
<a href="javascript:void(0)" class="boxes__title">Benachrichtigungen</a>
</figcaption>
</div>
</figure>
<figure>
<div class="boxes__content">
<div class="boxes__img u-center-landscape">
<img class="js-box" src="http://lorempixel.com/1200/500/" alt="">
</div>
<figcaption>
<a href="javascript:void(0)" class="boxes__title">Mein Nachbarn</a>
</figcaption>
</div>
</figure>
</section> <!-- end boxes -->
谢谢。
【问题讨论】:
标签: javascript jquery html css image