如何使用 CSS 和 JavaScript 创建响应式底部导航菜单?
javascriptweb developmentfront end technology更新于 2024/6/15 3:32:00
在本文中,我们将讨论如何使用 CSS 和 JavaScript 创建响应式底部导航菜单。
通常,一旦您创建网页,当您更改页面大小时,页面的某些内容将被隐藏。响应式页面是随着我们更改页面尺寸而调整页面内容的页面。
要创建响应式导航菜单,我们必须使用 CSS 和 JavaScript。响应式菜单会调整浏览器菜单的大小,以查看导航菜单在小型和移动尺寸显示屏上的运行情况。
简而言之,在响应式页面中,行为会根据不同的设备和屏幕宽度而改变。
让我们看一个响应式按钮导航的示例。
Example.html
创建一个 HTML 文件,我们将在其中定义页面的结构(视图)。在此示例中,我们使用 HTML 代码创建当前页面,其中包含所需文本、底部导航菜单和菜单的空导航链接。
<body> <div class="navbar" id="myNavbar"> <a href="#home" class="active">Home</a> <a href="#tutorials">Tutorials</a> <a href="#contact">Contact</a> <a href="#about">About</a> <a href="javascript:void(0);" style="font-size:15px;" class="icon" onclick="myFunction()">☰</a> </div> <div style="padding-left:16px"> <h2>Responsive Bottom Navigation </h2> <p>Resize the browser window to see how it works.</p> </div>
Example.css
添加 css 样式,为底部导航菜单栏提供背景和悬停效果,以获得更好的外观。在此示例中,我们正在设置底部导航菜单的样式,如果我们更改页面的尺寸,则菜单将在垂直方向上可见。
<style> body { margin: 0; font-family: Arial, Helvetica, sans-serif; } .navbar { overflow: hidden; background-color: rgb(124, 121, 121); position: fixed; bottom: 0; width: 100%; } .navbar a { float: left; display: block; color: #ffffff; text-align: center; padding: 14px 16px; text-decoration: none; font-size: 17px; } .navbar a:hover { background-color: rgb(214, 39, 39); color: black; } .navbar a.active { background-color: #045faa; color: white; } .navbar .icon { display: none; } @media screen and (max-width: 600px) { .navbar a:not(:first-child) { display: none; } .navbar a.icon { float: right; display: block; } } @media screen and (max-width: 600px) { .navbar.responsive .icon { position: absolute; right: 0; bottom: 0; } .navbar.responsive a { float: none; display: block; text-align: left; } } </style>
Example.js
使用 JavaScript,我们可以在页面上执行验证和处理事件。在此示例中,我们添加了活动类。如果您更改页面的尺寸,则我们将添加菜单栏按钮来打开和关闭底部导航菜单。为此,我们可以使用将捕获鼠标单击操作的侦听器。
<script> function myFunction() { var x = document.getElementById("myNavbar"); if (x.className === "navbar") { x.className += " responsive"; } else { x.className = "navbar"; } } </script>
完整示例
<!DOCTYPE html> <html> <head> <meta name="viewport" content="width=device-width, initial-scale=1"> <style> body { margin: 0; font-family: Arial, Helvetica, sans-serif; } .navbar { overflow: hidden; background-color: rgb(124, 121, 121); position: fixed; bottom: 0; width: 100%; } .navbar a { float: left; display: block; color: #ffffff; text-align: center; padding: 14px 16px; text-decoration: none; font-size: 17px; } .navbar a:hover { background-color: rgb(214, 39, 39); color: black; } .navbar a.active { background-color: #045faa; color: white; } .navbar .icon { display: none; } @media screen and (max-width: 600px) { .navbar a:not(:first-child) { display: none; } .navbar a.icon { float: right; display: block; } } @media screen and (max-width: 600px) { .navbar.responsive .icon { position: absolute; right: 0; bottom: 0; } .navbar.responsive a { float: none; display: block; text-align: left; } } </style> </head> <body> <div class="navbar" id="myNavbar"> <a href="#home" class="active">Home</a> <a href="#tutorials">Tutorials</a> <a href="#contact">Contact</a> <a href="#about">About</a> <a href="javascript:void(0);" style="font-size:15px;" class="icon" onclick="myFunction()">☰</a> </div> <div style="padding-left:16px"> <h2>Responsive Bottom Navigation </h2> <p>Resize the browser window to see how it works.</p> </div> <script> function myFunction() { var x = document.getElementById("myNavbar"); if (x.className === "navbar") { x.className += " responsive"; } else { x.className = "navbar"; } } </script> </body> </html>