【发布时间】:2020-10-03 12:09:49
【问题描述】:
我正在创建一个带有导航栏和一些 JavaScript 的网站,这些 JavaScript 设置 div(类:bubble)的位置和背景渐变,根据用户滚动到的位置突出显示导航栏项目。这是我的代码的每一点:
const sections = document.querySelectorAll('selection');
const bubble = document.querySelector('.bubble');
const gradient = 'linear-gradient(to right top, #000428, #004e92)';
const options = {
threshold: 0.7
}
let observer = new IntersectionObserver(navCheck, options);
function navCheck(entries) {
entries.forEach(entry => {
const className = entry.target.className;
const activeAnchor = document.querySelector(`[data-page=${className}]`);
const gradientIndex = entry.target.getAttribute(`data-index`)
const coords = activeAnchor.getBoundingClientRect();
const directions = {
height: coords.height,
width: coords.width,
top: coords.top,
left: coords.left
};
if (entry.isIntersecting) {
bubble.style.setProperty('left', `${directions.left}px`)
bubble.style.setProperty('top', `${directions.top}px`)
bubble.style.setProperty('width', `${directions.width}px`)
bubble.style.setProperty('height', `${directions.height}px`)
}
});
}
sections.forEach(section => {
observer.observe(section);
});
现在,我是 JavaScript 的超级新手,不知道为什么这不起作用。谁能帮帮我?
【问题讨论】:
-
你检查
if(entry.isIntersecting) {是否被执行了吗?
标签: javascript styles observers