简体   繁体   English

如何创建在移动设备和台式机之间动态更改的粘性标头(无汉堡包)

[英]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: 这是我需要帮助的内容:

  1. When a browser width shrinks below 600px I'd like the labels/text next to the icons to disappear and only show the icons. 当浏览器宽度缩小到600像素以下时,我希望图标旁边的标签/文本消失,仅显示图标。
  2. I want the .navbar to be sticky and remain atop the screen on desktop and mobile, when a users scrolls down. 我希望.navbar保持粘性,并在用户向下滚动时保持在台式机移动设备的屏幕顶部。
  3. I'd like to have the "active" menu item highlighted, depending on where a person is, in the site. 我想突出显示“活动的”菜单项,具体取决于网站上人的位置。
    1. Home, Courses, Resources, and Events are all going to be different areas within a Wordpress site. 主页,课程,资源和事件都将成为Wordpress网站中的不同区域。
    2. The Community tab will link to a subdomain which is running a Discourse community “社区”选项卡将链接到运行Discourse社区的子域

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? 至于第三部分,每个页面是否在单独的html文件中? Or how are you making the navbar globally available? 或者,您如何使导航栏全局可用? You'll need to write some PHP to echo the "active" class into your links with Wordpress. 您将需要编写一些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>
  1. Put a span between your texts in your anchor tags and target it on the media query with a display none 在锚定标记中的文本之间放置一个跨度,并在不显示的情况下将其定位到媒体查询上
  2. Put a position fixed on your navbar you might need to reset the margins on the body. 在导航栏上固定一个位置,您可能需要重置身体上的边距。 I added 4000px on height to reflect the sticky scroll. 我在高度上增加了4000像素以反映粘性滚动。
  3. Depending on the page change the class on the body tag and edit in the CSS when needed this example is targeting the events icon. 根据页面的不同,更改body标签上的类,并在需要时在CSS中进行编辑,此示例针对事件图标。 Codepen Hope this helps. Codepen希望这会有所帮助。

 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM