gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 19:37:24 25 4
gpt4 key购买 nike

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

这是一个工作 fiddle :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);

有什么想法吗?

最佳答案

您应该为每个元素设置不同的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()

关于javascript - 在函数中使用 getElementsByClassName 选择所有元素,但不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55790956/

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