Advertisement
  1. Web Design
  2. HTML/CSS
  3. CSS

Félrecsúszó oldalsáv menü készítése reszponzív dizájnhoz

Scroll to top
Read Time: 5 min

() translation by (you can also view the original English article)

Ebben a bemutatóban egy kiszélesíthető oldalsó navigációs menüt fogsz készíteni JavaScript és CSS segítségével. A végső termék úgy fog kinézni, ahogyan azt alább láthatod:

Sliding Side Navigation MenuSliding Side Navigation MenuSliding Side Navigation Menu

A forráskód

Kezdésként adjunk hozzá egy kis kódot az oldalsávhoz:

1
  <div id="sideNavigation" class="sidenav">
2
    <a href="javascript:void(0)" class="closebtn" onclick="closeNav()">&times;</a>
3
    <a href="#">About</a>
4
    <a href="#">Features</a>
5
    <a href="#">Contact Us</a>
6
  </div>
7
8
  <nav class="topnav">
9
    <a href="#" onclick="openNav()">
10
      <svg width="30" height="30" id="icoOpen">
11
          <path d="M0,5 30,5" stroke="#000" stroke-width="5"/>
12
          <path d="M0,14 30,14" stroke="#000" stroke-width="5"/>
13
          <path d="M0,23 30,23" stroke="#000" stroke-width="5"/>
14
      </svg>
15
    </a>
16
  </nav>
17
  
18
  <div id="main">
19
  <!-- Add all your websites page content here  -->
20
  </div>

Láthatod, hogy létrehoztunk egy oldalsó menü divet sidenav osztállyal. Ezután hozzáadtuk a tényleges felső sávos navigációt egy <nav> taggel, és SVG-t használtunk az oldalsáv menüikonjaiként.

Az ikon onclick attribútuma és a bezárás gomb JavaScriptre fog élesedni, amit a következőkben adunk hozzá.

Figyelj rá, hogy a weboldalad tartalmát a div id="main" konténeren belül helyezd el, mert az így fog csak jobbra csúszni.

JavaScript

Ezután add hozzá a JavaScriptet az openNav és a closeNav függvények elkészítése érdekében.

1
<script>
2
function openNav() {
3
    document.getElementById("sideNavigation").style.width = "250px";
4
    document.getElementById("main").style.marginLeft = "250px";
5
}
6
7
function closeNav() {
8
    document.getElementById("sideNavigation").style.width = "0";
9
    document.getElementById("main").style.marginLeft = "0";
10
}
11
</script>

CSS

Végül szükségünk lesz az oldal stilizálására némi CSS-sel az oldalsávhoz és a linkjeinkhez:

1
/* The side navigation menu */
2
.sidenav {
3
    height: 100%; /* 100% Full-height */
4
    width: 0; /* 0 width - change this with JavaScript */
5
    position: fixed; /* Stay in place */
6
    z-index: 1; /* Stay on top */
7
    top: 0;
8
    left: 0;
9
    background-color: #111; /* Black*/
10
    overflow-x: hidden; /* Disable horizontal scroll */
11
    padding-top: 60px; /* Place content 60px from the top */
12
    transition: 0.5s; /* 0.5 second transition effect to slide in the sidenav */
13
}
14
15
/* The navigation menu links */
16
.sidenav a {
17
    padding: 8px 8px 8px 32px;
18
    text-decoration: none;
19
    font-size: 25px;
20
    color: #818181;
21
    display: block;
22
    transition: 0.3s
23
}
24
25
/* When you mouse over the navigation links, change their color */
26
.sidenav a:hover, .offcanvas a:focus{
27
    color: #f1f1f1;
28
}
29
30
/* Position and style the close button (top right corner) */
31
.sidenav .closebtn {
32
    position: absolute;
33
    top: 0;
34
    right: 25px;
35
    font-size: 36px;
36
    margin-left: 50px;
37
}
38
39
/* Style page content - use this if you want to push the page content to the right when you open the side navigation */
40
#main {
41
    transition: margin-left .5s;
42
    padding: 20px;
43
    overflow:hidden;
44
    width:100%;
