如何使用CSS和JavaScript在向下滚动时隐藏导航菜单?


要使用CSSJavaScript在向下滚动时隐藏导航菜单,用户应该具备Javascript 条件语句和CSS的基础知识。我们将使用HTML创建导航菜单,使用CSS设置导航菜单的样式,并使用Javascript在向下滚动时隐藏导航菜单。

我们有一个带有文本内容的导航菜单,我们的任务是在向下滚动时隐藏导航菜单。在这篇文章中,我们将实现以下步骤,以使用CSS和JavaScript在向下滚动时隐藏导航菜单

使用HTML创建导航菜单的结构

  • 我们在HTML代码中使用了两个div标签和四个标签。
  • 带有class nav的div元素创建了导航菜单的结构,其中包含使用锚标签创建的四个选项。
  • 带有class para的div元素编写HTML内容,向下滚动时将隐藏导航菜单。

以下是上述步骤的HTML实现

<div id="nav">
<a href="#">Home</a>
<a href="#">Tutorials</a>
<a href="#">Tutorix</a>
<a href="#">Contact</a>
</div>
<div class="para">
    <p>
        CSS is the language used to design the web
        pages or specifying the presentation of a
        document written in a markup language like
        HTML.
    </p>
    <p>
        CSS helps web developers to control 
        the layout and other visual aspects of 
        the web pages.
    </p>
    <p>
        CSS stands for Cascading Style 
        Sheets, it is a simple design language 
        intended to simplify the process of making 
        web pages presentable using CSS properties.
    </p>
    <p>
        CSS specify how an HTML element should be
        displayed on the web page. If you think of
        the human body as a web page then CSS is
        styling part of the body. Like color of
        the eyes, size of the nose, skin tone, etc.
    </p>
    <p>
        This CSS tutorial is designed for aspiring 
        Web Designers and Developers from basics 
        to advance. CSS knowledge is must whoever 
        wants to be a web developer, there are a 
        lot of CSS frameworks like Bootstrap, 
        Tailwind CSS, etc. But having CSS knowledge 
        is must as a web developer.
    </p>
</div>  

使用CSS设置导航菜单的样式

  • 我们使用了nav ID来设计导航菜单,它将背景颜色设置为绿色,使用positiontop属性将导航菜单固定在顶部,并在隐藏和显示导航菜单时添加过渡
  • 我们使用了#nav a选择器,它选择导航菜单选项并设置其字体系列,我们使用了float属性,它从左侧水平排列菜单选项,设置填充字体大小
  • 将鼠标悬停在导航菜单选项上时,我们将其文本装饰设置为下划线。
  • 最后,我们为书面内容设置了顶部边距、填充、字体大小和高度

以下是上述步骤的CSS实现

#nav {
    background-color: #04af2f;
    position: fixed;
    top: 0;
    width: 100%;
    display: block;
    transition: top 0.3s;
}
#nav a {
    font-family: Verdana, sans-serif;
    float: left;
    display: block;
    color: #f2f2f2;
    padding: 10px;
    font-size: 16px;
    text-decoration: none;
}
#nav a:hover {
    text-decoration: underline;
}
.para {
    padding: 15px;
    margin-top: 30px;
    height: 1600px;
    font-size: 20px;
}

使用JavaScript隐藏导航菜单

  • 我们使用了两个变量 prevState 和 currentState,其中 prevState 是当前垂直滚动位置,currentState 在用户滚动时更新。
  • 我们使用了在滚动时执行的事件处理程序。
  • 我们使用了简单的if-else条件语句,其中prevState > currentState表示向上滚动,prevState < currentState表示向下滚动。
  • 向上滚动时,导航菜单使用top:0;设置在顶部,向下滚动时,导航菜单使用CSS top属性设置为负值,从而隐藏导航菜单。

以下是上述步骤的实现

let prevState = window.pageYOffset;
window.onscroll = function () {
    let currentState = window.pageYOffset;
    if (prevState > currentState) {
        document.getElementById("nav").
            style.top = "0";
    } 
    else {
        document.getElementById("nav").
            style.top = "-50px";
    }
    prevState = currentState;
}

完整示例代码

以下是使用CSS和JavaScript在向下滚动时隐藏导航菜单的完整示例代码。

<!DOCTYPE html>
<html>
<head>
    <title>
        To hide a navigation menu on scroll 
        down with CSS and JavaScript
    </title>
    <style>
        #nav {
            background-color: #04af2f;
            position: fixed;
            top: 0;
            width: 100%;
            display: block;
            transition: top 0.3s;
        }
        #nav a {
            font-family: Verdana, sans-serif;
            float: left;
            display: block;
            color: #f2f2f2;
            padding: 10px;
            font-size: 16px;
            text-decoration: none;
        }
        #nav a:hover {
            text-decoration: underline;
        }
        .para {
            padding: 15px;
            margin-top: 30px;
            height: 1600px;
            font-size: 20px;
        }
    </style>
</head>
<body>
    <div id="nav">
        <a href="#">Home</a>
        <a href="#">Tutorials</a>
        <a href="#">Tutorix</a>
        <a href="#">Contact</a>
    </div>
    <div class="para">
        <p>
            CSS is the language used to design the web
            pages or specifying the presentation of a
            document written in a markup language like
            HTML.
        </p>
        <p>
            CSS helps web developers to control 
            the layout and other visual aspects of 
            the web pages.
        </p>
        <p>
            CSS stands for Cascading Style 
            Sheets, it is a simple design language 
            intended to simplify the process of making 
            web pages presentable using CSS properties.
        </p>
        <p>
            CSS specify how an HTML element should be
            displayed on the web page. If you think of
            the human body as a web page then CSS is
            styling part of the body. Like color of
            the eyes, size of the nose, skin tone, etc.
        </p>
        <p>
            This CSS tutorial is designed for aspiring 
            Web Designers and Developers from basics 
            to advance. CSS knowledge is must whoever 
            wants to be a web developer, there are a 
            lot of CSS frameworks like Bootstrap, 
            Tailwind CSS, etc. But having CSS knowledge 
            is must as a web developer.
        </p>
    </div>
    <script>
        let prevState = window.pageYOffset;
        window.onscroll = function () {
            let currentState = window.pageYOffset;
            if (prevState > currentState) {
                document.getElementById("nav").
                    style.top = "0";
            } else {
                document.getElementById("nav").
                    style.top = "-50px";
            }
            prevState = currentState;
        }
    </script>
</body>
</html>

结论

在这篇文章中,我们学习并理解了如何使用CSS和JavaScript在向下滚动时隐藏导航菜单。我们使用HTML和CSS创建和设置导航菜单的样式,同时使用JavaScript添加滚动事件监听器,该监听器在向下滚动时隐藏导航菜单,并在向上滚动时重新显示。

更新于:2024年8月19日

5000+ 次浏览

开启你的职业生涯

完成课程获得认证

开始学习
广告
© . All rights reserved.