我有一张图片幻灯片,上面有四张不同高度的图片。我试图将它们保持在700px的相同高度,但我无法弄清楚如何。我将img
设置为max-height: 700px
,但这似乎不起作用。
这是codepen。
var slideIndex = 1;
showSlide(slideIndex);
function plusSlides(n) {
showSlide(slideIndex += n);
}
function currentSlide(n) {
showSlide(slideIndex = n);
}
function showSlide(n) {
var i;
var slides = document.getElementsByClassName("myslides");
var dots = document.getElementsByClassName("dots");
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";
}
body{
font-family: verdana,sans-serif;
margin: 0;
font-size: 100%;
}
#slideshow-container{
position: relative;
margin: auto;
background-color: black;
overflow: hidden;
}
#slideshow-container img{
display: block;
margin: 0 auto;
max-width: 100%;
position: relative;
max-height: 700px !important;
}
.myslides{
display: none;
}
.prev , .next{
position: absolute;
top: 50%;
font-size: 30px;
font-weight: bold;
padding: 16px;
margin-top: -22px;
border-radius: 0 3px 3px 0;
color: #fff;
cursor: pointer;
z-index: 1;
}
.next{
right: 0;
border-radius: 3px 0 0 3px;
}
.prev:hover,.next:hover{
background-color: rgba(0,0,0,0.8);
}
.caption{
text-align: center;
position: absolute;
bottom: 5px;
width: 100%;
color: #f2f2f2;
font-size: 1em;
/* font-size: 15px; */
/* padding: 8px 22px; */
}
.fade{
animation-name:fade;
animation-duration:1.5s;
}
@keyframes fade{
from {opacity: 0.4;}
to {opacity: 1;}
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<!---Images not owned by me --->
<div id="slideshow-container">
<div id="mainImg">
<div class="myslides fade">
<div><img src="https://static.boredpanda.com/blog/wp-content/uploads/2014/12/Top-10-photographers-for-travel-portraits27__700.jpg"></div>
<div class="caption">Text 1</div>
</div>
<div class="myslides fade">
<div><img src="https://cdn-images-1.medium.com/max/1600/1*way-yv9effv2b7PKZeUDMA.png"></div>
<div class="caption">Text 2</div>
</div>
<div class="myslides fade">
<div><img src="https://www.shutterbug.com/images/17/promosa111217.png"></div>
<div class="caption">Text 3</div>
</div>
<div class="myslides fade">
<div><img src="http://jmacpratt.weebly.com/uploads/1/2/0/1/12013129/portrait2_orig.jpg"></div>
<div class="caption">Text 4</div>
</div>
<a class="prev" onclick="plusSlides(-1)">❮</a>
<a class="next" onclick="plusSlides(1)">❯</a>
</div>
</div>
答案 0 :(得分:1)
将max-height: 700px
样式更改为height: 700px
,将width: 100%;
更改为width: auto;
var slideIndex = 1;
showSlide(slideIndex);
function plusSlides(n){
showSlide(slideIndex += n);
}
function currentSlide(n) {
showSlide(slideIndex = n);
}
function showSlide(n){
var i;
var slides = document.getElementsByClassName("myslides");
var dots = document.getElementsByClassName("dots");
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";
}
body{
font-family: verdana,sans-serif;
margin: 0;
font-size: 100%;
}
#slideshow-container{
position: relative;
margin: auto;
background-color: black;
overflow: hidden;
}
#slideshow-container img{
display: block;
margin: 0 auto;
width: auto;
position: relative;
height: 700px !important;
}
.myslides{
display: none;
}
.prev , .next{
position: absolute;
top: 50%;
font-size: 30px;
font-weight: bold;
padding: 16px;
margin-top: -22px;
border-radius: 0 3px 3px 0;
color: #fff;
cursor: pointer;
z-index: 1;
}
.next{
right: 0;
border-radius: 3px 0 0 3px;
}
.prev:hover,.next:hover{
background-color: rgba(0,0,0,0.8);
}
.caption{
text-align: center;
position: absolute;
bottom: 5px;
width: 100%;
color: #f2f2f2;
font-size: 1em;
/* font-size: 15px; */
/* padding: 8px 22px; */
}
.fade{
animation-name:fade;
animation-duration:1.5s;
}
@keyframes fade{
from {opacity: 0.4;}
to {opacity: 1;}
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<!---Images not owned by me --->
<div id="slideshow-container">
<div id="mainImg">
<div class="myslides fade">
<div><img src="https://static.boredpanda.com/blog/wp-content/uploads/2014/12/Top-10-photographers-for-travel-portraits27__700.jpg"></div>
<div class="caption">Text 1</div>
</div>
<div class="myslides fade">
<div><img src="https://cdn-images-1.medium.com/max/1600/1*way-yv9effv2b7PKZeUDMA.png"></div>
<div class="caption">Text 2</div>
</div>
<div class="myslides fade">
<div><img src="https://www.shutterbug.com/images/17/promosa111217.png"></div>
<div class="caption">Text 3</div>
</div>
<div class="myslides fade">
<div><img src="http://jmacpratt.weebly.com/uploads/1/2/0/1/12013129/portrait2_orig.jpg"></div>
<div class="caption">Text 4</div>
</div>
<a class="prev" onclick="plusSlides(-1)">❮</a>
<a class="next" onclick="plusSlides(1)">❯</a>
</div>
</div>
答案 1 :(得分:0)
添加属性height =&#34; 700px&#34;到外面的容器 和相同的属性高度=&#34; 700px&#34;每个img元素:
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<!---Images not owned by me --->
<div id="slideshow-container" height="700px">
<div id="mainImg">
<div class="myslides fade">
<div><img height="700px" src="https://static.boredpanda.com/blog/wp-content/uploads/2014/12/Top-10-photographers-for-travel-portraits27__700.jpg"></div>
<div class="caption">Text 1</div>
</div>
<div class="myslides fade">
<div><img height="700px" src="https://cdn-images-1.medium.com/max/1600/1*way-yv9effv2b7PKZeUDMA.png"></div>
<div class="caption">Text 2</div>
</div>
<div class="myslides fade">
<div><img height="700px" src="https://www.shutterbug.com/images/17/promosa111217.png"></div>
<div class="caption">Text 3</div>
</div>
<div class="myslides fade">
<div><img height="700px" src="http://jmacpratt.weebly.com/uploads/1/2/0/1/12013129/portrait2_orig.jpg"></div>
<div class="caption">Text 4</div>
</div>
<a class="prev" onclick="plusSlides(-1)">❮</a>
<a class="next" onclick="plusSlides(1)">❯</a>
</div>
</div>
&#13;
答案 2 :(得分:0)
我建议采用另一种方法,将图像添加为背景图像,因为它可以更容易地设置特定高度并保持相对于它的宽度。
var slideIndex = 1;
showSlide(slideIndex);
function plusSlides(n) {
showSlide(slideIndex += n);
}
function currentSlide(n) {
showSlide(slideIndex = n);
}
function showSlide(n) {
var i;
var slides = document.getElementsByClassName("myslides");
var dots = document.getElementsByClassName("dots");
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";
}
&#13;
body {
font-family: verdana, sans-serif;
margin: 0;
font-size: 100%;
}
#slideshow-container {
position: relative;
margin: auto;
background-color: black;
overflow: hidden;
}
#slideshow-container>div {
position: relative;
height: 700px;
width: 100%;
}
#slideshow-container .bg-img {
background-repeat: no-repeat;
background-size: auto 100%;
background-position: center;
position: absolute;
width: 100%;
height: 100%;
top: 0;
left: 0;
}
#slideshow-container img {
display: block;
margin: 0 auto;
max-width: 100%;
position: relative;
max-height: 700px !important;
}
.myslides {
display: none;
}
.prev,
.next {
position: absolute;
top: 50%;
font-size: 30px;
font-weight: bold;
padding: 16px;
margin-top: -22px;
border-radius: 0 3px 3px 0;
color: #fff;
cursor: pointer;
z-index: 1;
}
.next {
right: 0;
border-radius: 3px 0 0 3px;
}
.prev:hover,
.next:hover {
background-color: rgba(0, 0, 0, 0.8);
}
.caption {
text-align: center;
position: absolute;
bottom: 5px;
width: 100%;
color: #f2f2f2;
font-size: 1em;
/* font-size: 15px; */
/* padding: 8px 22px; */
}
.fade {
animation-name: fade;
animation-duration: 1.5s;
}
@keyframes fade {
from {
opacity: 0.4;
}
to {
opacity: 1;
}
}
&#13;
<!---Images not owned by me --->
<div id="slideshow-container">
<div id="mainImg">
<div class="myslides fade">
<div>
<span class="bg-img" style="background-image: url('https://static.boredpanda.com/blog/wp-content/uploads/2014/12/Top-10-photographers-for-travel-portraits27__700.jpg')"></span>
</div>
<div class="caption">Text 1</div>
</div>
<div class="myslides fade">
<div>
<span class="bg-img" style="background-image: url('https://cdn-images-1.medium.com/max/1600/1*way-yv9effv2b7PKZeUDMA.png')"></span>
</div>
<div class="caption">Text 2</div>
</div>
<div class="myslides fade">
<div>
<span class="bg-img" style="background-image: url('https://www.shutterbug.com/images/17/promosa111217.png')"></span>
</div>
<div class="caption">Text 3</div>
</div>
<div class="myslides fade">
<div>
<span class="bg-img" style="background-image: url('http://jmacpratt.weebly.com/uploads/1/2/0/1/12013129/portrait2_orig.jpg')"></span>
</div>
<div class="caption">Text 4</div>
</div>
<a class="prev" onclick="plusSlides(-1)">❮</a>
<a class="next" onclick="plusSlides(1)">❯</a>
</div>
</div>
<br>
<div style="text-align:center">
<span class="dots" onclick="currentSlide(1)"></span>
<span class="dots" onclick="currentSlide(2)"></span>
<span class="dots" onclick="currentSlide(3)"></span>
<span class="dots" onclick="currentSlide(4)"></span>
</div>
&#13;