2017-10-11 89 views
0

我正在按照这个w3c学校教程制作幻灯片的网页。幻灯片与风景和肖像

对我来说,唯一的要求差异是我有与垂直分辨率相同的横向和纵向图像(1280x720和420x720)。

当我加载肖像图像时,它们的缩放比较大,因为图像仅受最大宽度的限制。真的,我想限制为垂直最大高度,所以当我循环它们时,高度是恒定的。

问题是,当我指定max-height并移除.slideshow容器上的最大宽度时,图像将左对齐放在页面上。所以看起来我必须在那里保持最大高度,以保持中心的一致性。

当我定义最大高度和最大宽度时,它将宽度优先于高度。

那么我怎样才能让幻灯片放映图像都被强制保持一致的高度,同时仍然可以在页面中正确绘制一切?

var slideIndex = 1; 
 
showSlides(slideIndex); 
 

 
function plusSlides(n) { 
 
    showSlides(slideIndex += n); 
 
} 
 

 
function currentSlide(n) { 
 
    showSlides(slideIndex = n); 
 
} 
 

 
function showSlides(n) { 
 
    var i; 
 
    var slides = document.getElementsByClassName("mySlides"); 
 
    var dots = document.getElementsByClassName("dot"); 
 
    if (n > slides.length) { 
 
    slideIndex = 1 
 
    } 
 
    if (n < 1) { 
 
    slideIndex = slides.length 
 
    } 
 
    for (i = 0; i < slides.length; i++) { 
 
    slides[i].style.display = "none"; 
 
    } 
 
    for (i = 0; i < dots.length; i++) { 
 
    dots[i].className = dots[i].className.replace(" active", ""); 
 
    } 
 
    slides[slideIndex - 1].style.display = "block"; 
 
    dots[slideIndex - 1].className += " active"; 
 
}
* { 
 
    box-sizing: border-box 
 
} 
 

 
body { 
 
    font-family: Verdana, sans-serif; 
 
    margin: 0 
 
} 
 

 
.mySlides { 
 
    display: none 
 
} 
 

 

 
/* Slideshow container */ 
 

 
.slideshow-container { 
 
    max-width: 1000px; 
 
    position: relative; 
 
    margin: auto; 
 
} 
 

 

 
/* Next & previous buttons */ 
 

 
.prev, 
 
.next { 
 
    cursor: pointer; 
 
    position: absolute; 
 
    top: 50%; 
 
    width: auto; 
 
    padding: 16px; 
 
    margin-top: -22px; 
 
    color: white; 
 
    font-weight: bold; 
 
    font-size: 18px; 
 
    transition: 0.6s ease; 
 
    border-radius: 0 3px 3px 0; 
 
} 
 

 

 
/* Position the "next button" to the right */ 
 

 
.next { 
 
    right: 0; 
 
    border-radius: 3px 0 0 3px; 
 
} 
 

 

 
/* On hover, add a black background color with a little bit see-through */ 
 

 
.prev:hover, 
 
.next:hover { 
 
    background-color: rgba(0, 0, 0, 0.8); 
 
} 
 

 

 
/* Caption text */ 
 

 
.text { 
 
    color: #f2f2f2; 
 
    font-size: 15px; 
 
    padding: 8px 12px; 
 
    position: absolute; 
 
    bottom: 8px; 
 
    width: 100%; 
 
    text-align: center; 
 
} 
 

 

 
/* Number text (1/3 etc) */ 
 

 
.numbertext { 
 
    color: #f2f2f2; 
 
    font-size: 12px; 
 
    padding: 8px 12px; 
 
    position: absolute; 
 
    top: 0; 
 
} 
 

 

 
/* The dots/bullets/indicators */ 
 

 
.dot { 
 
    cursor: pointer; 
 
    height: 15px; 
 
    width: 15px; 
 
    margin: 0 2px; 
 
    background-color: #bbb; 
 
    border-radius: 50%; 
 
    display: inline-block; 
 
    transition: background-color 0.6s ease; 
 
} 
 

 
.active, 
 
.dot:hover { 
 
    background-color: #717171; 
 
} 
 

 

 
/* Fading animation */ 
 

 
.fade { 
 
    -webkit-animation-name: fade; 
 
    -webkit-animation-duration: 1.5s; 
 
    animation-name: fade; 
 
    animation-duration: 1.5s; 
 
} 
 

 
@-webkit-keyframes fade { 
 
    from { 
 
    opacity: .4 
 
    } 
 
    to { 
 
    opacity: 1 
 
    } 
 
} 
 

 
@keyframes fade { 
 
    from { 
 
    opacity: .4 
 
    } 
 
    to { 
 
    opacity: 1 
 
    } 
 
} 
 

 

 
/* On smaller screens, decrease text size */ 
 

 
@media only screen and (max-width: 300px) { 
 
    .prev, 
 
    .next, 
 
    .text { 
 
    font-size: 11px 
 
    } 
 
}
<div class="slideshow-container"> 
 

 
    <div class="mySlides fade"> 
 
    <div class="numbertext">1/3</div> 
 
    <img src="img_nature_wide.jpg" style="width:100%"> 
 
    <div class="text">Caption Text</div> 
 
    </div> 
 

 
    <div class="mySlides fade"> 
 
    <div class="numbertext">2/3</div> 
 
    <img src="img_fjords_wide.jpg" style="width:100%"> 
 
    <div class="text">Caption Two</div> 
 
    </div> 
 

 
    <div class="mySlides fade"> 
 
    <div class="numbertext">3/3</div> 
 
    <img src="img_mountains_wide.jpg" style="width:100%"> 
 
    <div class="text">Caption Three</div> 
 
    </div> 
 

 
    <a class="prev" onclick="plusSlides(-1)">&#10094;</a> 
 
    <a class="next" onclick="plusSlides(1)">&#10095;</a> 
 

 
</div> 
 
<br> 
 

 
<div style="text-align:center"> 
 
    <span class="dot" onclick="currentSlide(1)"></span> 
 
    <span class="dot" onclick="currentSlide(2)"></span> 
 
    <span class="dot" onclick="currentSlide(3)"></span> 
 
</div>

+0

你能不能代替实际图像的图像HTML正确显示图像?这会有很大的帮助。谢谢! –

回答

1

您可以将以下样式保持滑块高度:

.mySlides { 
    position: relative; 
    overflow: hidden; 
    height: 100vh; /* whatever height you want it to be */ 
} 

.mySlides img { 
    /* centre the image */ 
    height: 100%; 
    position: absolute; 
    top: 50%; 
    left: 50%; 
    transform: translate(-50%, -50%); 
} 

var slideIndex = 1; 
 
showSlides(slideIndex); 
 

 
function plusSlides(n) { 
 
    showSlides(slideIndex += n); 
 
} 
 

 
function currentSlide(n) { 
 
    showSlides(slideIndex = n); 
 
} 
 

 
function showSlides(n) { 
 
    var i; 
 
    var slides = document.getElementsByClassName("mySlides"); 
 
    var dots = document.getElementsByClassName("dot"); 
 
    if (n > slides.length) { 
 
    slideIndex = 1 
 
    } 
 
    if (n < 1) { 
 
    slideIndex = slides.length 
 
    } 
 
    for (i = 0; i < slides.length; i++) { 
 
    slides[i].style.display = "none"; 
 
    } 
 
    for (i = 0; i < dots.length; i++) { 
 
    dots[i].className = dots[i].className.replace(" active", ""); 
 
    } 
 
    slides[slideIndex - 1].style.display = "block"; 
 
    dots[slideIndex - 1].className += " active"; 
 
}
* { 
 
    box-sizing: border-box 
 
} 
 

 
body { 
 
    font-family: Verdana, sans-serif; 
 
    margin: 0 
 
} 
 

 
.mySlides { 
 
    display: none 
 
} 
 

 

 
/* Slideshow container */ 
 

 
.slideshow-container { 
 
    max-width: 1000px; 
 
    position: relative; 
 
    margin: auto; 
 
} 
 

 

 
/* Next & previous buttons */ 
 

 
.prev, 
 
.next { 
 
    cursor: pointer; 
 
    position: absolute; 
 
    top: 50%; 
 
    width: auto; 
 
    padding: 16px; 
 
    margin-top: -22px; 
 
    color: white; 
 
    font-weight: bold; 
 
    font-size: 18px; 
 
    transition: 0.6s ease; 
 
    border-radius: 0 3px 3px 0; 
 
} 
 

 

 
/* Position the "next button" to the right */ 
 

 
.next { 
 
    right: 0; 
 
    border-radius: 3px 0 0 3px; 
 
} 
 

 

 
/* On hover, add a black background color with a little bit see-through */ 
 

 
.prev:hover, 
 
.next:hover { 
 
    background-color: rgba(0, 0, 0, 0.8); 
 
} 
 

 

 
/* Caption text */ 
 

 
.text { 
 
    color: #f2f2f2; 
 
    font-size: 15px; 
 
    padding: 8px 12px; 
 
    position: absolute; 
 
    bottom: 8px; 
 
    width: 100%; 
 
    text-align: center; 
 
} 
 

 

 
/* Number text (1/3 etc) */ 
 

 
.numbertext { 
 
    color: #f2f2f2; 
 
    font-size: 12px; 
 
    padding: 8px 12px; 
 
    position: absolute; 
 
    top: 0; 
 
} 
 

 

 
/* The dots/bullets/indicators */ 
 

 
.dot { 
 
    cursor: pointer; 
 
    height: 15px; 
 
    width: 15px; 
 
    margin: 0 2px; 
 
    background-color: #bbb; 
 
    border-radius: 50%; 
 
    display: inline-block; 
 
    transition: background-color 0.6s ease; 
 
} 
 

 
.active, 
 
.dot:hover { 
 
    background-color: #717171; 
 
} 
 

 

 
/* Fading animation */ 
 

 
.fade { 
 
    -webkit-animation-name: fade; 
 
    -webkit-animation-duration: 1.5s; 
 
    animation-name: fade; 
 
    animation-duration: 1.5s; 
 
} 
 

 
@-webkit-keyframes fade { 
 
    from { 
 
    opacity: .4 
 
    } 
 
    to { 
 
    opacity: 1 
 
    } 
 
} 
 

 
@keyframes fade { 
 
    from { 
 
    opacity: .4 
 
    } 
 
    to { 
 
    opacity: 1 
 
    } 
 
} 
 

 

 
/* On smaller screens, decrease text size */ 
 

 
@media only screen and (max-width: 300px) { 
 
    .prev, 
 
    .next, 
 
    .text { 
 
    font-size: 11px 
 
    } 
 
} 
 

 
.mySlides { 
 
    position: relative; 
 
    overflow: hidden; 
 
    height: 100vh; 
 
} 
 

 
.mySlides img { 
 
    /* centre the image */ 
 
    position: absolute; 
 
    top: 50%; 
 
    left: 50%; 
 
    transform: translate(-50%, -50%); 
 
    height: 100%; 
 
}
<div class="slideshow-container"> 
 

 
    <div class="mySlides fade"> 
 
    <div class="numbertext">1/3</div> 
 
    <img src="https://i.imgur.com/hd6AbVQ.jpg"> 
 
    <div class="text">Caption Text</div> 
 
    </div> 
 

 
    <div class="mySlides fade"> 
 
    <div class="numbertext">2/3</div> 
 
    <img src="https://i.imgur.com/ZXwfiH2.jpg"> 
 
    <div class="text">Caption Two</div> 
 
    </div> 
 

 
    <div class="mySlides fade"> 
 
    <div class="numbertext">3/3</div> 
 
    <img src="https://i.imgur.com/hd6AbVQ.jpg"> 
 
    <div class="text">Caption Three</div> 
 
    </div> 
 

 
    <a class="prev" onclick="plusSlides(-1)">&#10094;</a> 
 
    <a class="next" onclick="plusSlides(1)">&#10095;</a> 
 

 
</div> 
 
<br> 
 

 
<div style="text-align:center"> 
 
    <span class="dot" onclick="currentSlide(1)"></span> 
 
    <span class="dot" onclick="currentSlide(2)"></span> 
 
    <span class="dot" onclick="currentSlide(3)"></span> 
 
</div>

+0

添加您提到的.mySlides风格,不保持宽高比。它显示1280x720的420x720图像。添加“.mySlides img”风格没有任何明显的影响,我可以注意到。 – ScottF

+0

这是因为你已经为图像设置了“宽度:100%”内嵌。所以图像被拉伸以适合容器宽度。删除它并切换到'height:100%'会给你想要的结果。我已经更新了我的答案。 –