[英]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.