带有 3 行下拉菜单的粘性响应顶部导航栏 [英] Sticky responsive top navigation bar with 3-line dropdown menu

查看:54
本文介绍了带有 3 行下拉菜单的粘性响应顶部导航栏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的粘性响应式顶部导航栏,带有 3 行下拉菜单.

<头><meta name="viewport" content="width=device-width, initial-scale=1"><link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css"><风格>身体 {边距:0;字体系列:继承}.header {字体系列:Consolas、Helvetica、sans-serif、Arial;填充:20px;文本对齐:居中;背景图像:线性渐变(向右,#000a98,#00B8FF);白颜色;字体大小:15px;}.topnav {溢出:隐藏;背景色:#333;背景图像:线性渐变(向右,#000a98,#00B8FF);}.topnav a {向左飘浮;显示:块;颜色:#f2f2f2;文本对齐:居中;填充:14px 16px;文字装饰:无;字体大小:17px;}.积极的 {背景图像:线性渐变(向右,#000a98,#00B8FF);白颜色;}.topnav .icon {显示:无;}.落下 {向左飘浮;溢出:隐藏;}.dropdown .dropbtn {字体大小:17px;边界:无;大纲:无;白颜色;填充:14px 16px;背景色:继承;字体系列:继承;边距:0;}.下拉内容{显示:无;位置:绝对;背景色:#f9f9f9;最小宽度:160px;box-shadow: 0px 8px 16px 0px rgba(0, 0, 0, 0.2);z-索引:1;}.dropdown-content a {浮动:无;颜色:黑色;填充:12px 16px;文字装饰:无;显示:块;文本对齐:左;}.topnav a:悬停,.dropdown:悬停 .dropbtn {背景色:#555;白颜色;}.dropdown-content a:hover {背景色:#ddd;颜色:黑色;}.dropdown:悬停 .dropdown-content {显示:块;}@media 屏幕和(最大宽度:600px){.topnav a:not(:first-child),.dropdown .dropbtn {显示:无;}.topnav a.icon {浮动:对;显示:块;}}@media 屏幕和(最大宽度:600px){.topnav.responsive {位置:相对;}.topnav.responsive .icon {位置:绝对;右:0;顶部:0;}.topnav.responsive 一个 {浮动:无;显示:块;文本对齐:左;}.topnav.responsive .dropdown {浮动:无;}.topnav.responsive .dropdown-content {位置:相对;}.topnav.responsive .dropdown .dropbtn {显示:块;宽度:100%;文本对齐:左;}}.内容 {填充:16px;}.sticky {位置:固定;顶部:0;宽度:100%;}.sticky+.content {填充顶部:60px;}</风格><身体><div class="header">hhvvvvvvvvv

<div class="topnav" id="myTopnav"><a href="#home" class="active">首页</a><a href="#news">新闻</a><a href="#contact">联系方式</a><div class="下拉菜单"><button class="dropbtn">下拉菜单<i class="fa fa-caret-down"></i><div class="dropdown-content"><a href="#">链接 1</a><a href="#">链接 2</a><a href="#">链接 3</a>

<a href="#about">关于</a><a href="javascript:void(0);"样式=字体大小:15px;"class="icon" onclick="myFunction()">&#9776;</a>

<脚本>函数 myFunction() {var x = document.getElementById("myTopnav");if (x.className === "topnav") {x.className += "响应式";} 别的 {x.className = "topnav";}}window.onscroll = function () { myFunction() };var navbar = document.getElementById("myTopnav");var sticky = navbar.offsetTop;函数 myFunction() {如果(window.pageYOffset >=粘性){navbar.classList.add("粘性")} 别的 {navbar.classList.remove("粘性");}}window.addEventListener('scroll',stickyNavigation);<div style="padding-left:16px"><h2>带有下拉菜单的响应式 Topnav</h2><p>调整浏览器窗口的大小以查看其工作原理.</p><p>将鼠标悬停在下拉按钮上以打开下拉菜单.</p><h2>带有下拉菜单的响应式 Topnav</h2><p>调整浏览器窗口的大小以查看其工作原理.</p><p>将鼠标悬停在下拉按钮上以打开下拉菜单.</p><h2>带有下拉菜单的响应式 Topnav</h2><p>调整浏览器窗口的大小以查看其工作原理.</p><p>将鼠标悬停在下拉按钮上以打开下拉菜单.</p><h2>带有下拉菜单的响应式 Topnav</h2><p>调整浏览器窗口的大小以查看其工作原理.</p><p>将鼠标悬停在下拉按钮上以打开下拉菜单.</p></html>

