×

【第五章】定制foundation顶部工具栏

作者:Terry2014.08.01来源:Web前端之家浏览:14906评论:0
关键词:Foundation

Zurb开发的第5版本Foundation(简称为Foundation 5)使得顶部工具栏表现为非常突出,出色的顶部工具栏差不多成为一个由Foundation搭建起来网站的象征元素。今天我们将学习如何通过不同方式去实现它,把它放置在页面的其他地方,让你拥有自定义样式和响应式的水平导航菜单。

首先我们将需要最新版本的Foundation。解压源码包并且把所有的文件放置在你的工程或者测试目录下。我们将直接使用index.html文件,创建一个style.css样式文件将用来重写顶部工具栏的样式来达到自定义导航栏效果。

搭起HTML结构

第一步:常用的模板

index.html文件里面已经预包含一些HTML代码。你可以保留<header>里面的代码,也可以保留所有的脚本(JavaScript)链接(放置在</body>标签之前)。保留这些来确保Foundation的网格系统和顶部工具栏可以运行起来。

你可以删掉其他内容代码,我们将采用一种全部宽度的设计,这不是很复杂的,只是需要想到更好的办法来处理它。

好的,我们将编写header、导航、主内容和footer区域的内容,我们将给每个区域都添加class="full-width",然后再添加一个块元素 <div class="row">,块元素里面再添加一个块元素<div class="large-12 columns">,这也就是搭起基本的网格结构。

备注:更多关于网格系统如何运行的,可以回顾教程第二章:【第二章】WEB前端框架-Foundation之网格系统

  1. <!-- HEADER AREA -->   

  2. <header class="full-width header-area">   

  3.     <div class="row">   

  4.         <div class="large-12 columns">   

  5.             <h2>Foundation-4 Custom Top Bar</h2>   

  6.         </div>   

  7.     </div>   

  8. </header>   

  9. <!-- NAVIGATION AREA -->   

  10. <div class="full-width navigation-area">   

  11.     <div class="row">   

  12.         <div class="large-12 columns">   

  13.              <nav class="top-bar"></nav>   

  14.         </div>   

  15.     </div>   

  16. </div>   

  17. <!-- CONTENT AREA -->   

  18. <div class="full-width content-area">   

  19.     <div class="row">   

  20.         <div class="large-12 columns">   

  21.              <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Harum, asperiores, voluptas, veniam commodi impedit tenetur dolores cumque facere explicabo esse quaerat veritatis laboriosam eius modi amet maxime non officia nemo? Iste, quisquam, voluptatum, dolor nam reiciendis unde aliquam numquam necessitatibus odio et perspiciatis facere nihil inventore ullam aspernatur corporis veritatis quia dolorum? Sed, hic, eos quis quibusdam eum aut optio repudiandae at! Eligendi, neque ratione alias enim quae magnam dolores esse pariatur earum laborum reiciendis nobis sunt sequi sapiente ducimus iure ipsam. Sapiente, minima, rerum, facere quos saepe pariatur magni dolorem cum amet nemo quis laborum ipsa dignissimos ducimus inventore modi rem cumque quibusdam quam asperiores! Optio, nobis suscipit molestias voluptas veritatis aspernatur accusamus excepturi rem quaerat impedit animi voluptate at facilis aliquid cum fugit labore omnis provident recusandae autem. Doloribus, mollitia quos officiis quas sapiente nam dolor praesentium maxime cupiditate illum? Rem, esse, nulla vitae adipisci sequi deleniti quasi!</p>   

  22.         </div>   

  23.     </div>   

  24. </div>   

  25. <!-- FOOTER AREA -->   

  26. <div class="full-width footer-area">   

  27.     <div class="row">   

  28.         <div class="large-12 columns">&copy; 2014</div>   

  29.     </div>   

  30. </div>   

第二步:顶部工具栏标记

在编写样式之前,我们需要编写出一些必须的HTML结构代码使得Foundation顶部工具栏能够正确地运行起来。因此需要5样基础元素使得引擎跑起来。

  • <nav class="top-bar">

  • <ul class="title-area">

  • <li class="toggle-topbar"> = 为了增强在手机布局上的菜单。

  • <section class="top-bar-section">

  • <ul class="left">和<ul class="right">

现在我们用这5样基础元素来搭建基础框架并且实现功能。

备注:在此教程例子中,我们将创建一个自定义的导航菜单,因此删除一些包括logo、文本和图片的标题。因此从下面代码例子可以看到在块元素<ul class="title-area">里的<li class="name"></li>里面是空的。

接着,我们来添加一些菜单元素和一个下拉菜单代码,在你想添加下拉菜单的li元素里添加class="has-dropdown",然后在里面包含下拉菜单代码 <ul class="dropdown">...</ul>。如果想表示当前使用状态的li,则需要添加”active”。<ul class="right"><ul>里面可以为空的,嵌入代码中。一般情况下,你将会在这个空的ul里添加一个按钮或者搜索栏。更多关于顶部工具栏,可看上一篇教程:初学者学习foundation:顶部工具栏