45
}
46
body {
47
  overflow-x: hidden;
48
}
49
50
/* Add a black background color to the top navigation */
51
.topnav {
52
    background-color: #333;
53
    overflow: hidden;
54
}
55
56
/* Style the links inside the navigation bar */
57
.topnav a {
58
    float: left;
59
    display: block;
60
    color: #f2f2f2;
61
    text-align: center;
62
    padding: 14px 16px;
63
    text-decoration: none;
64
    font-size: 17px;
65
}
66
67
/* Change the color of links on hover */
68
.topnav a:hover {
69
    background-color: #ddd;
70
    color: black;
71
}
72
73
/* Add a color to the active/current link */
74
.topnav a.active {
75
    background-color: #4CAF50;
76
    color: white;
77
}
78
79
/* On smaller screens, where height is less than 450px, change the style of the sidenav (less padding and a smaller font size) */
80
@media screen and (max-height: 450px) {
81
    .sidenav {padding-top: 15px;}
82
    .sidenav a {font-size: 18px;}
83
}
84
85
a svg{
86
  transition:all .5s ease;
87
88
  &:hover{
89
    #transform:rotate(180deg);
90
  }
91
}
92
93
#ico{
94
  display: none;
95
}
96
97
.menu{
98
  background: #000;
99
  display: none;
100
  padding: 5px;
101
  width: 320px;
102
  @include border-radius(5px);
103
104
  #transition: all 0.5s ease;
105
106
  a{
107
    display: block;
108
    color: #fff;
109
    text-align: center;
110
    padding: 10px 2px;
111
    margin: 3px 0;
112
    text-decoration: none;
113
    background: #444;
114
115
    &:nth-child(1){
116
      margin-top: 0;
117
      @include border-radius(3px 3px 0 0 );
118
    }
119
    &:nth-child(5){
120
      margin-bottom: 0;
121
      @include border-radius(0 0 3px 3px);
122
    }
123
124
    &:hover{
125
      background: #555;
126
    }
127
  }
128
}

Megjegyzés: A body {overflow-x: hidden;} kötelező, hogy a vízszintes csúszka ne jelenjen meg, amikor a már létező CSS-eddel együtt használjuk.

Most vess egy pillantást a menüdre és próbáld ki a böngésződben.

jQuery használata

Ha szeretnéd létrehozni az oldalsáv JavaScriptjét jQuery használatával, az meg tudod tenni úgy, hogy az általam korábban megadott JavaScriptet lecseréled a következő résszel:

1
$('.topnav a').click(function(){
2
  $('#sideNavigation').style.width = "250px";
3
  $("#main").style.marginLeft = "250px";
4
});
5
6
$('.closebtn').click(function(){
7
  $('#sideNavigation').style.width = "0";
8
  $("#main").style.marginLeft = "0";
9
});

A csúszka eltávolítása

Ha azt akarod, hogy a menü csúszka animáció nélkül jelenjen meg, egyszerűen csak változtasd meg a transition CSS tulajdonságot, ahogyan azt rövidített formában alább láthatod:

1
.sidenav {
2
    transition: 0s; 
3
}
4
5
#main {
6
    transition: margin-left 0s;
7
}

Ennek következtében a változás azonnali lesz, mert nem lesz a transitionnál megadott késleltetés. Mi az alapértelmezett 0.5s-t használtuk.

Konklúzió

Egy félrecsúszó oldalsáv elkészítése csak pár sornyi kódba kerül, és nem kell hozzá sok erőforrás. Ha a jQuery már az oldalon van más feladatok miatt, a feladat alig pár kódsorral megoldható és tovább módosítható.

A kód reszponzívvá tétele, hogy működjön különféle eszközök képernyőfelbontásán, csupán a CSS módosításán múlik azáltal, hogy a speciális eseteknek megfelelő media query-ket adunk hozzá.

Ezt tovább is lehet vinni, esetleg a menüdet stilizálhatod egy olyan CSS keretrendszerrel, mint a Bootstrap vagy a Bulma.

Advertisement
Did you find this post useful?
Want a weekly email summary?
Subscribe below and we’ll send you a weekly email summary of all new Web Design tutorials. Never miss out on learning about the next big thing.
Advertisement
Looking for something to help kick start your next project?
Envato Market has a range of items for sale to help get you started.