【问题标题】:Fade in and fade out background with bootstrap text carousel使用引导文本轮播淡入淡出背景
【发布时间】:2021-02-18 00:47:45
【问题描述】:

我想要以 5 秒的间隔淡入和淡出三个背景图像。我还利用原生引导轮播组件以 5 秒的间隔将文本滑入和滑出,以匹配变化和褪色的背景图像。

场景

  • 问题 1:第一个背景图像将是页面加载时的默认背景图像。 5 秒后,第二个背景图像消失,这也与轮播滑动到第二个文本的时间一致。再过 5 秒后,第三个背景图像淡入,轮播滑到下一个文本。

  • 问题 2:引导轮播组件有一个指示器,当单击该指示器时,它会切换并滑动到该轮播部分中的文本。我希望在单击轮播指示器时更改背景图像。也就是说,如果单击轮播指示器 1,则背景图像应淡入到第一个背景图像。这应该适用于第二个和第三个轮播指示器及其各自的背景图像。如果当前是活动的轮播项目和背景图像,则背景图像不应更改并淡化为单击的图像。

现状

  • 我已经能够使用 jquery 和我在 stackoverflow 中得到的解决方案来实现具有褪色效果的背景图像。我还使用带有选项 (data-interval="5000") 的引导轮播组件完成了文本轮播,它将切换时间设置为 5 秒,以匹配背景图像淡入和淡出的相同时间间隔,但仍然存在明显的小延迟。如何消除明显的延迟?

  • 图像有时会在褪色前闪烁。有没有更好更合适的方法来实现不闪退的褪色效果?

  • 即使我在单击时运行该功能,单击轮播指示器也不会切换并将背景图像淡入下一个。如何以正确的方式实现?

这是我尝试过的:

var bgImageArray = ["1_ozdgvm.jpg", "2_vjtjfy.jpg", "3_oxpdx2.jpg"],
  base = "https://res.cloudinary.com/iolamide/image/upload/v1604569872/home_banner_",
  secs = 5;
bgImageArray.forEach(function(img) {
  new Image().src = base + img;
  // caches images, avoiding white flash between background replacements
});

function backgroundSequence() {
  window.clearTimeout();
  var k = 0;
  for (i = 0; i < bgImageArray.length; i++) {
    setTimeout(function() {
      document.getElementById('animated-bg').style.backgroundImage = "url(" + base + bgImageArray[k] + ")";
      // document.getElementById('animated-bg').style.backgroundSize ="cover";
      if ((k + 1) === bgImageArray.length) {
        setTimeout(function() {
          backgroundSequence()
        }, (secs * 1000))
      } else {
        k++;
      }
    }, (secs * 1000) * i)
  }
}

backgroundSequence();


$('.carousel-item').click(function() {
  backgroundSequence();
})
*,
*::before,
*::after {
  margin: 0;
  padding: 0;
}

html {
  box-sizing: border-box;
}

body {
  box-sizing: inherit;
  color: #fff !important;
}

.animated-bg {
  background-image: url("https://res.cloudinary.com/iolamide/image/upload/v1604569872/home_banner_1_ozdgvm.jpg");
  background-size: cover;
  background-position: center;
  background-repeat: no-repeat;
  transition: background 1s;
  height: 694px;
  position: relative;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
}

.animated-bg:before {
  content: "";
  position: absolute;
  top: 0;
  bottom: 0;
  left: 0;
  right: 0;
}

.container {
  padding: 3rem;
  border-radius: 20px;
  width: 700px;
  background: rgba(0, 0, 0, 0.3);
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  text-align: center;
}

.text-top p {
  width: 400px;
  margin: 30px auto;
  text-align: center;
}

h1 {
  margin-top: 0;
  text-align: center;
  font-weight: 600;
}

.carousel-item p {
  width: 330px;
  text-align: center;
}

.carousel-indicators {
  bottom: -50px !important;
}

@media only screen and (max-width: 800px) {
  .container {
    padding: 2rem;
    width: 90%;
  }
}

@media only screen and (max-width: 500px) {
  .text-top p {
    width: 80%;
  }
}
<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" rel="stylesheet" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js"></script>