如下HTML代码示例,其中的注释会帮助你理解整体是如何运作的。

  1. <!-- Nav Wrap -->   

  2. <nav class="top-bar">   

  3.     <ul class="title-area">   

  4.         <!-- Title Area -->   

  5.         <li class="name"></li>   

  6.         <!-- Remove the class "menu-icon" to get rid of menu icon. Take out "Menu" to just have icon alone -->   

  7.         <li class="toggle-topbar menu-icon"><a href="#"><span>Menu</span></a></li>   

  8.     </ul>   

  9.     <!-- The Section wrap -->   

  10.     <section class="top-bar-section">   

  11.         <!-- Left Nav Section -->   

  12.         <ul class="left">   

  13.             <li class="active"><a href="#">Home</a></li>   

  14.             <li><a href="#">About us</a></li>   

  15.             <li><a href="#">Our products</a></li>   

  16.             <li><a href="#">Portfolio</a></li>   

  17.             <li><a href="#">Blog</a></li>   

  18.             <li><a href="#">Prices</a></li>   

  19.             <li class="has-dropdown">   

  20.                 <a href="#">Features</a>   

  21.                 <ul class="dropdown">   

  22.                     <li><a href="#">Modalboxes</a></li>   

  23.                     <li><a href="#">Submenu's and navigation</a></li>   

  24.                     <li><a href="#">Price tables</a></li>   

  25.                     <li><a href="#">Buttons</a></li>   

  26.                     <li><a href="#">Button groups</a></li>   

  27.                     <li><a href="#">Labels, Keystrokes and Tooltips</a></li>   

  28.                     <li><a href="#">Alert boxes</a></li>   

  29.                     <li><a href="#">Columns</a></li>   

  30.                 </ul>   

  31.             </li>   

  32.             <li><a href="#">Contact</a></li>   

  33.         </ul>   

  34.     <!-- Right Nav Section -->   

  35.     <ul class="right"></ul>   

  36.     </section>   

  37. </nav>  

第三步:目前效果

现在可以在你的浏览器上看到目前的效果,基于Foundation框架使用顶部工具栏我们已经创建一个水平菜单。为了方便把菜单放置到其他地方,而不是固定在浏览器的顶部,我们需要在以上代码外层添加网格的<div>。

当你在调整浏览器窗口时,会发现菜单栏在预先设定的断点发生改变。

接下来着手编写自定义样式,将关注如何去编写顶部工具栏的样式和可以使用哪些class。

编写CSS

第一步:通用样式

如果你还没做好准备,可以创建一个css文件,命名为style.css并且放置到下载下来的Foundation工程下css文件夹下。但别忘记把此引用到index.html文件里。如下代码示例:

  1. <head>   

  2.     <meta charset="utf-8">   

  3.     <meta name="viewport" content="width=device-width">   

  4.     <title>Foundation 4</title>   

  5.     <link rel="stylesheet" href="css/foundation.css">   

  6.     <link rel="stylesheet" href="css/style.css">   

  7.     <script src="js/vendor/custom.modernizr.js"></script>   

  8. </head>  

这里如果你不把style.css放置在foundation.css下面,将不会覆盖原来顶部工具栏的样式。

很好,接下来我们要为header、导航、内容和footer写一些基础样式,如导航块,我们将添加一张已经下载好的背景图。这些通用样式一般不是很难去实现的,见如下代码示例,我们添加一个class”full-width”到每个区域中,以致于每个区域的宽度都是占满浏览器宽度的。

  1. body {   

  2. background-color#ccc;   

  3. }   

  4. /** Set the backgrounds for the different sections **/  

  5. .header-area {   

  6. background-color#2d465c;   

  7. min-height160px;   

  8. }   

  9. .navigation-area {   

  10. background-imageurl('../img/navigation-container.jpg');   

  11. background-repeatrepeat-x;   

  12. }   

  13. .content-area {   

  14. padding50px 0 70px 0;   

  15. }   

  16. .footer-area {   

  17. background-color#1f1f1f;   

  18. color#fff;   

  19. min-height50px;   

  20. padding20px 0 0 0;   

  21. }   

  22. .full-width {   

  23. min-width:100%;   

  24. positionrelative;   

  25. }   

  26. h2 {   

  27. color#fff;   

  28. font-weightnormal;   

  29. margin-top50px;   

  30. }  

第二部:顶部工具栏样式

现在看一下效果,会发现菜单会往外偏移一点,原因就是使用默认的样式,接下来修复这个问题!

有一些样式类是需要修改以达到想要的效果。首先,我们将在.top-bar和.top-bar-section li里移除黑背景,改变height(在导航区域内)和line-height为58px。如下代码,可看注释帮助理解。

  1. /** Changes background color, height and margin of the border **/  

  2. .top-bar {   

  3. backgroundnone;   

  4. height58px;   

  5. line-height58px;   

  6. margin-bottom: 0;   

  7. }   

  8. /** Removes black background on menu bar **/  

  9. .top-bar-section ul {   

  10. backgroundnone;   

  11. text-transformuppercase;   

  12. }   

  13. /** Removes black background on menu item **/  

  14. .top-bar-section li a:not(.button) {   

  15. backgroundnone;   

  16. line-height58px;   

  17. padding: 0 27px;   

  18. }  

我们已经移除黑背景了,并且适应高度、行高和内边距,将文字转化为大写字母,这些都是为了适应我们自定义设计。

如果你进行刷新页面,可以看到出现雏形了,我们继续编写下拉菜单、菜单子项目、激活(active)和鼠标悬空(hover)的状态的样式吧。继续往下看代码示例,并阅读注释:

  1. /** Changes the active menu item from default black to a gradient **/  

  2. .top-bar-section ul li.active > a {   

  3. backgroundrgb(0, 0, 0);   

  4. background: linear-gradient(to bottombottom, rgba(0, 0, 0, 0.4) 0%, rgba(0, 0, 0, 0.7) 100%) repeat scroll 0 0 transparent;   

  5. color#fff;   

  6. }   

  7. /** Changes the hover state of non active menu items **/  

  8. .top-bar-section li:hover a {   

  9. background: linear-gradient(to bottombottom, rgba(0, 0, 0, 0.4) 0%, rgba(0, 0, 0, 0.7) 100%) repeat scroll 0 0 transparent;   

  10. color#fff;   

  11. }   

  12. /** Changes non active menu items text color to black **/  

  13. .top-bar-section ul li > a {   

  14. color#2d2d2d;   

  15. }   

  16. /** Changes the hover state of dropdown menu items **/  

  17. .top-bar-section ul.dropdown li a:hover:not(.button) {   

  18. backgroundnone repeat scroll 0 0 rgba(0, 0, 0, 0.9);   

  19. }   

  20. /** IMPORTANT fill for the ul dropdown container **/  

  21. .top-bar-section ul.dropdown {   

  22. background#333;   

  23. color#fff;   

  24. }   

  25. /** This fixes the position and the color of the dropdown arrow **/  

  26. .top-bar-section .has-dropdown > a:after {   

  27. border-color: rgba(0, 0, 0, 1) transparent transparent;   

  28. margin-top: 2.5px;   

  29. }  

我们已经改变菜单的好几个地方(样式),如首先,我们就改变激活(active)菜单默认的黑背景为“CSS渐变”,接着给不激活菜单添加hover状态,并改变里面的文字颜色为深灰色,目的是显示得更为清晰。然后.top-bar-section li:hover 的样式将使得下拉菜单里的子项目被鼠标悬空时显示特定效果。为了完善样式,我们ul.dropdown添加一个背景颜色和重新设置下拉菜单箭头的位置,是因为我们内边距适应.top-bar-section。

第三步:效果

重新刷新页面,看一下现在的效果,我们还没完成样式的编写,只是想看看当调整浏览器的大小时(甚至在更小的屏幕上),页面效果是否显示正常。接下来我们将添加“媒体查询”代码来实现这种效果。

编写媒体查询

第一步:媒体查询

我们需要做些操作来使得使用我们自定义样式的菜单栏能够适应不断缩小的屏幕。这里主要涉及到添加一些内边距、行高、文本颜色和背景颜色。如下CSS代码示例,查看每个section的注释。

  1.     @media only screen and (max-width942px) {   

  2.     /* Makes the responsive menu fit in the navigation container and change its background to black */  

  3.     .top-bar ul {   

  4.     background-color: rgba(0, 0, 0, 0.5);   

  5.     padding-bottom13px;   

  6.     }   

  7.     /* Change non active menu item color to black */  

  8.     .top-bar-section ul li > a {   

  9.     color#fff;   

  10.     }   

  11.     /* Gives the dropdown ul a black fill */  

  12.     .top-bar-section ul {   

  13.     background#000;   

  14.     }   

  15.     /* Give the BACK button after going in a submenu the appropriate filling */  

  16.     .top-bar-section .dropdown li.title h5 a {   

  17.     line-height57px;   

  18.     }   

  19.     /* This fixes the position and the color of the dropdown arrow */  

  20.     .top-bar-section .has-dropdown > a:after {   

  21.     border-color: rgba(255, 255, 255, 1) transparent transparent;   

  22.     margin-top: 2.5px;   

  23.     }   

  24.     } /* end media query */  

  25.   

  26. 第二步:享受你的效果   

  27.   

  28. 保存文件,然后重新刷新页面,不断地调整浏览器窗口的大小,你就会看到菜单栏都能很好地展示出来。  

总结

最后,总结一下此教程:我们如何利用Foundation5框架里的顶部工具栏来创建一个自定义菜单。需要记住的一点是,菜单栏不一定要被放置在页面的顶部的,可以使用网格系统来包含它,然后就可以放置任何你想放置的地方。

Foundation是一系列非常有用的工具可以快速地创建起响应式网站设计,当你不断地熟悉Foundation这个框架,就可以更高效地、创意地创建出更多好玩的东西出来,继续努力吧!

您的支持是我们创作的动力!
温馨提示:本文作者系Terry ,经Web前端之家编辑修改或补充,转载请注明出处和本文链接:
https://www.jiangweishan.com/article/foundation5.html

网友评论文明上网理性发言已有0人参与

发表评论: