A tutorial on how to recreate the effect of YouTube's little left side menu. The idea is to slide a little menu icon to the right side while revealing some menu item list beneath.
How to recreate the little menu effect that you can see in the left side-menu on YouTube when watching a video (where it says “Guide”). The menu is made of a little menu icon, a label and a list of menu items that appears when the label or menu icon is clicked. Once it’s clicked the menu icon slides to the right and the label moves up while the list items fade in sequentially. We’ll add some more style and effects to it in order to make it a bit more interesting.
The Markup
For the HTML we will use anav
element and inside we’ll
add a div that will contain the menu icon and the label. We’ll use an
unordered list for the menu items:
1
2
3
4
5
6
7
8
9
10
11
12
| < nav id = "dr-menu" class = "dr-menu" > < div class = "dr-trigger" >< span class = "dr-icon dr-icon-menu" ></ span >< a class = "dr-label" >Account</ a ></ div > < ul > < li >< a class = "dr-icon dr-icon-user" href = "#" >Xavier Densmore</ a ></ li > < li >< a class = "dr-icon dr-icon-camera" href = "#" >Videos</ a ></ li > < li >< a class = "dr-icon dr-icon-heart" href = "#" >Favorites</ a ></ li > < li >< a class = "dr-icon dr-icon-bullhorn" href = "#" >Subscriptions</ a ></ li > < li >< a class = "dr-icon dr-icon-download" href = "#" >Downloads</ a ></ li > < li >< a class = "dr-icon dr-icon-settings" href = "#" >Settings</ a ></ li > < li >< a class = "dr-icon dr-icon-switch" href = "#" >Logout</ a ></ li > </ ul > </ nav > |
Let’s take a look at the CSS.
The CSS
Note that the CSS will not contain any vendor prefixes, but you will find them in the files.First, we will include our icon font:
1
2
3
4
5
6
7
8
9
10
| @font-face { font-family : 'icomoon' ; src : url ( '../fonts/icomoon.eot' ); src : url ( '../fonts/icomoon.eot?#iefix' ) format ( 'embedded-opentype' ), url ( '../fonts/icomoon.woff' ) format ( 'woff' ), url ( '../fonts/icomoon.ttf' ) format ( 'truetype' ), url ( '../fonts/icomoon.svg#icomoon' ) format ( 'svg' ); font-weight : normal ; font-style : normal ; } |
1
2
3
4
5
6
7
8
9
10
11
| .dr-menu { width : 100% ; max-width : 400px ; min-width : 300px ; position : relative ; font-size : 1.3em ; line-height : 2.5 ; font-weight : 400 ; color : #fff ; padding-top : 2em ; } |
1
2
3
4
5
6
| .dr-menu > div { cursor : pointer ; position : absolute ; width : 100% ; z-index : 100 ; } |
1
2
3
4
5
6
7
8
9
| .dr-menu > div .dr- icon { top : 0 ; left : 0 ; position : absolute ; font-size : 150% ; line-height : 1.6 ; padding : 0 10px ; transition : all 0.4 s ease; } |
1
2
3
4
5
| .dr-menu.dr-menu-open > div .dr- icon { color : #60a773 ; left : 100% ; transform : translateX ( -100% ); } |
1
2
3
4
5
6
7
8
| .dr-menu > div .dr-icon:after { content : "\e008" ; position : absolute ; font-size : 50% ; line-height : 3.25 ; left : -10% ; opacity : 0 ; } |
Once we open the menu, we will want it to become visible:
1
2
3
| .dr-menu.dr-menu-open > div .dr-icon:after { opacity : 1 ; } |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
| .dr-menu > div .dr-label { padding-left : 3em ; position : relative ; display : block ; color : #60a773 ; font-size : 0.9em ; font-weight : 700 ; letter-spacing : 1px ; text-transform : uppercase ; line-height : 2.75 ; transition : all 0.2 s ease-in; } .dr-menu.dr-menu-open > div .dr-label { transform : translateY ( -90% ); } |
1
2
3
4
5
6
7
8
9
10
| .dr-menu ul { padding : 0 ; margin : 0 3em 0 0 ; list-style : none ; opacity : 0 ; position : relative ; z-index : 0 ; pointer-events : none ; transition : opacity 0 s linear 205 ms; } |
1
2
3
4
5
6
| .dr-menu.dr-menu-open ul { opacity : 1 ; z-index : 200 ; pointer-events : auto ; transition : opacity 0 s linear 0 s; } |
The list items will also be invisible and we’ll set a transition for the opacity:
1
2
3
4
5
6
7
8
9
10
| .dr-menu ul li { display : block ; margin : 0 0 5px 0 ; opacity : 0 ; transition : opacity 0.3 s ease; } .dr-menu.dr-menu-open ul li { opacity : 1 ; } |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
| .dr-menu.dr-menu-open ul li:nth-child( 2 ) { transition-delay : 35 ms; } .dr-menu.dr-menu-open ul li:nth-child( 3 ) { transition-delay : 70 ms; } .dr-menu.dr-menu-open ul li:nth-child( 4 ) { transition-delay : 105 ms; } .dr-menu.dr-menu-open ul li:nth-child( 5 ) { transition-delay : 140 ms; } .dr-menu.dr-menu-open ul li:nth-child( 6 ) { transition-delay : 175 ms; } .dr-menu.dr-menu-open ul li:nth-child( 7 ) { transition-delay : 205 ms; } |
1
2
3
4
5
| .dr-menu ul li a { display : inline-block ; padding : 0 20px ; color : #fff ; } |
1
2
3
| .dr-menu ul li a:hover { color : #60a773 ; } |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
| .dr-icon:before, .dr-icon:after { position : relative ; font-family : 'icomoon' ; speak : none ; font-style : normal ; font-weight : normal ; font-variant : normal ; text-transform : none ; -webkit- font-smoothing : antialiased ; } .dr-menu ul .dr-icon:before { margin-right : 15px ; } .dr-icon-bullhorn:before { content : "\e000" ; } .dr-icon-camera:before { content : "\e002" ; } .dr-icon-heart:before { content : "\e003" ; } .dr-icon-settings:before { content : "\e004" ; } .dr-icon-switch:before { content : "\e005" ; } .dr-icon-download:before { content : "\e006" ; } .dr-icon-user:before { content : "\e001" ; } .dr-icon-menu:before { content : "\e007" ; } |
The JavaScript
We’ll create a small script that will take care of the menu functionality. When we click on the trigger division, we want the menu wrapper to get the class “dr-menu-open”. Since we will be animating the label out and the menu icon to the right, we want the closing to happen only when we click on the menu icon, just like in the YouTube menu:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
| var YTMenu = ( function () { function init() { [].slice.call( document.querySelectorAll( '.dr-menu' ) ).forEach( function ( el, i ) { var trigger = el.querySelector( 'div.dr-trigger' ), icon = trigger.querySelector( 'span.dr-icon-menu' ), open = false ; trigger.addEventListener( 'click' , function ( event ) { if ( !open ) { el.className += ' dr-menu-open' ; open = true ; } }, false ); icon.addEventListener( 'click' , function ( event ) { if ( open ) { event.stopPropagation(); open = false ; el.className = el.className.replace(/\bdr-menu-open\b/, '' ); return false ; } }, false ); } ); } init(); })(); |
Source: http://tympanus.net/codrops/2013/04/25/simple-youtube-menu-effect/?utm_source=feedburner&utm_medium=feed&utm_campaign=Feed%3A+tympanus+%28Codrops%29
0 komentar:
Post a Comment