Javascript 淡入叠加块,3秒后淡出,然后悬停淡入

Javascript 淡入叠加块,3秒后淡出,然后悬停淡入,javascript,html,css,Javascript,Html,Css,我试图在图像上创建一个覆盖层,这样覆盖层在图像可见时应在0.25秒内淡入,然后在3秒钟后淡出。在悬停事件中,覆盖应再次淡入,然后在光标不再悬停时淡出。我如何实现上述目标?我有以下资料: .img-container { position: relative; } .img-container img { display: block; width: 100%; } .overlay { position: absolute; top: 50; left: 0;

我试图在图像上创建一个覆盖层,这样覆盖层在图像可见时应在0.25秒内淡入,然后在3秒钟后淡出。在悬停事件中,覆盖应再次淡入,然后在光标不再悬停时淡出。我如何实现上述目标?我有以下资料:

.img-container {
  position: relative;
}

.img-container img {
  display: block;
  width: 100%;
}

.overlay {
  position: absolute;
  top: 50; 
  left: 0; 
  right: 0; 
  bottom: 0;
  width: 30%;

  text-align: center;
  opacity: 0;
  transition: opacity 0.25s; /* fade-in over s time */
  /* background: black; */
  /* color: white; */
  /*display: flex;
  align-items: center;
  justify-content: center;*/
  font-size: 1em; 
  background-color: rgba(0,0,0,0); /* Black background with opacity */
}

.img-container:hover .overlay {
  opacity: 1;
}

.image {
  display: block;
  width: 100%;
  height: auto;
}
我还想在幻灯片中实现同样的效果:

<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<style>
* {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: 13px;
  width: 13px;
  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}
}
</style>
</head>
<body>

<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>

<script>
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";
}
</script>

</body>
</html> 

*{框大小:边框框}
正文{字体系列:Verdana,无衬线;边距:0}
.mySlides{display:none}
/*幻灯片容器*/
.slideshow容器{
最大宽度:1000px;
位置:相对位置;
保证金:自动;
}
/*下一步和上一步按钮*/
.prev,.next{
光标:指针;
位置:绝对位置;
最高:50%;
宽度:自动;
填充:16px;
利润上限:-22px;
颜色:白色;
字体大小:粗体;
字号:18px;
过渡:0.6s缓解;
边界半径:0 3px 3px 0;
}
/*将“下一步按钮”置于右侧*/
.下一个{
右:0;
边界半径:3px 0 3px;
}
/*在悬停时,添加一个黑色背景色,带有一点透明*/
.prev:悬停,.next:悬停{
背景色:rgba(0,0,0,0.8);
}
/*标题文本*/
.文本{
颜色:#F2F2;
字体大小:15px;
填充:8px 12px;
位置:绝对位置;
底部:8px;
宽度:100%;
文本对齐:居中;
}
/*数字文本(1/3等)*/
.numbertext{
颜色:#F2F2;
字体大小:12px;
填充:8px 12px;
位置:绝对位置;
排名:0;
}
/*点/子弹/指示器*/
多特先生{
光标:指针;
高度:13px;
宽度:13px;
边际:0.2px;
背景色:#bbb;
边界半径:50%;
显示:内联块;
过渡:背景色0.6s;
}
.active,.dot:悬停{
背景色:#717171;
}
/*褪色动画*/
.褪色{
-webkit动画名称:淡入淡出;
-webkit动画持续时间:1.5s;
动画名称:淡入淡出;
动画持续时间:1.5s;
}
@-webkit关键帧淡入淡出{
来自{opacity:.4}
到{opacity:1}
}
@关键帧淡入淡出{
来自{opacity:.4}
到{opacity:1}
}
/*在较小的屏幕上,减小文本大小*/
@仅介质屏幕和(最大宽度:300px){
.prev、.next、.text{font size:11px}
}
1/3
标题文本
2/3
标题二
3/3
标题三
❮
❯

var slideIndex=1; 放映幻灯片(幻灯片索引); 函数加滑块(n){ 放映幻灯片(幻灯片索引+=n); } 功能当前幻灯片(n){ 放映幻灯片(幻灯片索引=n); } 功能演示幻灯片(n){ var i; var slides=document.getElementsByClassName(“mySlides”); var dots=document.getElementsByClassName(“dot”); 如果(n>slides.length){slideIndex=1} 如果(n<1){slideIndex=slides.length} 对于(i=0;i
您可以使用transition:

.container{
宽度:200px;
高度:200px;
边框:1px纯红;
过渡色:背景色3s;
背景颜色:绿色;
}
.容器:悬停{
背景颜色:黄色;
过渡:背景色0.25s;
}