javascript CSS转换关闭效果

cld4siwp  于 2023-04-28  发布在  Java
关注(0)|答案(2)|浏览(129)

我正在使用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

9jyewag0

9jyewag01#

您可以通过将深色背景移动到::after元素来实现。
之前:

.overlay {
  background-color: rgba(0, 0, 0, 0.5);
}

之后:

.overlay::after {
  content: "";
  position: absolute;
  z-index: -1;
  top: 0;
  left: 0;
  bottom: 0;
  right: 0;
  background-color: rgba(0, 0, 0, 0.5);
  transform: scale(.75);  
  transition: transform .25s ease-out;
}
.swiper-slide-active .overlay::after {
  transform: scale(1);  
}

使用z-index: -1将其显示在文本下方。
另外,作为一个建议,而不是通过切换display属性来显示文本,您可以切换opacity,然后标题位置不会改变。
下面是您提供的代码,上面有https://codepen.io/bortao/pen/YzJZPMp的更改
编辑:从OP输入后,他只是想使图像变暗,这可以通过css获得:

filter: brightness(50%);
yqhsw0fo

yqhsw0fo2#

它可能会帮助你。

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;
  align-items: center;
}

.swiper-slide {
  transition: all .25s ease-out;
  opacity: 1;
}

.swiper-slide-active+.swiper-slide~.swiper-slide {
  opacity: .35;
}

.swiper-slide img {
  max-width: 100%;
  transition: all .25s ease-out;
  transform: scale(.75);
  transform-origin: center;
}

.swiper-slide img::after {
  content: "";
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background-image: linear-gradient(120deg, #eaee44, #33d0ff);
  opacity: .7;
}

.swiper-slide-active img {
  transform: scale(1);
}

.overlay {
  position: absolute;
  top: 0;
  left: 0;
  bottom: 0;
  right: 0;
  color: #fff;
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  transform: scale(.75);
  transform-origin: center;
}

.overlay::after {
  content: "";
  position: absolute;
  z-index: -1;
  top: 0;
  left: 0;
  bottom: 0;
  right: 0;
  background-color: rgba(0, 0, 0, 0.5);
  color: #fff;
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  transform-origin: center;
  transform: scale(.75);
  transition: all .25s ease-out;
}

.swiper-slide-active .overlay::after {
  transform: scale(1);
}

.text {
  opacity: 0;
  text-align: center;
  padding: 0 15px;
  transition: all .25s ease-out;
}

.swiper-slide-active .text {
  opacity: 1;
}
<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">
      <img src="https://picsum.photos/600/900?image=10" alt="dummy-image">
      <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">
      <img src="https://picsum.photos/600/900?image=11" alt="dummy-image">
      <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">
      <img src="https://picsum.photos/600/900?image=12" alt="dummy-image">
      <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">
      <img src="https://picsum.photos/600/900?image=13" alt="dummy-image">
      <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">
      <img src="https://picsum.photos/600/900?image=14" alt="dummy-image">
      <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">
      <img src="https://picsum.photos/600/900?image=15" alt="dummy-image">
      <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">
      <img src="https://picsum.photos/600/900?image=16" alt="dummy-image">
      <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-slide">
      <img src="https://picsum.photos/600/900?image=17" alt="dummy-image">
      <div class="overlay">
        <div class="year">2004</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">
      <img src="https://picsum.photos/600/900?image=18" alt="dummy-image">
      <div class="overlay">
        <div class="year">2008</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">
      <img src="https://picsum.photos/600/900?image=19" alt="dummy-image">
      <div class="overlay">
        <div class="year">2012</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">
      <img src="https://picsum.photos/600/900?image=20" alt="dummy-image">
      <div class="overlay">
        <div class="year">2020</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>
  <div class="swiper-button-prev"></div>
  <div class="swiper-button-next"></div>
</div>

您还可以选择设置最大高度的动画,但要做到这一点,您需要知道最大值。

相关问题