gpt4 book ai didi

javascript - CSS 动画 - 带有图像的垂直选框 - 每次特定图像可见时淡入新的背景颜色

转载 作者:行者123 更新时间:2023-11-28 13:59:42 25 4
gpt4 key购买 nike

我有一个垂直选取框,里面有静态内容。我正在尝试实现下面 JavaScript 注释中概述的内容 - 根据刚刚进入 View 的图像淡入新的背景颜色。

在这里画笔:https://codepen.io/TraeRegan/pen/mdbKRXY

我已经尝试了各种 jQuery 插件,这些插件适用于当某些元素进入视口(viewport)时用户滚动触发的动画,但我无法让它们在 CSS 动画元素变得可见时检测它们。

JavaScript

$(function() {
var image1_bg_color = '#317a5c';
var image2_bg_color = '#dedede';
var image3_bg_color = '#ff0000';
var image4_bg_color = '#000000';

// pseudo-code...

// When #image2 becomes visible fade .container bg color from image1_bg_color to image2_bg_color

// When #image3 becomes visible fade .container bg color from image2_bg_color to image3_bg_color

// When #image4 becomes visible fade .container bg color from image3_bg_color to image4_bg_color

// When #image1 re-enters view fade .container bg color from image4_bg_color to image1_bg_color

// etc.
});

HTML

<div class="container">
<div class="marquee">
<img src="https://dummyimage.com/320x240/000/fff.gif&text=1" id="image1">
<p>This is some text about image 1.</p>

<img src="https://dummyimage.com/320x240/000/fff.gif&text=2" id="image2">
<p>This is some text about image 2.</p>

<img src="https://dummyimage.com/320x240/000/fff.gif&text=3" id="image3">
<p>This is some text about image 3</p>

<img src="https://dummyimage.com/320x240/000/fff.gif&text=4" id="image4">
<p>This is some text about image 4.</p>

</div>
</div>

CSS

.container {
width: 640px;
height: 480px;
margin: 0 auto;
overflow: hidden;
background: white;
position: relative;
box-sizing: border-box;
background-color: #317a5c;
}

.marquee {
width: 320px;
top: 480px;
position: relative;
box-sizing: border-box;
animation: marquee 30s linear infinite;
margin: 0 auto;
text-align: center;
color: #ffffff;
}

@keyframes marquee {
from {
transform: translateY(0);
}
to {
transform: translateY(-150%);
}
}

最佳答案

您必须在带有 data 属性的 img 标签中指定框背景颜色。因此,当图像到达框中的预期位置时,我们可以获得背景颜色的值。

$(function() {
var boxTop = $('.container').offset().top + ($('.container').innerHeight()/2);
setInterval(function(){
// console.clear();
$('.marquee img').each(function(index, el) {
var imgTop = $(this).offset().top + ($(this).innerHeight()/2);
var boxBg = $(this).attr('data-bgcolor');
// console.log(boxTop, imgTop, imgId);
if(imgTop <= boxTop){
$('.container').css('background-color', boxBg);
}
});
}, 100);
});
.container {
width: 640px;
height: 480px;
margin: 0 auto;
overflow: hidden;
background: white;
position: relative;
box-sizing: border-box;
background-color: #317a5c;
transition: all 0.5s ease-in-out;
}

.marquee {
width: 320px;
top: 480px;
position: relative;
box-sizing: border-box;
animation: marquee 30s linear infinite;
margin: 0 auto;
text-align: center;
color: #ffffff;
}

@keyframes marquee {
from {
transform: translateY(0);
}
to {
transform: translateY(-150%);
}
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="container">
<div class="marquee">
<img src="https://dummyimage.com/320x240/000/fff.gif&text=image 1" id="image1" data-bgcolor="#317a5c">
<p>This is some text about image 1.</p>

<img src="https://dummyimage.com/320x240/000/fff.gif&text=image 2" id="image2" data-bgcolor="#dedede">
<p>This is some text about image 2.</p>

<img src="https://dummyimage.com/320x240/000/fff.gif&text=image 3" id="image3" data-bgcolor="#ff0000">
<p>This is some text about image 3</p>

<img src="https://dummyimage.com/320x240/000/fff.gif&text=image 4" id="image4" data-bgcolor="#000000">
<p>This is some text about image 4.</p>

</div>
</div>

关于javascript - CSS 动画 - 带有图像的垂直选框 - 每次特定图像可见时淡入新的背景颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57914482/

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