javascript - 带有淡入淡出到黑色过渡的图像轮播

标签 javascript jquery html css jquery-animate

我需要制作一个带有淡入淡出过渡到黑色的旋转木马,或者更好的是像 #2B303A 这样的深灰色。我在网上发现这段代码运行良好,唯一的问题是淡入淡出效果是一种非常明亮的白色,我不喜欢它,而且它很烦人。

如何让它在图像之间淡化为黑色?

$(document).ready(function() {

  //Carousel
  var vet_url = ["https://i.imgur.com/Bb39Qpp.jpg", "https://images.wallpaperscraft.com/image/palms_road_marking_123929_1920x1080.jpg"];
  var len = vet_url.length;
  var i = 0;

  function swapBackgrounds() {
    $("#background").animate({
      opacity: 0
    }, 700, function() {
      $($("#background")).css('background-image', 'url(' + vet_url[(i + 1) % len] + ')').animate({
        opacity: 1
      }, 700);
    });
    i++;
  }
  setInterval(swapBackgrounds, 10000);
});
#background {
  background-color: #2B303A;
  position: absolute;
  z-index: 1;
  min-height: 100%;
  min-width: 100%;
  background-image: url("https://images.wallpaperscraft.com/image/palms_road_marking_123929_1920x1080.jpg");
  background-position: center;
  background-repeat: no-repeat;
  background-size: cover;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<div id="background"></div>

View on JSFiddle

最佳答案

您需要在 html 元素上执行此操作,至少在您的测试用例中。

html
{
 background-color:#2B303A;
}

关于javascript - 带有淡入淡出到黑色过渡的图像轮播,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52843129/

相关文章:

javascript - 当名称和值之间有空格字符时如何获取属性数组?

javascript - Node js 需要代码而不是文件

jquery - 基本的 jquery slider 图像大小不可调整

javascript - 如何在函数外访问javascript变量

html - Dhtml 菜单导航问题

javascript - 为什么 $watch 没有像我预期的那样工作 - Angular

javascript - 如何从javascript设置html表单的属性

jquery - 如何使用 jQuery animate() 方法使 div 左右移动?

html - 如何阻止我的 css 声明被覆盖

javascript - 使用 Angular2 根据选择框值显示或隐藏内容 div