如何使导航栏及其元素根据悬停和滚动而变化?

How to make navbar and its elements change based on hover and scrollY?

提问人:Patrycja Zborowska 提问时间:10/29/2023 最后编辑:Patrycja Zborowska 更新时间:10/29/2023 访问量:31

问:

嗨,我正在制作一个导航栏,该导航栏会根据悬停和 . 以下是要求:scrollY

when scrollY > 0 (That means, navbar not on top):
    navbar bg-color -> "#fff"    
    children (a, i, p tags) color -> "#000"    
    when child hovered:
       child color -> "ff7bac" 
and that is whiteNavbar() function, it is done and it's working. now I need to do: 
when scrollY <= 0 (navbar on top):
    navbar bg-color -> rgba(0, 0, 0, 0.2)
    children color -> "#fff"
    when navbar hovered:
        navbar bg-color -> "#fff"
        children color -> "#000"
    when child hovered:
        child color -> "ff7bac"

当导航栏和子项悬停时当没有悬停时,scrollY <= 0,当导航栏悬停且滚动时,Y <= 0 或当 scrollY > 0 时

JS代码:

let navbar = document.querySelector('.navbar');
let navbar_tags = navbar.querySelectorAll("p, i, a");
let navbar_products = document.querySelector('.navbar-products');
let children = [...navbar_tags, navbar_products];
let timeout_n;

function whiteNavbar() { 
    navbar.style.backgroundColor = '#fff';
    children.forEach(child => {
        child.style.color = "#000";
        child.addEventListener('mouseover', function() {
            this.style.color = "#ff7bac";
        });
        child.addEventListener('mouseout', function() {
            this.style.color = "#000";
        });
    });
}

function transparentNavbar() {
    
}

function changeNavbarColor() {
    const scrollY = window.scrollY;
    if (scrollY > 0) {
        whiteNavbar();
    }
    else {
        transparentNavbar();
    }
}


transparentNavbar();
window.addEventListener('scroll', function () {
    clearTimeout(timeout_n);
    timeout_n = setTimeout(changeNavbarColor, 10);
});

一开始我是用CSS完成的,但是当scrollY>0时,我不得不切换到JS来添加功能 当与函数类似时,一切正常,除了导航栏事件与子事件重叠,并且子颜色 -> “ff7bac” 停止工作。 法典:whiteNavbar()

function transparentNavbar() {
    navbar.style.backgroundColor = 'rgba(0, 0, 0, 0.2)';
    children.forEach(child => {
        child.style.color = "#fff";
        child.addEventListener('mouseover', function() {
            this.style.color = "#ff7bac";
        });
        child.addEventListener('mouseout', function() {
            this.style.color = "#fff";
        });
    });

    navbar.addEventListener('mouseover', function() {
        this.style.backgroundColor = '#fff';
        children.forEach(child => {
            child.style.color = "#000";
        });
    });

    navbar.addEventListener('mouseout', function() {
        this.style.backgroundColor = 'rgba(0, 0, 0, 0.2)';
        children.forEach(child => {
            child.style.color = "#fff";
        });
    });
}

这是最好的版本^^,除了子色之外几乎有效 - 悬停时>“#77bac”

我试过了,但随后导航栏悬停 bg-color 更改,停止工作。stopPropagation()

JavaScript 事件 事件处理 导航栏 父子项

评论


答:

0赞 Mr Khan 10/29/2023 #1

我认为你可以用 Css 简化这个逻辑,而不是完全依赖 JS 事件。我只需创建一个类,该类将负责设置导航栏、背景和子链接的样式。

由于您没有提供完整的代码,因此我添加了一个 and 来显示完整的工作示例。这是你如何做到的。position stickytop: 0

let timeout_n; // Define timeout_n variable

const navbar = document.querySelector('.navbar');
const navbarTags = navbar.querySelectorAll("p, i, a");

function whiteNavbar() {
    navbar.classList.add('white-bg');
    navbarTags.forEach(child => {
        child.classList.add('white-text');
    });
}

function transparentNavbar() {
    navbar.classList.remove('white-bg');
    navbarTags.forEach(child => {
        child.classList.remove('white-text');
    });
}

function changeNavbarColor() {
    const scrollY = window.scrollY;
    if (scrollY > 0) {
        whiteNavbar();
    } else {
        transparentNavbar();
    }
}

transparentNavbar();

window.addEventListener('scroll', function() {
    clearTimeout(timeout_n);
    timeout_n = setTimeout(changeNavbarColor, 10);
});
/* Initial styles for navbar */
.navbar {
    top:0;
    position: sticky;
    background-color: rgba(0, 0, 0, 0.2);
    color: #fff;
    border: 1px solid;
}

/* Styles when navbar is white */
.navbar.white-bg {
    background-color: #fff;
    color: #000;
}

/* Styles for child elements when navbar is white */
.navbar.white-bg p,
.navbar.white-bg i,
.navbar.white-bg a {
    color: #000;
}

/* Hover styles for child elements when navbar is white */
.navbar p:hover, .navbar.white-bg p:hover,
.navbar i:hover, .navbar.white-bg i:hover,
.navbar a:hover, .navbar.white-bg a:hover {
    color: #ff7bac;
}

/* Hover styles for navbar when navbar is white */
.navbar.white-bg:hover {
    background-color: #fff;
}
<div class="navbar">
  <a href="#">Link 1</a>
  <a href="#">Link 2</a>
  <i>Icon</i>
  <p>Paragraph</p>
</div>

<br/>
<br/>
<br/>
<br/>
<br/>

<br/>
<br/>
<br/>
<br/>
<br/>

<br/>
<br/>
<br/>
<br/>
<br/>

<br/>
<br/>
<br/>
<br/>
<br/>

评论

0赞 Patrycja Zborowska 10/29/2023
真的很感谢,实际上这是最好的方法,我试图避免这种情况,因为我真的很讨厌css,但它实际上很容易。再次感谢您的帮助