简体   繁体   English

使用getBoundingClientRect()处理具有相同类的多个元素?

[英]Using getBoundingClientRect() for multiple elements with the same class?

I'm trying to add sticky navigation to my website that will change as it scrolls over different sections. 我正在尝试将粘性导航添加到我的网站,该导航会在滚动到不同部分时发生变化。 When scrolling over a section with the class .dark , it should change the logo and text colour to white.. otherwise black. 当滚动类为.dark的部分时,应将徽标和文本颜色更改为白色,否则为黑色。

The javascript I've been using is below but this only seems to apply to the first element with the class .dark , how can I adapt this to target all elements with the same class? 我一直在使用的javascript如下,但这似乎仅适用于类.dark的第一个元素,我如何才能使其适应于具有相同类的所有元素?

window.addEventListener('scroll', function () {

        var section = document.querySelector('.dark').getBoundingClientRect(),
            logo = document.querySelector('#logo-container').getBoundingClientRect();

          if (section.top <= logo.top + logo.height && section.top + section.height > logo.top) {
            document.getElementById('logo-container').classList.add('white-logo');
            document.getElementById('navholder').style.color = "#fff";

          } else {
            document.getElementById('logo-container').classList.remove('white-logo');
            document.getElementById('navholder').style.color = "#111";
          }

     });

I apologise if this is an obvious question, I'm not too knowledgeable when it comes to javascript! 如果这是一个显而易见的问题,我深表歉意,我对javascript的了解也不多! I've tried to look for a solution to this but have not had much success.. any help would be massively appreciated. 我试图寻找一种解决方案,但没有取得太大的成功。任何帮助将不胜感激。

If you break this out into several functions, it makes life easier. 如果将其分解为几个功能,则可以使生活更轻松。 You can check if the logo is is any of the sections, and then set its class accordingly: 您可以检查徽标是否属于任何部分,然后相应地设置其类别:

 const setLogoBlackStatus = status => { if (status) { document.getElementById('logo-container').classList.add('black-logo'); document.getElementById('logo-container').classList.remove('white-logo'); } else { document.getElementById('logo-container').classList.add('white-logo'); document.getElementById('logo-container').classList.remove('black-logo'); } } const logoIsInSection = logo => sectionRect => sectionRect.top <= logo.top + logo.height && sectionRect.top + sectionRect.height > logo.top window.addEventListener('scroll', function() { var sectionRects = [...document.querySelectorAll('.dark')] .map(el => el.getBoundingClientRect()); var logo = document.querySelector('#logo-container').getBoundingClientRect(); var logoInAnySections = sectionRects .some(logoIsInSection(logo)) setLogoBlackStatus(!logoInAnySections); }); 
 img { width: 50px; position: fixed; top: 20vw; left: 20vw; z-index: 1; } .white-logo { filter: invert(90%); } .section { width: 100%; height: 300px; } .dark { background-color: rgba(20, 20, 30); } .white { background-color: white; } 
 <img src="https://dev.w3.org/SVG/tools/svgweb/samples/svg-files/cc.svg" id="logo-container"/> <div class="section white"></div> <div class="section dark"></div> <div class="section white"></div> <div class="section dark"></div> 

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

相关问题 如何通过 getBoundingClientRect() 设置多个元素样式 - How to set multiple elements style via getBoundingClientRect() 与使用getBoundingClientRect的先前放置位置相比,元素的位置不会保持不变 - The position of the elements do not remain the same compared to their previous placed position using getBoundingClientRect Javascript getBoundingClientRect()-适用于类的多个实例 - Javascript getBoundingClientRect() - apply to multiple instances of class 使用同一个类使用补间触发多个元素 - trigger multiple elements using tween using same class 切换具有相同 class 的多个元素 - Toggle multiple elements with same class querySelectorAll 和具有相同类的多个元素 - querySelectorAll and multiple elements with the same class .mouseleave 多个元素相同 class - .mouseleave multiple elements with the same class 如何使用jquery检查多个元素是否具有相同的类? - How to check multiple elements has same class using jquery? 使用 GTM 中的 JS 变量获取具有相同 class 名称的多个元素 - Grab multiple elements with the same class name using a JS Variable in GTM 使用jQuery设置元素的html内容,但具有同一类的多个元素 - Set html contents of an element using jQuery but with multiple elements of the same class
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM