gpt4 book ai didi

javascript - 使用 Bootstrap 文本轮播淡入和淡出背景

转载 作者:行者123 更新时间:2023-12-04 08:38:11 26 4
gpt4 key购买 nike

我有三个背景图像,我想以 5 秒的间隔淡入和淡出。我还利用 native 引导轮播组件以 5 秒的间隔将文本滑入和滑出,以与不断变化和淡出的背景图像相匹配。

场景

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

  • 问题 2:bootstrap 轮播组件有一个指示器,单击该指示器后,它会切换并滑动到该轮播部分中的文本。我希望在单击轮播指示器时更改背景图像。也就是说,如果轮播指示器 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>

最佳答案

您不需要自己对图像施以魔法的所有逻辑。您可以使用 carousel api on('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

关于javascript - 使用 Bootstrap 文本轮播淡入和淡出背景,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64695070/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com