【问题标题】:Getting color array to put it in the function获取颜色数组以将其放入函数中
【发布时间】:2020-08-13 02:39:33
【问题描述】:

我正在处理 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>

【问题讨论】:

  • 您想使用数组中的哪种颜色?现在,您的 for 循环将 colors 数组中的最后一项分配给 col
  • @Maaz Syed Adeeb 感谢您的评论。我想要每一种颜色。块 1 为绿色,块 2 为棕色,块 3 为蓝色。我不知道如何让每种颜色在函数中使用...

标签: javascript arrays function for-loop intersection-observer


【解决方案1】:

编辑:

根据IntersectionObserver api,我们不能调用takeRecords,因为它在回调中是空的(因为队列被刷新)(希望得到所有观察到的记录)

并且intersectionobserverentry 也不返回对观察到的节点的引用

因此我们可以回退到检索部分以从中获取当前条目索引

const sections = document.querySelectorAll('section');
const colors = ['green','brown', 'blue'];

const action = function (entries) {
  entries.forEach(entry => { 
    if(entry.isIntersecting) {
      // retrieve the entry's index from sections
      const i = [...sections].indexOf(entry.target)
      
      // or... traverse to its parent praying for all the observed entries to be there
      // console.log(entry.target.parentNode.querySelectorAll('section'))
      entry.target.style.backgroundColor= colors[i];  //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: 30vh;
  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>

【讨论】:

  • 感谢您的回答。但是,我只能通过这种方式获得绿色。这很奇怪,因为在从顶部打开页面到向下滚动时显示不同的方式,只显示绿色并在显示所有三种颜色的页面中间重新加载它。可能IntersectionObserver 和循环不是很好的匹配?也许我需要找出不同的方法。无论如何,谢谢。
  • 我已经更新了@user10991526 上面的代码我认为你确实想使用观察者。更好的选择可能是将一些 data-colorIndex 编码到您观察到的部分(然后您将从当前目标读取 colorIndex)。我建议的方法意味着要知道哪些部分已经被听过,这是相当侵入性的......
  • 哇!这正是我想要的。是的,我想在我的练习中使用观察者......我只关心颜色数组,但我需要获取要配对的部分索引。现在我明白你的意思了。谢谢你的帮助!!!
【解决方案2】:

实现它的一种方法是使用 CSS 类。因此,当元素相交时,添加一个 intersecting 类,如果不是,则将其删除。并具有匹配块的相应 CSS。我不太确定 IntersectionObserver 选项,但我已更改它们以让您了解此方法的工作原理。

const sections = document.querySelectorAll('section');

const action = function(entries) {
  entries.forEach(entry => {
    const elem = entry.target;
    if (entry.isIntersecting) {
      if (!elem.classList.contains("intersect")) {
        elem.classList.add("intersect");
      }
    } else {
      elem.classList.remove("intersect");
    }
  });
}

const options = {
//  root: null,
//    rootMargin: "30% 0px",
  threshold: 0.5
};

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;
}

.block1.intersect {
  background: green;
}

.block2 {
  background: #aaa;
}

.block2.intersect {
  background: brown;
}

.block3 {
  background: #333;
}

.block3.intersect {
  background: blue;
}
<header>header</header>
<section class="block block1">green</section>
<section class="block block2">brown</section>
<section class="block block3">blue</section>

【讨论】:

  • 非常感谢您的建议。我实际上已经尝试过你的方法。在这种情况下,我只是想以另一种方式尝试使用数组来改进 javascript。无论如何,非常感谢您的帮助!
猜你喜欢
  • 2013-05-05
  • 2012-10-02
  • 1970-01-01
  • 1970-01-01
  • 2014-10-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多