Here are the translations:
Today I will teach you to design a colorful and mobile-friendly responsive navigation bar. This navigation bar is inspired by a set of colors called the Maliwan weapon manufacturer's trademark in a game called the Borderlands. The navigation bar automatically adjusts the layout according to the size of the browser window: it is rendered as a row of buttons under the width of the PC, rendered as a three-row button in the flat width, and a menu bar button connection on the mobile side, and click to show and hide the entire navigation bar. In order to make this navigation bar really mobile friendly, we will use the icon font as a navigation bar chart, so that when the interface zoom out, the icon will automatically adjust the resolution, to avoid aliasing (high pixels) and blur (Pixels too low).
Ready to work: Select Icon Font
The creation of the icon font is not a simple task, but there is absolutely no need for us to find the trouble for ourselves. Plugins like Icomoon or fontawesome have ready-made icons for us to choose from, and we can use these tools to quickly create the font of the icons we need. The advantage of using the icon text is that these icons, like Web fonts, can easily change the color and size of the image without the blurring of a picture that looks like it was zoomed in. So we're not going to have to prepare the same picture for the mobile side with different resolution sizes.
First we need to make the icon buttons we need (of course, you can also choose the plugin has been made good icon text). I mainly use Adobe Illustrator to make vectors, and of course other similar vector-making tools can do this task. We first need to do the required buttons in the vector drawing tool and export them to the SVG format file. To ensure proper display in all browsers, we must combine the different lines of the design element into a complete object, and then import each icon into the Icomoon App tool:
After importing these elements, click the Settings button next to the download in the lower right corner, we can configure the generated fonts in detail, including the font encoding format, assigning the corresponding characters to each font, etc. Of course, most of the time the default configuration is able to meet the needs, when we just click the download button directly, we can get the entire font package and related files.
After getting the icon font, the first thing is of course to put in our project to see the effect. The Icomoon download package provides the necessary 4 types of font file resources, corresponding to different browsers, as well as a responsive CSS style. More convenient is that it also provides a demo.html file where we can see the effect of the custom icon directly in it.
HTML implementation of the button
<nav id= "menu" class= "nav" > <ul> <li> <a href= "#" title= "> <span class= "icon" > <i aria-hidden= "true" class= "Icon-home" ></i></span><span>home </span> </a> </li> <li> <a href= "#" title= "" ><span cl ass= "icon" > <i aria-hidden= "true" class= "Icon-services" ></i></span><span>services</ span></a> </li> <li> <a href= "#" title= "" ><span class= "icon" ><i ar Ia-hidden= "true" class= "Icon-portfolio" ></i></span><span>Portfolio</span></a> </li><li> <a href= "#" title= "" ><span class= "icon" ><i aria-hidden= "true" class= "Icon-blog" ></i></span><span>Blog</span></a></li><li> <a href= "#" title= "" ><span class= "icon" ><i aria-hidden= "true" class= "Icon-team" ></i></span><span>the team</span></a></li><li> <a href= "#" title= "" ><span class= "icon" ><i aria-hidden= "true" class= "Icon-contact" ></i></span ><span>Contact</span></a> </li> </ul></nav>
The way to use an icon font in a Web page is simply to add a class attribute called "Icon-iconname" to an I tag or span tag element. To mention here, we will generally add no-js this class property in the body tag, when using Modernizr, if the browser does not disable JavaScript, then Modernizr will automatically change the No-js to JS. The advantage of this is that if the browser disables the script, we can use this class property to make the navigation bar default in the expanded state. Modernizr is very powerful, and we can also use it to detect how mobile devices support touch screens and respond to strategies.
Implementation of CSS and JavaScript
/* 全局CSS配置,将在所有设备下生效 */.nav ul {max-width: 1240px;margin: 0;padding: 0;list-style: none;font-size: 1.5em;font-weight: 300;}.nav li span {display: block;}.nav a {display: block;color: rgba(249, 249, 249, .9);text-decoration: none;transition: color .5s, background .5s, height .5s;}.nav i{/* 确保图标文字在谷歌浏览器下显示清晰 */transform: translate3d(0, 0, 0);}/* 去掉webkit内核下点击按钮出现的蓝色背景 */a, button {-webkit-tap-highlight-color: rgba(0,0,0,0);}
We first add a mouse hover effect to the button, and when the mouse hovers over the button, the button becomes translucent.
/* PC端鼠标悬停在按钮上方时的特效 */.no-touch .nav ul:hover a {color: rgba(249, 249, 249, .5);}.no-touch .nav ul:hover a:hover {color: rgba(249, 249, 249, 0.99);}
Here's a way to give each of the LI elements different display effects without adding different class attributes, which is li:nth-child. We use this method to give each button in the list a different color.
.nav li:nth-child(6n+1) {background: rgb(208, 101, 3);}.nav li:nth-child(6n+2) {background: rgb(233, 147, 26);}.nav li:nth-child(6n+3) {background: rgb(22, 145, 190);}.nav li:nth-child(6n+4) {background: rgb(22, 107, 162);}.nav li:nth-child(6n+5) {background: rgb(27, 54, 71);}.nav li:nth-child(6n+6) {background: rgb(21, 40, 54);}
Next, we introduce the basis of the responsive interface design, which is the application of @media. Here in passing a brief introduction of the EM Unit, EM refers to the body global font size, if the body global font size is 15px, then 50em is equal to 800px. The advantage of this is that when the body font is adjusted, the entire page size can be changed dynamically, which facilitates the adaptation of the mobile side.
@media (min-width: 50em) {/* 当屏幕的宽度小于50em时,将水平的导航栏转换成垂直的列表格式 */.nav li {float: left;width: 16.66666666666667%;text-align: center;transition: border .5s;}.nav a {display: block;width: auto;}
We add the bottom edge of the color to each button by selecting the click. Add the focus and active options so that you can see the effect when the mobile device is selected and the keyboard is selected.
/* Color bottom */.no-touch appears with hover, check, and click. Nav li:nth-child (6n+1) A:hover,.no-touch. Nav li:nth-child (6n+1) a:active,. No-touch. Nav li:nth-child (6n+1) a:focus {border-bottom:4px solid RGB (174, 78, 1);}. No-touch. Nav li:nth-child (6n+2) A:hover,.no-touch. Nav li:nth-child (6n+2) A:active,.no-touch. Nav li:nth-child (6n+2) A : Focus {BORDER-BOTTOM:4PX solid RGB (191, 117, 20);}. No-touch. Nav li:nth-child (6n+3) A:hover,.no-touch. Nav li:nth-child (6n+3) A:active,.no-touch. Nav li:nth-child (6n+3) A : Focus {BORDER-BOTTOM:4PX solid RGB (12, 110, 149);}. No-touch. Nav li:nth-child (6n+4) A:hover,.no-touch. Nav li:nth-child (6n+4) A:active,.no-touch. Nav li:nth-child (6n+4) A : Focus {BORDER-BOTTOM:4PX solid RGB (10, 75, 117);}. No-touch. Nav li:nth-child (6n+5) A:hover,.no-touch. Nav li:nth-child (6n+5) A:active,.no-touch. Nav li:nth-child (6n+5) A : Focus {BORDER-BOTTOM:4PX solid RGB (16, 34, 44);}. No-touch. Nav li:nth-child (6n+6) A:hover,.no-touch. Nav li:nth-child (6n+6) A:active,.no-touch. Nav li:nth-child (6n+6) A: Focus {BORDER-BOTTOM:4PX solid RGB (9, 18, 25);}
Adjust the relative position and gradient of the icon:
/* 调整图标的位置 */.icon {padding-top: 1.4em;}.icon + span {margin-top: 2.1em;transition: margin .5s;}
When the mouse hovers over the button, the height of the element is changed to produce an animated effect:
/* 鼠标悬停的时候出现高度变化动画 */.nav a {height: 9em;}.no-touch .nav a:hover ,.no-touch .nav a:active ,.no-touch .nav a:focus {height: 10em;}/* 这里让文字跟着整个元素的高度变化而变化 */.no-touch .nav a:hover .icon + span {margin-top: 3.2em;transition: margin .5s;}
Adjust the position of the button and design the CSS gradient effect:
/* 调整按钮的位置,并设计CSS渐变效果 */.nav i {position: relative;display: inline-block;margin: 0 auto;padding: 0.4em;border-radius: 50%;font-size: 1.8em;box-shadow: 0 0 0 0.8em transparent;background: rgba(255,255,255,0.1);transform: translate3d(0, 0, 0);transition: box-shadow .6s ease-in-out;}
To get the effect we want, we change the shadow of the box model and transform its size from 0.8em to 0, shifting its color from transparent to a relatively opaque state.
/* 盒模型阴影动画效果 */.no-touch .nav a:hover i,.no-touch .nav a:active i,.no-touch .nav a:focus i {box-shadow: 0 0 0px 0px rgba(255,255,255,0.2);transition: box-shadow .4s ease-in-out;}}
Slightly adjust the position of the screen width between 800-980px:
@media (min-width: 50em) and (max-width: 61.250em) {/* Size and font adjustments to make it fit better */.nav ul {font-size: 1.2em;}}
This is finished PC-side design, of course, now many tablets and even mobile phone resolution has reached more than 1024px, PC and mobile end of the line is increasingly blurred.
/* Design 800px below screen layout style and effects */@media (MAX-WIDTH:49.938EM) {/* multi-row buttons can not be changed border effects, we change the background transparency of the effect */.no-touch. Nav ul Li: Nth-child (6n+1) A:hover,.no-touch. Nav ul li:nth-child (6n+1) A:active,.no-touch. Nav ul li:nth-child (6n+1) A:focus { Background:rgb (227, 119, 20);}. No-touch. Nav li:nth-child (6n+2) A:hover,.no-touch. Nav li:nth-child (6n+2) A:active,.no-touch. Nav li:nth-child (6n+2) A : Focus {Background:rgb (245, 160, 41);}. No-touch. Nav li:nth-child (6n+3) A:hover,.no-touch. Nav li:nth-child (6n+3) A:active,.no-touch. Nav li:nth-child (6n+3) A : Focus {Background:rgb (44, 168, 219);}. No-touch. Nav li:nth-child (6n+4) A:hover,.no-touch. Nav li:nth-child (6n+4) A:active,.no-touch. Nav li:nth-child (6n+4) A : Focus {Background:rgb (31, 120, 176);}. No-touch. Nav li:nth-child (6n+5) A:hover,.no-touch. Nav li:nth-child (6n+5) A:active,.no-touch. Nav li:nth-child (6n+5) A : Focus {Background:rgb (39, 70, 90);}. No-touch. Nav li:nth-child (6n+6) A:hover,.no-touch. Nav li:nth-child (6n+6) A:active,.no-touch. Nav li:nTh-child (6n+6) A:focus {Background:rgb (32, 54, 68);}. Nav ul li {transition:background 0.5s;}}
When the screen size is between 520px and 799px, let the navigation bar layout in the form of 2x3.
/* 导航栏按照2X3的形式布局 */@media (min-width: 32.5em) and (max-width: 49.938em) {/* 用左浮动的方法实现两个按钮并排 */.nav li {display: block;float: left;width: 50%;}/* 加入padding使得元素按钮看上去好看些 */.nav a {padding: 0.8em;}/* 用inline-block可以让按钮在左边,文字在右边 */.nav li span, .nav li span.icon {display: inline-block;}.nav li span.icon {width: 50%;}.nav li .icon + span {font-size: 1em;}.icon + span {position: relative;top: -0.2em;}
The change in the size of the element on the PC cannot be achieved in the flat layout, so we can simplify the animation effect.
/* 在平板布局的情形下,我们以一种更为保守的方式设计动画,只改变一下border的大小 */.nav li i {display: inline-block;padding: 8% 9%;border: 4px solid transparent;border-radius: 50%;font-size: 1.5em;background: rgba(255,255,255,0.1);transition: border .5s;}/* border颜色渐变效果 */.no-touch .nav li:hover i,.no-touch .nav li:active i,.no-touch .nav li:focus i {border: 4px solid rgba(255,255,255,0.1);}}
Adjust the size and width of the text under flat-panel layout
/* 平板布局下,调整文字的大小和宽度 */@media (min-width: 32.5em) and (max-width: 38.688em) {.nav li span.icon {width: 50%;}.nav li .icon + span {font-size: 0.9em;}}
For smaller mobile devices, we can hide the entire navigation bar and then display a navigation button to show and hide the navigation bar by clicking the navigation button. This feature may need to be implemented via JavaScript. (Translator: A simpler approach is to use jquery's Toggleclass method).
The way to toggle the Class property is var Changeclass = function (r,classname1,classname2) {var regex = new RegExp (?: ^| \\s+) "+ ClassName1 +" (?: \ \s+|$), if (Regex.test (R.classname)) {r.classname = R.classname.replace (regex, ' +classname2+ '); } else{r.classname = R.classname.replace (New RegExp (?: ^| \\s+) "+ ClassName2 +" (?: \ \s+|$) "), ' +classname1+ '); } return r.classname;};/ /write menu bar button var menuelements = document.getElementById (' menu '); menuelements.insertadjacenthtml (' Afterbegin ', ' < Button type= "button" id= "Menutoggle" class= "Navtoogle" aria-hidden= "true" ><i aria-hidden= "true" class= " Icon-menu "> </i> menu</button> ');//Toggle the Class Property document.getElementById (' Menutoggle ') when you click the menu bar button. onclick = function () {Changeclass (this, ' navtoogle active ', ' navtoogle ');} Automatically hide the navigation bar when you click another location in the document Document.onclick = function (e) {var Mobilebutton = document.getElementById (' Menutoggle '), ButtonStyle = Mobilebutton.currentstyle? MobileButton.currentStyle.display:getComputedStyle (Mobilebutton, nulL). display;if (ButtonStyle = = = ' block ' && e.target!== mobilebutton && new RegExp (' + ' active ' + '). tes T (' + mobilebutton.classname + ')) {Changeclass (Mobilebutton, ' navtoogle active ', ' navtoogle ');}}
This paragraph uses the JavaScript native method to carry on the class attribute switch and the click Action carries on the DOM document operation realization. When the button is clicked, the first thing is to determine what property the button has, and then change it to another property, so that the class attribute is switched. After the navigation bar is hidden and displayed, we then set the style.
/* 导航栏按钮的样式,默认隐藏 */.nav .navtoogle{display: none;width: 100%;padding: 0.5em 0.5em 0.8em;font-family: ‘Lato‘,Calibri,Arial,sans-serif;font-weight: normal;text-align: left;color: rgb(7, 16, 15);font-size: 1.2em;background: none;border: none;border-bottom: 4px solid rgb(221, 221, 221);cursor: pointer;}.navtoogle i{z-index:-1;}.icon-menu {position: relative;top: 3px;line-height: 0;font-size: 1.6em;}
If the screen is detected to be less than 519px, the navigation bar button is displayed
@media (max-width: 32.438em) {/* 显示导航栏按钮 */.nav .navtoogle{margin: 0;display: block;}
Next we implement the navigation bar display and hide animation. We control the display and hiding through the height of the element. In particular, if the page is detected to disable the script, we should make the navigation bar display by default.
/* 如果禁用了脚本,那么导航栏总是显示的。 */.no-js .nav ul {max-height: 30em;overflow: hidden;}/* 如果脚本是可生效的,那么我们控制高度为0,隐藏之 */.js .nav ul {max-height: 0em;overflow: hidden;}/* class的active属性激活之后,给max-height一个动画效果,就能够实现导航栏向下滚动出现的特效 */.js .nav .active + ul {max-height: 30em;overflow: hidden;transition: max-height .4s;}
Then set the style for this small screen navigation bar
/* 小屏幕导航栏样式设置 */.nav li span {display: inline-block;height: 100%;}.nav a {padding: 0.5em;}.icon + span {margin-left: 1em;font-size: 0.8em;}
Add a border effect to the left (designers need to pay more attention to these details:))
/* 左侧增加一个border效果 */.nav li:nth-child(6n+1) {border-left: 8px solid rgb(174, 78, 1);}.nav li:nth-child(6n+2) {border-left: 8px solid rgb(191, 117, 20);}.nav li:nth-child(6n+3) {border-left: 8px solid rgb(13, 111, 150);}.nav li:nth-child(6n+4) {border-left: 8px solid rgb(10, 75, 117);}.nav li:nth-child(6n+5) {border-left: 8px solid rgb(16, 34, 44);}.nav li:nth-child(6n+6) {border-left: 8px solid rgb(9, 18, 25);}
The rest of the work may be about adjusting the effects of mobile devices. We use the Modernizr tool to help us identify the compatibility of mobile devices, so that we can make adjustments to the feature support or devices that are not supported by the feature by using the Class property. For example, the button on the navigation bar appears normal under the PC, but the same style may seem too small to press on the phone device. This is how we can adjust the style.
/* 让导航栏在触屏设备下看起来更舒适一些 */.touch .nav a {padding: 0.8em;}}
So we finished a pretty beautiful CSS3 navigation bar production, how, is not very simple ah?
"Translation" mobile-friendly responsive navigation bar tutorial