简体   繁体   English

如何仅使用纯JavaScript对滚动元素进行简单的不透明度更改?

[英]how to do a simple opacity change of an element on scroll with only pure javascript?

I am trying to have elements with opacity set to 0 change to 1 when brought into the user's view. 我试图将不透明度设置为0的元素带入用户视图时更改为1。 how do I do this using only pure javascript? 我该如何仅使用纯JavaScript? NO JQUERY, please. 请不要。 Thank you in advance for any help offered. 预先感谢您提供的任何帮助。

I have tried adding a class and removing a class when in view and nothing is working. 我尝试添加一个类并在查看时删除一个类,但没有任何效果。 This is a very simple task and I am just not sure what to do here. 这是一个非常简单的任务,我不确定在这里做什么。 I have sought out documentation but all that I have found either include jquery or are more complicated than what I am looking to do. 我已经查找了文档,但是发现的所有内容要么包含jquery,要么比我想要做的要复杂。

<div class="outer">

        <div class="booger" ></div>

        <div class="booger"></div>

        <div class="booger"></div>

        <div class="booger"></div>

        <div class="booger"></div>

        <div class="booger"></div>

        <div class="booger"></div>

        <div class="booger"></div>

        <div class="booger"></div>
</div>


body {
box-sizing: border-box;

margin: 0;
padding: 0;
text-align: center;

text-decoration: none;
outline: none;
}

.outer {
width: 100%;
min-height: 100vh;
background-color: grey;

}

.booger {
display: inline-block;
width: 49%;

margin: 50px 30px;
padding: 100px 0;
background-color: darkgray;
opacity: 0;
}

.boogers {
opacity: 1;
} 

var toBeShown = document.querySelectorAll(".booger");

function showIt() {

const scrolled = (window.scrollY + window.innerHeight) -                        
(toBeShown.height/2);

const imageBottom = scrolled.offsetTop + scrolled.height;
const isHalfShown = scrolled > scrolled.offsetTop;
const isNotScrolledPast = window.scrollY < imageBottom;

if (isHalfShown && isNotScrolledPast) {
scrolled.classList.add('boogers');
} else {
scrolled.classList.remove('boogers');
}
}

window.addEventListener('scroll', showIt);

You have a couple of problems. 你有几个问题。 The code below sort of does what you want, but the math to determine when a div is shown is not 100% correct. 下面的代码可以满足您的要求,但是用于确定何时显示div的数学方法并非100%正确。 I'll leave it up to you to fix that. 我将由您自己解决。

The main problem is that document.querySelectorAll returns an array of elements . 主要的问题是document.querySelectorAll返回一个元素数组 So you need to process that array of elements, checking each one to see if it is in view, and then adding / removing classes for just that element. 因此,您需要处理该元素数组,检查每个元素数组是否在视图中,然后为该元素添加/删除类。

Also, there is no height attribute for elements. 此外,元素没有height属性。 Use console.log liberally to explore what properties are available and which one suits your purpose. 自由地使用console.log来探索哪些属性可用,哪些适合您的目的。

 var toBeShown = document.querySelectorAll(".booger"); function showIt() { toBeShown.forEach(function(elem) { const scrolled = (window.scrollY + window.innerHeight) - (elem.offsetTop/2); const imageBottom = elem.offsetTop + scrolled; const isHalfShown = scrolled > elem.offsetTop; const isNotScrolledPast = window.scrollY < imageBottom; if (isHalfShown && isNotScrolledPast) { elem.classList.add('boogers'); } else { elem.classList.remove('boogers'); } }); } window.addEventListener('scroll', showIt); 
 body { box-sizing: border-box; margin: 0; padding: 0; text-align: center; text-decoration: none; outline: none; } .outer { width: 100%; min-height: 100vh; background-color: grey; } .booger { display: inline-block; width: 49%; margin: 50px 30px; padding: 100px 0; background-color: darkgray; opacity: 0.3; } .boogers { opacity: 1; } 
 <div class="outer"> <div class="booger" >hay</div> <div class="booger">b</div> <div class="booger">c</div> <div class="booger">d</div> <div class="booger">e</div> <div class="booger">f</div> <div class="booger">gee</div> <div class="booger">haytch</div> <div class="booger">hi</div> </div> 

I agree with Todd on all the reasons he cited, but I think the logic can be cleaned up quite a bit. 对于托德所列举的所有原因,我都表示同意,但是我认为可以对逻辑进行相当多的整理。

All you want to watch for is when the top of the item scrolls into the center of the viewport, add an class, when it is below, remove said class. 您要注意的是,当项目顶部滚动到视口的中心时,添加一个类,当它位于下面时,删除该类。

I put together a codepen to demonstrate. 我整理了一个Codepen进行演示。

https://codepen.io/bickle66/pen/YbamYq https://codepen.io/bickle66/pen/YbamYq

function showIt() {
  const toBeShown = document.querySelectorAll(".booger"); // consider adding :not(.scrolled) to selector to reduce the number of iterations if you don't want to support scrolling up

  // consider taking this outside of the loop and resetting it on window resize to optimize the loop
  const halfScreen = window.innerHeight / 2;

  toBeShown.forEach((item, i) => {
    const scrolled = (window.scrollY + window.innerHeight);// - (item.offsetHeight/2);

    if (item.offsetTop - window.scrollY < halfScreen) {
      item.classList.add('scrolled');
    } else {
      item.classList.remove('scrolled');
    }
  })

}

window.addEventListener('scroll', showIt);

Although I love your class naming standard, ;-) I changed the boogers class to scrolled to be a bit more obvious on it's meaning 尽管我喜欢您的类命名标准,;-)我还是将boogers类更改为scrolled ,以使其含义更明显

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM