我正在研究 javascript IntersectionObserver 属性。
我想从colors数组中获取颜色以将其放入entry.target.style.backgroundColor= col; //changing to background color to the color from colors array函数中action。但我得到的唯一一个是数组blue的最后一个。colors
如何从数组中获取每种颜色并使它们发挥作用?另外,向上滚动时是否可以将颜色恢复为原始背景颜色?
const sections = document.querySelectorAll('section');
const colors = ['green','brown', 'blue'];
for(let i=0; i < colors.length; i ++) {
col = colors[i];
}
const action = function (entries) {
entries.forEach(entry => {
if(entry.isIntersecting) {
entry.target.style.backgroundColor= col; //changing to background color to the color from colors array
} else {
return false; // going back to original background color???
}
});
}
const options = {
root: null,
rootMargin: "30% 0px",
threshold: 1
};
const observer = new IntersectionObserver(action, options);
sections.forEach(section => {
observer.observe(section);
});
header { height: 100vh; background: #ccc;}
.block {
position: relative;
width: 100%;
height: 100vh;
transition: .5s;
}
.block1 {background: #666;}
.block2 { background: #aaa;}
.block3 { background: #333;}
<header>header</header>
<section class="block block1">green</section>
<section class="block block2">brown</section>
<section class="block block3">blue</section>
Smart猫小萌
胡说叔叔
相关分类