我遇到了一个问题。我试图用响应图像构建全屏图像滑块。我尝试了所有组合:添加样式height:100%,width:100%,height :auto,width:auto在HTML中,在CSS中,但似乎没有任何作用。我将向您展示代码,也许我做错了什么。 我无法添加代码段,因为它们不显示图像,并且其中包含很多代码和类。这个问题使我发疯,我正在寻找4小时的修复程序。
HTML
<div id="showcase">
<div id="arrow-left" class="arrow"></div>
<div id="slider">
<div class="slide slide1"></div>
<div class="slide slide2 "></div>
<div class="slide slide3"></div>
</div>
<div id="arrow-right" class="arrow"></div>
</div>
CSS
#showcase,
#slider,
.slide-content {
height: 100vh;
width: 100%;
overflow-x: hidden;
}
#showcase {
position: relative;
height: 100%;
width: 100%;
}
.slide {
width: 100%;
height: 100%;
}
.slide1 {
background: url(/Core/img/lazar1.jpg) no-repeat center center/cover;
}
.slide2 {
background: url(/Core/img/lazar2.jpg) no-repeat center center/cover;
}
.slide3 {
background: url(/Core/img/lazar3.jpg) no-repeat center center/cover;
}
.arrow {
cursor: pointer;
position: absolute;
top: 50%;
margin-top: -50px;
border-style: solid;
}
#arrow-left {
border-width: 30px 40px 30px 0;
border-color: transparent blue transparent transparent;
left: 0;
margin-left: 30px;
}
#arrow-right {
border-width: 30px 0 30px 40px;
border-color: transparent blue transparent blue;
right: 0;
margin-right: 30px;
}
答案 0 :(得分:1)
要解决此类问题,对于响应式设计,请使用媒体查询。被引用的页面在某些宽度断点处使用媒体查询来减小包含图像的div的高度,以便允许屏幕沿宽度方向缩小,同时仍以完整的长宽比显示图像(因为它们是较宽的图像) 。代码中的示例如下所示:
//HTML
<div id="showcase">
<div id="arrow-left" class="arrow"></div>
<div id="slider">
<div class="slide slide1"></div>
<div class="slide slide2 "></div>
<div class="slide slide3"></div>
</div>
<div id="arrow-right" class="arrow"></div>
</div>
//CSS
#showcase,#slider{
height:100vh;
width:100%;
}
.slide{
width:100vw;
height:100vh
}
.slide1{
background:url(http://www.lazarangelov.com/wp-content/uploads/2015/12/lazar3-1920.jpg) no-repeat center center/cover;
}
@media screen and (max-width: 900px) {
.slide{
height: 80vh;
}
}
@media screen and (max-width: 700px) {
.slide{
height: 70vh;
}
}
@media screen and (max-width: 600px) {
.slide{
height: 60vh;
}
}
@media screen and (max-width: 500px) {
.slide{
height: 50vh;
}
}
@media screen and (max-width: 400px) {
.slide{
height: 40vh;
}
}
@media screen and (max-width: 300px) {
.slide{
height: 30vh;
}
}
请注意,这些并不是媒体查询的引人注目的示例,它们只是用于演示该机制。
答案 1 :(得分:1)
在“ slide”类中添加以下代码:
background-size: contain !important;