【发布时间】:2017-08-12 02:39:22
【问题描述】:
我有一个标题图像,当屏幕宽度短于高度时,我希望屏幕宽度为 100%,当屏幕高度短于高度时,我希望屏幕高度为 100%宽度。有没有办法让我做到这一点?
编辑——有些人想要所有相关的代码,所以就在这里!
HTML
<div id="home-slider" class="carousel slide carousel-fade" data-ride="carousel">
<div class="carousel-inner">
<div class="item active" style="background-image: url(images/logo.png)" >
<div class="caption">
<h1 class="animated fadeInLeftBig">The *** Organization </h1>
<p class="animated fadeInRightBig">***</p>
<a data-scroll class="btn btn-start animated fadeInUpBig" href="#rescue">***</a>
</div>
</div>
</div>
</div>
CSS
#home-slider {
overflow: hidden;
position: relative;
attachment: fixed;
size: cover;
repeat: no-repeat;
width: 100%;
height: 100vh;
max-width: 100%;
height: auto;
}
#home-slider .caption {
position: absolute;
top: 50%;
margin-top: -104px;
left: 0;
right: 0;
text-align: center;
text-transform: uppercase;
z-index: 15;
font-size: 18px;
font-weight: 300;
color: #fff;
vertical-align: center;
height: 100vh;
font-size: 2em;
background-size: contain;
}
#home-slider .caption h1 {
color: #fff;
size: 72px;
font-weight: 800;
margin-bottom: 30px;
vertical-align: center;
}
.caption .btn-start {
color: #fff;
font-size: 14px;
font-weight: 600;
padding:14px 40px;
border: 2px solid #6e6d6c;
border-radius: 4px;
margin-top: 40px;
vertical-align: center;
}
.caption .btn-start:hover {
color: #fff
}
.carousel-fade .carousel-inner .item {
opacity: 0;
background-repeat: no-repeat;
background-size: cover;
height: 100%;
width: 100%;
/* just in case if I want the image to be "parallax"
background-attachment: fixed;
*/
background-position: center;
background-color: #6e6d6c;
}
.carousel-fade .carousel-inner .active {
opacity: 1;
background-size: 100%;
}
.carousel-fade .carousel-control {
z-index: 2;
}
js
var slideHeight = $(window).height();
$('#home .item').css('height',slideHeight);
$(window).resize(function(){'use strict',
$('#home .item').css('height',slideHeight);
});
$(function() {
var width = $(window).width();
var height = $(window).height();
if( width < height ) {
$('#home .item').css({width:'initial',height:height+'px'});
}
});
var logoFix = function() {
if( $(window).width() < $(window).height() ) {
$('.item').css('background-size','auto 100%');
}
else {
$('.item').css('background-size','100%');
}
}
$(function() {
logoFix();
});
【问题讨论】:
-
这是网页版吗?如果是这样 - 您是否使用 bootstrap/jquery/任何其他插件?
-
是网页,我用的是bootstrap和jquery。
标签: image responsive