<div class="animated-bg" id="animated-bg">
  <div class="container">
    <div class="text-top" id="texttop">
      <h1>Crossfade and Text Carousel</h1>
      <p>Fade background image and swipe text at the same time. Also fade the background image to the one that matches the carousel's text when the carousel's indicator is clicked</p>
    </div>
    <div id="carouselExampleIndicators" class="carousel slide" data-ride="carousel" data-interval="5000">
      <ol class="carousel-indicators">
        <li data-target="#carouselExampleIndicators" data-slide-to="0" class="active"></li>
        <li data-target="#carouselExampleIndicators" data-slide-to="1"></li>
        <li data-target="#carouselExampleIndicators" data-slide-to="2"></li>
      </ol>
      <div class="carousel-inner">
        <div class="carousel-item active">
          <p>
            Carousel Text One
          </p>
        </div>
        <div class="carousel-item">
          <p>Carousel Text Two</p>
        </div>
        <div class="carousel-item">
          <p>Carousel Text Three</p>
        </div>
      </div>
    </div>
  </div>
</div>

【问题讨论】:

    标签: javascript html jquery css bootstrap-4


    【解决方案1】:

    你不需要自己去魔化图像的所有逻辑。您可以通过轮播apion('slide.bs.carousel')收听幻灯片变化并相应地切换图像。

    const bgImageArray = ["1_ozdgvm.jpg", "2_vjtjfy.jpg", "3_oxpdx2.jpg"],
      base = "https://res.cloudinary.com/iolamide/image/upload/v1604569872/home_banner_";
      
    bgImageArray.forEach(function(img) {
      new Image().src = base + img;
      // caches images, avoiding white flash between background replacements
    });
      
    $('.carousel'). on('slide.bs.carousel', function(e) {
      $('.animated-bg').css({
        backgroundImage: `url(${base}${bgImageArray[e.to]})`
      });
    });
    *,
    *::before,
    *::after {
      margin: 0;
      padding: 0;
    }
    
    html {
      box-sizing: border-box;
    }
    
    body {
      box-sizing: inherit;
      color: #fff !important;
    }
    
    .animated-bg {
      background-image: url("https://res.cloudinary.com/iolamide/image/upload/v1604569872/home_banner_1_ozdgvm.jpg");
      background-size: cover;
      background-position: center;
      background-repeat: no-repeat;
      transition: background 1s;
      height: 694px;
      position: relative;
      display: flex;
      flex-direction: column;
      align-items: center;
      justify-content: center;
    }
    
    .animated-bg:before {
      content: "";
      position: absolute;
      top: 0;
      bottom: 0;
      left: 0;
      right: 0;
    }
    
    .container {
      padding: 3rem;
      border-radius: 20px;
      width: 700px;
      background: rgba(0, 0, 0, 0.3);
      display: flex;
      flex-direction: column;
      align-items: center;
      justify-content: center;
      text-align: center;
    }
    
    .text-top p {
      width: 400px;
      margin: 30px auto;
      text-align: center;
    }
    
    h1 {
      margin-top: 0;
      text-align: center;
      font-weight: 600;
    }
    
    .carousel-item p {
      width: 330px;
      text-align: center;
    }
    
    .carousel-indicators {
      bottom: -50px !important;
    }
    
    @media only screen and (max-width: 800px) {
      .container {
        padding: 2rem;
        width: 90%;
      }
    }
    
    @media only screen and (max-width: 500px) {
      .text-top p {
        width: 80%;
      }
    }
    <link href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" rel="stylesheet" />
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js"></script>
    <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js"></script>
    
    <div class="animated-bg" id="animated-bg">
      <div class="container">
        <div class="text-top" id="texttop">
          <h1>Crossfade and Text Carousel</h1>
          <p>Fade background image and swipe text at the same time. Also fade the background image to the one that matches the carousel's text when the carousel's indicator is clicked</p>
        </div>
        <div id="carouselExampleIndicators" class="carousel slide" data-ride="carousel" data-interval="5000">
          <ol class="carousel-indicators">
            <li data-target="#carouselExampleIndicators" data-slide-to="0" class="active"></li>
            <li data-target="#carouselExampleIndicators" data-slide-to="1"></li>
            <li data-target="#carouselExampleIndicators" data-slide-to="2"></li>
          </ol>
          <div class="carousel-inner">
            <div class="carousel-item active">
              <p>
                Carousel Text One
              </p>
            </div>
            <div class="carousel-item">
              <p>Carousel Text Two</p>
            </div>
            <div class="carousel-item">
              <p>Carousel Text Three</p>
            </div>
          </div>
        </div>
      </div>
    </div>

    https://jsbin.com/mewiwuv/edit?js,output

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-10-27
      • 2017-10-15
      • 2011-11-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多