在函数中使用getElementsByClassName选择所有元素,不起作用

时间:2019-04-22 07:46:44

标签: javascript

我有一个使用Javascript的简单的“滚动淡出文本”效果。

这是一个工作的小提琴:https://jsfiddle.net/4opjh5ef/1/

我想让效果应用于类fade的所有元素。但是,尝试在函数内部循环它似乎不起作用。

这是无效代码:

var fadeOut = document.getElementsByClassName('fade');
var fadeOutParent = fadeOut.parentElement.scrollHeight;


function scrolled() {

    for (var i = 0; i < fadeOut.length; i++) {
      	fadeOut[i].style.opacity = 1-((window.pageYOffset)/(fadeOutParent/2));
    }

}

window.addEventListener('scroll', scrolled);

有什么主意吗?

2 个答案:

答案 0 :(得分:1)

每个元素应该有不同的fadeOutParent,因为每个元素都有不同的父元素。您应该在循环内分配fadeOutParent

var fadeOut = document.getElementsByClassName('fade');
function scrolled() {
  for (var i = 0; i < fadeOut.length; i++) {
        var fadeOutParent = fadeOut[i].parentElement.scrollHeight;
      	fadeOut[i].style.opacity = (1-((window.pageYOffset)/(fadeOutParent/2)))
    }
}

window.addEventListener('scroll', scrolled);
body {
  margin: 0;
  height: 1000px;
  font-family: sans-serif;
  color: #333;
}

.content {
  padding: 10%;
}

p {
  line-height: 1.75;
}

.top {
  margin: 0;
  position: relative;
  width: 100%;
  background-color: #aaa;
  height: 300px;
  opacity: 1;
  text-align: center;
  font-family: 'helvetica';
  color: #fff;
}

.title {
  font-size: 100px;
  font-weight: 700;
  font-size: 100px;
  font-weight: 700;
  position: absolute;
  top: 60%;
  left: 100px;
  /* gak perlu, udah cukup smooth
  transition: opacity 0.5s ease;
  */
}
.span {
  font-size: 50px;
  font-weight: 300;
  position: absolute;
  top: 77%;
  right: 100px;
}
<div class="container">

  <div class="top" id="top">
    <div class="title fade" id="title">
      Fade Away
    </div>
    <div class="span fade">
      Subtitle
    </div>
  </div>

  <div class="content">
    <p>
      What's scrolling is actually .content, not body.
    </p>
    <p>
      White paper segmentation families granular big data dynamic natural resources energize, vibrant families social return on investment human-centered radical. Inclusive philanthropy design thinking agile, we must stand up ecosystem; support social impact
      efficient game-changer ecosystem and correlation shared value. Ideate state of play technology circular, disrupt innovation paradigm movements change-makers. Natural resources triple bottom line bandwidth movements venture philanthropy incubator
      energize effective problem-solvers uplift. Greenwashing we must stand up segmentation, program area; resilient venture philanthropy, academic expose the truth entrepreneur activate transparent venture philanthropy empower communities deep dive.
      NGO collective impact synergy initiative, bandwidth, storytelling revolutionary inspiring, our work segmentation.
    </p>
    <p>
      Deep dive the resistance, problem-solvers impact investing social entrepreneur indicators inspiring energize. Boots on the ground our work systems thinking think tank innovation. Green space catalyze blended value sustainable empower communities thought
      partnership.

    </p>
    <p>
      NGO disrupt, expose the truth save the world, black lives matter challenges and opportunities thought leader movements efficient theory of change cultivate activate strategy LGBTQ+. Paradigm; emerging because, a; social enterprise strategy accessibility.
      Design thinking, segmentation; relief, justice her body her rights gender rights. Catalyze parse inclusion thought leader, overcome injustice, expose the truth collective impact silo ideate.

    </p>
    <p>
      Transparent benefit corporation social impact data families scale and impact social capital a shared unit of analysis. Uplift dynamic; movements, co-creation, co-create, expose the truth segmentation data technology collaborate, academic cultivate collective
      impact accessibility. Benefit corporation theory of change, entrepreneur venture philanthropy move the needle, indicators, segmentation co-creation revolutionary revolutionary co-create deep dive gender rights. NGO initiative; vibrant; our work
      inspire; inspire communities, rubric, gender rights collaborate gender rights data deep dive.

    </p>
    <p>
      To innovate outcomes justice benefit corporation, policymaker impact investing. Equal opportunity green space empower communities strategy, co-create compelling, gender rights shared vocabulary shared value transparent. Uplift, NGO resist, vibrant, mass
      incarceration collaborative cities vibrant and. Shared vocabulary paradigm, boots on the ground program areas change-makers, corporate social responsibility data strategy because replicable expose the truth human-centered. Program area storytelling
      social innovation preliminary thinking social innovation storytelling correlation. Then; ecosystem granular impact impact expose the truth outcomes but entrepreneur movements.
    </p>

  </div>

</div>

我建议您使用querySelectorAll()代替getElementsByClass()

答案 1 :(得分:0)

您的问题是您试图选择parentElement中的NodeList,这是不可能的。

在这里,尝试将fadeOutParent变量移到for循环中:

var fadeOut = document.getElementsByClassName("fade");
function scrolled() {
  for (var i = 0; i < fadeOut.length; i++) {
    var fadeOutParent = fadeOut[i].parentElement.scrollHeight;
    fadeOut[i].style.opacity = 1 - ((window.pageYOffset) / (fadeOutParent / 2));
  }
}
window.addEventListener("scroll", scrolled);

祝你好运。