[英]How to create a sticky header which dynamically changes between mobile and desktop (No hamburger)
I'd like to have a sticky header navigation menu that contains Text and an icon on a desktop browser, but only icons on a mobile device (as illustrated in my sketch at the bottom of this post - ignore colors used.) I've started writing the code here: https://codepen.io/jord8on/pen/pxgjZq 我想要一个粘性的标题导航菜单,其中包含文本和桌面浏览器上的图标,但仅包含移动设备上的图标(如本文底部的素描所示-忽略使用的颜色。)开始在此处编写代码: https : //codepen.io/jord8on/pen/pxgjZq
This article gives another exmaple of what I'd love to have. 这篇文章给出了我想要拥有的另一个例子。 (specifically the 3rd of the "Tabbed Navigation" samples)
(特别是“标签导航”示例的第3个)
Here's what I need some help with: 这是我需要帮助的内容:
Any help would be so much appreciated! 任何帮助将不胜感激! 🙌
🙌
Current CSS/HTML: 当前的CSS / HTML:
/* Style the navigation bar */ .navbar { width: 100%; background-color: #B7234C; overflow: auto; } /* Navbar links */ .navbar a { float: left; text-align: center; padding: 12px 0px; color: white; text-decoration: none; font-size: 17px; width: 20%; } /* Navbar links on mouse-over */ .navbar a:hover { background-color: #961d3f; } /* Current/active navbar link */ .active { background-color: #d6295a; } /* Add responsiveness - will automatically display the navbar vertically instead of horizontally on screens less than 500 pixels */ @media screen and (max-width: 600px) { .navbar a { float: none; display: block; } }
<!-- Load font awesome icon library --> <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css"> <!-- Load font awesome icon library --> <div class="navbar"> <a class="active" href="#"><i class="fa fa-fw fa-home"></i> Home</a> <a href="#"><i class="fa fa-fw fa-comments"></i> Community</a> <a href="#"><i class="fa fa-fw fa-edit"></i> Courses</a> <a href="#"><i class="fa fa-fw fa-star"></i> Resources</a> <a href="#"><i class="fa fa-fw fa-calendar"></i> Events</a> </div>
I commented above, but here's an example: 我在上面评论过,但这是一个例子:
@jord8on I added another attribute to the navbar in the code above. @ jord8on我在上面的代码中向导航栏添加了另一个属性。 Keep in mind that you may need to add a top margin to any subsequent elements at an equal height to the navbar.
请记住,您可能需要在与导航栏相同高度的所有后续元素上添加一个上边距。 Position fixed takes that element out of the flow of the DOM.
固定位置将元素从DOM流中移出。
As for the third part, is each page in a separate html file? You'll need to write some PHP to echo the "active" class into your links with Wordpress. 至于第三部分,每个页面是否在单独的html文件中? Or how are you making the navbar globally available?
或者,您如何使导航栏全局可用?
您将需要编写一些PHP,以将“活动”类回显到与Wordpress的链接中。
This got a bit lengthy for commenting, so here's the easier to read option. 这需要冗长的评论时间,因此这是更易于阅读的选项。 Sorry about that.
对于那个很抱歉。
/* Style the navigation bar */
.navbar {
width: 100%;
background-color: #B7234C;
overflow: auto;
//Add this to the navbar element
position: fixed;
}
@media screen and (max-width: 600px) {
.navbar a {
float: none;
display: block;
}
.mob-off {
display: none;
}
}
<div class="navbar">
<a class="active" href="#">
<i class="fa fa-fw fa-home"></i> <span class="mob-off">Home</span>
</a>
<a href="#">
<i class="fa fa-fw fa-comments"></i> <span class="mob-off">Community</span>
</a>
<a href="#"><i class="fa fa-fw fa-edit">
</i> <span class="mob-off">Courses</span>
</a>
<a href="#">
<i class="fa fa-fw fa-star"></i> <span class="mob-off">Resources</span>
</a>
<a href="#">
<i class="fa fa-fw fa-calendar"></i> <span class="mob-off">Events</span>
</a>
</div>
body{ margin: 0; height: 4000px; } /* Style the navigation bar */ .navbar { position: fixed; width: 100%; background-color: #B7234C; overflow: auto; } /* Navbar links */ .navbar a { float: left; text-align: center; padding: 12px 0px; color: white; text-decoration: none; font-size: 17px; width: 20%; } /* Navbar links on mouse-over */ .navbar a:hover { background-color: #961d3f; } /* Current/active navbar link */ /* In your html you change the body class and target it in here */ /* This example uses the events icon which is a child 5 in the navigation */ .events .navbar a:nth-child(5) { background-color: #d6295a; } /* Add responsiveness - will automatically display the navbar vertically instead of horizontally on screens less than 500 pixels */ @media screen and (max-width: 700px) { .navbar{ display: flex; } .navbar span{ display: none; } }
<!-- Change the class name depending on the page that you're in then modify the css --> <body class="events"> <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css"> <!-- Load font awesome icon library --> <div class="navbar"> <a class="active" href="#"><i class="fa fa-fw fa-home"></i><span> Home</span></a> <a href="#"><i class="fa fa-fw fa-comments"></i> <span>Community</span></a> <a href="#"><i class="fa fa-fw fa-edit"></i><span> Courses</span></a> <a href="#"><i class="fa fa-fw fa-star"></i> <span>Resources</span></a> <a href="#"><i class="fa fa-fw fa-calendar"></i><span> Events</span></a> </div> </body>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.