我正在使用Swiper构建幻灯片,需要一些帮助来设计活动幻灯片的样式。目前,幻灯片的比例为。75,并且活动幻灯片被设置为1,导致特写效果。然而,我在调整覆盖以匹配图像对象的增长方面遇到了困难。有没有一种更有效的方法来实现这一点,而不应用相同的规模转换覆盖div?
此外,如果可能的话,我想确保比例转换在任何情况下都不会对文本产生影响。
var swiper = new Swiper('.swiper-container', {
slidesPerView: 5,
centeredSlides: true,
loop: true,
spaceBetween: 0,
navigation: {
nextEl: '.swiper-button-next',
prevEl: '.swiper-button-prev',
},
});
body {
font-size: 20px;
}
.swiper-container {
border: 1px solid red;
}
.swiper-slide {
transition: all .25s ease-out;
opacity: 1;
}
.image-overlay {
transition: all .25s ease-in-out;
transform: scale(.75);
transform-origin: center;
}
.swiper-slide img {
max-width: 100%;
display: block;
}
.swiper-slide-active .image-overlay {
transform: scale(1);
}
.image-overlay::after {
content: '';
position: absolute;
top: 0;
left: 0;
bottom: 0;
right: 0;
background-color: rgba(0, 0, 0, 0.5);
}
.overlay {
position: absolute;
top: 0;
left: 0;
bottom: 0;
right: 0;
color: #fff;
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
}
.text {
display: none;
text-align: center;
padding: 0 25px;
transition: all .25s ease-in-out;
}
.swiper-slide-active .text {
display: block;
}
<link href="https://cdnjs.cloudflare.com/ajax/libs/Swiper/9.2.4/swiper-bundle.css" rel="stylesheet"/>
<script src="https://cdnjs.cloudflare.com/ajax/libs/Swiper/9.2.4/swiper-bundle.min.js"></script>
<div class="swiper-container">
<div class="swiper-wrapper">
<div class="swiper-slide">
<div class="image-overlay">
<img src="https://picsum.photos/600/900?image=10" alt="dummy-image">
</div>
<div class="overlay">
<div class="year">1906</div>
<div class="text">Lorem ipsum dolor sit amet, consectetur adipiscing elit. Phasellus aliquet gravida ligula. Praesent vel urna sit amet enim hendrerit gravida.</div>
</div>
</div>
<div class="swiper-slide">
<div class="image-overlay">
<img src="https://picsum.photos/600/900?image=11" alt="dummy-image">
</div>
<div class="overlay">
<div class="year">1908</div>
<div class="text">Lorem ipsum dolor sit amet, consectetur adipiscing elit. Phasellus aliquet gravida ligula. Praesent vel urna sit amet enim hendrerit gravida.</div>
</div>
</div>
<div class="swiper-slide">
<div class="image-overlay">
<img src="https://picsum.photos/600/900?image=12" alt="dummy-image">
</div>
<div class="overlay">
<div class="year">1920</div>
<div class="text">Lorem ipsum dolor sit amet, consectetur adipiscing elit. Phasellus aliquet gravida ligula. Praesent vel urna sit amet enim hendrerit gravida.</div>
</div>
</div>
<div class="swiper-slide">
<div class="image-overlay">
<img src="https://picsum.photos/600/900?image=13" alt="dummy-image">
</div>
<div class="overlay">
<div class="year">1945</div>
<div class="text">Lorem ipsum dolor sit amet, consectetur adipiscing elit. Phasellus aliquet gravida ligula. Praesent vel urna sit amet enim hendrerit gravida.</div>
</div>
</div>
<div class="swiper-slide">
<div class="image-overlay">
<img src="https://picsum.photos/600/900?image=14" alt="dummy-image">
</div>
<div class="overlay">
<div class="year">1967</div>
<div class="text">Lorem ipsum dolor sit amet, consectetur adipiscing elit. Phasellus aliquet gravida ligula. Praesent vel urna sit amet enim hendrerit gravida.</div>
</div>
</div>
<div class="swiper-slide">
<div class="image-overlay">
<img src="https://picsum.photos/600/900?image=15" alt="dummy-image">
</div>
<div class="overlay">
<div class="year">1990</div>
<div class="text">Lorem ipsum dolor sit amet, consectetur adipiscing elit. Phasellus aliquet gravida ligula. Praesent vel urna sit amet enim hendrerit gravida.</div>
</div>
</div>
<div class="swiper-slide">
<div class="image-overlay">
<img src="https://picsum.photos/600/900?image=16" alt="dummy-image">
</div>
<div class="overlay">
<div class="year">2001</div>
<div class="text">Lorem ipsum dolor sit amet, consectetur adipiscing elit. Phasellus aliquet gravida ligula. Praesent vel urna sit amet enim hendrerit gravida.</div>
</div>
</div>
<div class="swiper-button-prev"></div>
<div class="swiper-button-next"></div>
</div>
</div>
你也可以在这里看到代码:https://codepen.io/tebrown/pen/dygNxJo
2条答案
按热度按时间9jyewag01#
您可以通过将深色背景移动到
::after
元素来实现。之前:
之后:
使用
z-index: -1
将其显示在文本下方。另外,作为一个建议,而不是通过切换
display
属性来显示文本,您可以切换opacity
,然后标题位置不会改变。下面是您提供的代码,上面有https://codepen.io/bortao/pen/YzJZPMp的更改
编辑:从OP输入后,他只是想使图像变暗,这可以通过css获得:
yqhsw0fo2#
它可能会帮助你。
您还可以选择设置最大高度的动画,但要做到这一点,您需要知道最大值。