要查看上述代码的现场演示,请点击这里一切都很好,但下拉菜单不起作用.在我尝试使导航栏变得粘稠之前,下拉菜单工作正常,但在使导航栏变得粘稠后我面临这个问题.

解决方案

Replace position: absolute;position: fixed;

.dropdown-content {显示:无;位置:固定;/* <------------- */背景色:#f9f9f9;最小宽度:160px;box-shadow: 0px 8px 16px 0px rgba(0, 0, 0, 0.2);z-索引:1;}

示例:

<头><meta name="viewport" content="width=device-width, initial-scale=1"><link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css"><风格>身体 {边距:0;字体系列:继承}.header {字体系列:Consolas、Helvetica、sans-serif、Arial;填充:20px;文本对齐:居中;背景图像:线性渐变(向右,#000a98,#00B8FF);白颜色;字体大小:15px;}.topnav {溢出:隐藏;背景色:#333;背景图像:线性渐变(向右,#000a98,#00B8FF);}.topnav a {向左飘浮;显示:块;颜色:#f2f2f2;文本对齐:居中;填充:14px 16px;文字装饰:无;字体大小:17px;}.积极的 {背景图像:线性渐变(向右,#000a98,#00B8FF);白颜色;}.topnav .icon {显示:无;}.落下 {向左飘浮;溢出:隐藏;}.dropdown .dropbtn {字体大小:17px;边界:无;大纲:无;白颜色;填充:14px 16px;背景色:继承;字体系列:继承;边距:0;}.下拉内容{显示:无;位置:固定;背景色:#f9f9f9;最小宽度:160px;box-shadow: 0px 8px 16px 0px rgba(0, 0, 0, 0.2);z-索引:1;}.dropdown-content a {浮动:无;颜色:黑色;填充:12px 16px;文字装饰:无;显示:块;文本对齐:左;}.topnav a:悬停,.dropdown:悬停 .dropbtn {背景色:#555;白颜色;}.dropdown-content a:hover {背景色:#ddd;颜色:黑色;}.dropdown:悬停 .dropdown-content {显示:块;}@media 屏幕和(最大宽度:600px){.topnav a:not(:first-child),.dropdown .dropbtn {显示:无;}.topnav a.icon {浮动:对;显示:块;}}@media 屏幕和(最大宽度:600px){.topnav.responsive {位置:相对;}.topnav.responsive .icon {位置:绝对;右:0;顶部:0;}.topnav.responsive 一个 {浮动:无;显示:块;文本对齐:左;}.topnav.responsive .dropdown {浮动:无;}.topnav.responsive .dropdown-content {位置:相对;}.topnav.responsive .dropdown .dropbtn {显示:块;宽度:100%;文本对齐:左;}}.内容 {填充:16px;}.sticky {位置:固定;顶部:0;宽度:100%;}.sticky+.content {填充顶部:60px;}</风格><身体><div class="header">hhvvvvvvvvv

<div class="topnav" id="myTopnav"><a href="#home" class="active">首页</a><a href="#news">新闻</a><a href="#contact">联系方式</a><div class="下拉菜单"><button class="dropbtn">下拉菜单<i class="fa fa-caret-down"></i><div class="dropdown-content"><a href="#">链接 1</a><a href="#">链接 2</a><a href="#">链接 3</a>

<a href="#about">关于</a><a href="javascript:void(0);"样式=字体大小:15px;"class="icon" onclick="myFunction()">&#9776;</a>

<脚本>函数 myFunction() {var x = document.getElementById("myTopnav");if (x.className === "topnav") {x.className += "响应式";} 别的 {x.className = "topnav";}}window.onscroll = function () { myFunction() };var navbar = document.getElementById("myTopnav");var sticky = navbar.offsetTop;函数 myFunction() {如果(window.pageYOffset >=粘性){navbar.classList.add("粘性")} 别的 {navbar.classList.remove("粘性");}}//window.addEventListener('scroll',stickyNavigation);<div style="padding-left:16px"><h2>带有下拉菜单的响应式 Topnav</h2><p>调整浏览器窗口的大小以查看其工作原理.</p><p>将鼠标悬停在下拉按钮上以打开下拉菜单.</p><h2>带有下拉菜单的响应式 Topnav</h2><p>调整浏览器窗口的大小以查看其工作原理.</p><p>将鼠标悬停在下拉按钮上以打开下拉菜单.</p><h2>带有下拉菜单的响应式 Topnav</h2><p>调整浏览器窗口的大小以查看其工作原理.</p><p>将鼠标悬停在下拉按钮上以打开下拉菜单.</p><h2>带有下拉菜单的响应式 Topnav</h2><p>调整浏览器窗口的大小以查看其工作原理.</p><p>将鼠标悬停在下拉按钮上以打开下拉菜单.</p>

</html>

I have a simple sticky responsive top navigation bar with 3-line dropdown menu.

<html>

<head>
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
    <style>
        body {
            margin: 0;
            font-family: inherit
        }

        .header {
            font-family: Consolas, Helvetica, sans-serif, Arial;
            padding: 20px;
            text-align: center;
            background-image: linear-gradient(to right, #000a98, #00B8FF);
            color: white;
            font-size: 15px;

        }

        .topnav {
            overflow: hidden;
            background-color: #333;
            background-image: linear-gradient(to right, #000a98, #00B8FF);
        }


        .topnav a {
            float: left;
            display: block;
            color: #f2f2f2;
            text-align: center;
            padding: 14px 16px;
            text-decoration: none;
            font-size: 17px;
        }

        .active {
            background-image: linear-gradient(to right, #000a98, #00B8FF);
            color: white;
        }

        .topnav .icon {
            display: none;
        }

        .dropdown {
            float: left;
            overflow: hidden;

        }

        .dropdown .dropbtn {
            font-size: 17px;
            border: none;
            outline: none;
            color: white;
            padding: 14px 16px;
            background-color: inherit;
            font-family: inherit;
            margin: 0;
        }

        .dropdown-content {
            display: none;
            position: absolute;
            background-color: #f9f9f9;
            min-width: 160px;
            box-shadow: 0px 8px 16px 0px rgba(0, 0, 0, 0.2);
            z-index: 1;
        }

        .dropdown-content a {
            float: none;
            color: black;

            padding: 12px 16px;
            text-decoration: none;
            display: block;
            text-align: left;
        }

        .topnav a:hover,
        .dropdown:hover .dropbtn {
            background-color: #555;
            color: white;
        }

        .dropdown-content a:hover {
            background-color: #ddd;
            color: black;
        }

        .dropdown:hover .dropdown-content {
            display: block;
        }

        @media screen and (max-width: 600px) {

            .topnav a:not(:first-child),
            .dropdown .dropbtn {
                display: none;
            }

            .topnav a.icon {
                float: right;
                display: block;
            }
        }

        @media screen and (max-width: 600px) {
            .topnav.responsive {
                position: relative;
            }

            .topnav.responsive .icon {
                position: absolute;
                right: 0;
                top: 0;
            }

            .topnav.responsive a {
                float: none;
                display: block;
                text-align: left;
            }

            .topnav.responsive .dropdown {
                float: none;
            }

            .topnav.responsive .dropdown-content {
                position: relative;
            }

            .topnav.responsive .dropdown .dropbtn {
                display: block;
                width: 100%;
                text-align: left;
            }

        }

        .content {
            padding: 16px;
        }

        .sticky {
            position: fixed;
            top: 0;
            width: 100%;
        }

        .sticky+.content {
            padding-top: 60px;
        }
    </style>
</head>

<body>
    <div class="header">hhvvvvvvvvv
    </div>
    <div class="topnav" id="myTopnav">
        <a href="#home" class="active">Home</a>
        <a href="#news">News</a>
        <a href="#contact">Contact</a>
        <div class="dropdown">

            <button class="dropbtn">Dropdown
                <i class="fa fa-caret-down"></i>
            </button>
            <div class="dropdown-content">
                <a href="#">Link 1</a>
                <a href="#">Link 2</a>
                <a href="#">Link 3</a>
            </div>
        </div>
        <a href="#about">About</a>
        <a href="javascript:void(0);" style="font-size:15px;" class="icon" onclick="myFunction()">&#9776;</a>

    </div>
    <script>
        function myFunction() {
            var x = document.getElementById("myTopnav");

            if (x.className === "topnav") {
                x.className += " responsive";
            } else {
                x.className = "topnav";
            }
        }
        window.onscroll = function () { myFunction() };

        var navbar = document.getElementById("myTopnav");
        var sticky = navbar.offsetTop;

        function myFunction() {
            if (window.pageYOffset >= sticky) {
                navbar.classList.add("sticky")
            } else {
                navbar.classList.remove("sticky");
            }
        }

        window.addEventListener('scroll', stickyNavigation);

    </script>
    <div style="padding-left:16px">
        <h2>Responsive Topnav with Dropdown</h2>

        <p>Resize the browser window to see how it works.</p>
        <p>Hover over the dropdown button to open the dropdown menu.</p>
        <h2>Responsive Topnav with Dropdown</h2>
        <p>Resize the browser window to see how it works.</p>
        <p>Hover over the dropdown button to open the dropdown menu.</p>
        <h2>Responsive Topnav with Dropdown</h2>
        <p>Resize the browser window to see how it works.</p>
        <p>Hover over the dropdown button to open the dropdown menu.</p>
        <h2>Responsive Topnav with Dropdown</h2>
        <p>Resize the browser window to see how it works.</p>
        <p>Hover over the dropdown button to open the dropdown menu.</p>

</body>
</html>

To see the live demo of above code click here Everything is fine but dropdown is not working. Before I tried to make navbar sticky, dropdown is working perfect but I am facing this problem after making navbar sticky.

解决方案

Replace position: absolute; with position: fixed;

.dropdown-content {
    display: none;
    position: fixed; /* <------------- */
    background-color: #f9f9f9;
    min-width: 160px;
    box-shadow: 0px 8px 16px 0px rgba(0, 0, 0, 0.2);
    z-index: 1;
}

Example:

<html>

<head>
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
    <style>
        body {
            margin: 0;
            font-family: inherit
        }

        .header {
            font-family: Consolas, Helvetica, sans-serif, Arial;
            padding: 20px;
            text-align: center;
            background-image: linear-gradient(to right, #000a98, #00B8FF);
            color: white;
            font-size: 15px;

        }

        .topnav {
            overflow: hidden;
            background-color: #333;
            background-image: linear-gradient(to right, #000a98, #00B8FF);
        }


        .topnav a {
            float: left;
            display: block;
            color: #f2f2f2;
            text-align: center;
            padding: 14px 16px;
            text-decoration: none;
            font-size: 17px;
        }

        .active {
            background-image: linear-gradient(to right, #000a98, #00B8FF);
            color: white;
        }

        .topnav .icon {
            display: none;
        }

        .dropdown {
            float: left;
            overflow: hidden;

        }

        .dropdown .dropbtn {
            font-size: 17px;
            border: none;
            outline: none;
            color: white;
            padding: 14px 16px;
            background-color: inherit;
            font-family: inherit;
            margin: 0;
        }

        .dropdown-content {
            display: none;
            position: fixed;
            background-color: #f9f9f9;
            min-width: 160px;
            box-shadow: 0px 8px 16px 0px rgba(0, 0, 0, 0.2);
            z-index: 1;
        }

        .dropdown-content a {
            float: none;
            color: black;

            padding: 12px 16px;
            text-decoration: none;
            display: block;
            text-align: left;
        }

        .topnav a:hover,
        .dropdown:hover .dropbtn {
            background-color: #555;
            color: white;
        }

        .dropdown-content a:hover {
            background-color: #ddd;
            color: black;
        }

        .dropdown:hover .dropdown-content {
            display: block;
        }

        @media screen and (max-width: 600px) {

            .topnav a:not(:first-child),
            .dropdown .dropbtn {
                display: none;
            }

            .topnav a.icon {
                float: right;
                display: block;
            }
        }

        @media screen and (max-width: 600px) {
            .topnav.responsive {
                position: relative;
            }

            .topnav.responsive .icon {
                position: absolute;
                right: 0;
                top: 0;
            }

            .topnav.responsive a {
                float: none;
                display: block;
                text-align: left;
            }

            .topnav.responsive .dropdown {
                float: none;
            }

            .topnav.responsive .dropdown-content {
                position: relative;
            }

            .topnav.responsive .dropdown .dropbtn {
                display: block;
                width: 100%;
                text-align: left;
            }

        }

        .content {
            padding: 16px;
        }

        .sticky {
            position: fixed;
            top: 0;
            width: 100%;
        }

        .sticky+.content {
            padding-top: 60px;
        }
    </style>
</head>

<body>
    <div class="header">hhvvvvvvvvv
    </div>
    <div class="topnav" id="myTopnav">
        <a href="#home" class="active">Home</a>
        <a href="#news">News</a>
        <a href="#contact">Contact</a>
        <div class="dropdown">

            <button class="dropbtn">Dropdown
                <i class="fa fa-caret-down"></i>
            </button>
            <div class="dropdown-content">
                <a href="#">Link 1</a>
                <a href="#">Link 2</a>
                <a href="#">Link 3</a>
            </div>
        </div>
        <a href="#about">About</a>
        <a href="javascript:void(0);" style="font-size:15px;" class="icon" onclick="myFunction()">&#9776;</a>

    </div>
    <script>
        function myFunction() {
            var x = document.getElementById("myTopnav");

            if (x.className === "topnav") {
                x.className += " responsive";
            } else {
                x.className = "topnav";
            }
        }
        window.onscroll = function () { myFunction() };

        var navbar = document.getElementById("myTopnav");
        var sticky = navbar.offsetTop;

        function myFunction() {
            if (window.pageYOffset >= sticky) {
                navbar.classList.add("sticky")
            } else {
                navbar.classList.remove("sticky");
            }
        }

        //window.addEventListener('scroll', stickyNavigation);

    </script>
    <div style="padding-left:16px">
        <h2>Responsive Topnav with Dropdown</h2>

        <p>Resize the browser window to see how it works.</p>
        <p>Hover over the dropdown button to open the dropdown menu.</p>
        <h2>Responsive Topnav with Dropdown</h2>
        <p>Resize the browser window to see how it works.</p>
        <p>Hover over the dropdown button to open the dropdown menu.</p>
        <h2>Responsive Topnav with Dropdown</h2>
        <p>Resize the browser window to see how it works.</p>
        <p>Hover over the dropdown button to open the dropdown menu.</p>
        <h2>Responsive Topnav with Dropdown</h2>
        <p>Resize the browser window to see how it works.</p>
        <p>Hover over the dropdown button to open the dropdown menu.</p>
        
   </div>

</body>

</html>

这篇关于带有 3 行下拉菜单的粘性响应顶部导航栏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
前端开发最新文章
热门教程
热门工具
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