向网页添加 CSS 淡入淡出

问题描述 投票:0回答:1

我正在为我正在处理的个人项目在网页中创建幻灯片,虽然我对 HTML 和 Javascript 了解一些,但我对 CSS 仍然不太熟悉。

就这一点而言,我在这个页面上找到了代码:

https://www.w3docs.com/tools/code-editor/3720

它实现了我想要实现的 95%,这很棒。但是,我想为我的图像添加淡入效果,而不仅仅是让它们在淡出后出现在屏幕上。

有人可以帮我添加正确的代码来实现这一点吗?一旦我看到什么去了哪里,我就可以从那时起弄清楚事情,但正如我所说,我还没有跟上 CSS 的速度以及它到底是如何工作的。

非常感谢!约瑟夫。

css css-transitions fadein
1个回答
0
投票

第1点:-链接:https://www.w3docs.com/tools/code-editor/3720您提供的是对您的问题的完整解释。但输出是在链接中实现的:https://www.w3docs.com/tools/code-editor/3720只能使用HTML,CSS3和JS!

第2点:-我还希望您阅读您想要实现的所提供链接输出的主要文章https://www.w3docs.com/snippets/css/how-to-create-an-image- slider-or-slideshow.html 通过阅读本文,您将更好地了解如何仅使用 HTM、CSS3 和 JS 实现图像滑块或任何类型的滑块。

第 3 点:- 如果您对此有任何疑问,请随时询问。我很乐意为您提供帮助。

第 4 点:- 下面给出的代码片段选自 https://www.w3docs.com/tools/code-editor/3720 在 CSS3 代码中,从第 84 行到第 105 行的代码块淡出-in code 如果您想在自定义项目中淡入图像,您可以选择该代码块并根据需要使用它 通过提供 “fade” 作为 html 中特定元素/标签的类

/* 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
    }
  }

let slideIndex = 0;
let timeoutId = null;
const slides = document.getElementsByClassName("mySlides");
const dots = document.getElementsByClassName("dot");

showSlides();

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

function plusSlides(step) {

  if (step < 0) {
    slideIndex -= 2;

    if (slideIndex < 0) {
      slideIndex = slides.length - 1;
    }
  }

  showSlides();
}

function showSlides() {
  for (let i = 0; i < slides.length; i++) {
    slides[i].style.display = "none";
    dots[i].classList.remove('active');
  }
  slideIndex++;
  if (slideIndex > slides.length) {
    slideIndex = 1
  }
  slides[slideIndex - 1].style.display = "block";
  dots[slideIndex - 1].classList.add('active');
  if (timeoutId) {
    clearTimeout(timeoutId);
  }
  timeoutId = setTimeout(showSlides, 5000); // Change image every 5 seconds
}
* {
  box-sizing: border-box
}

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

.mySlides {
  display: none
}

img {
  vertical-align: middle;
}

.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;
  user-select: none;
}


/* 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: #ffffff;
  font-size: 15px;
  padding: 8px 12px;
  position: absolute;
  bottom: 8px;
  width: 100%;
  text-align: center;
}


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

.numbertext {
  color: #ffffff;
  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: #999999;
  border-radius: 50%;
  display: inline-block;
  transition: background-color 0.6s ease;
}

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


/* 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
  }
}
<!DOCTYPE html>
<html>

<head>
  <title>Slideshow Images</title>
</head>

<body>
  <div class="slideshow-container">
    <div class="mySlides fade">
      <div class="numbertext">1 / 3</div>
      <img src="https://www.w3docs.com/uploads/media/default/0001/03/66cf5094908491e69d8187bcf934050a4800b37f.jpeg" style="width:100%">
      <div class="text">London, Ebgland</div>
    </div>
    <div class="mySlides fade">
      <div class="numbertext">2 / 3</div>
      <img src="https://www.w3docs.com/uploads/media/default/0001/03/b7d624354d5fa22e38b0ab1f9b905fb08ccc6a05.jpeg" style="width:100%">
      <div class="text">Sunset in Romania</div>
    </div>
    <div class="mySlides fade">
      <div class="numbertext">3 / 3</div>
      <img src="https://www.w3docs.com/uploads/media/default/0001/03/5bfad15a7fd24d448a48605baf52655a5bbe5a71.jpeg" style="width:100%">
      <div class="text">New York, USA</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(0)"></span>
    <span class="dot" onclick="currentSlide(1)"></span>
    <span class="dot" onclick="currentSlide(2)"></span>
  </div>
</body>

</html>

© www.soinside.com 2019 - 2024. All rights reserved.