bootstrap4滑动侧边栏特效代码

bootstrap4滑动侧边栏特效代码

这是一款基于bootstrap4的滑动侧边栏特效。该滑动侧边栏使用bootstrap4框架,通过css和jquery来完成隐藏侧边栏,当点击打开侧边栏按钮时,侧边栏会以动画的方式滑动出来。

使用方法
在页面中引入jquery和bootstrap4相关文件。

1
2
3
<link href="css/bootstrap.min.css" rel="stylesheet">
<script src="js/jquery-1.11.0.min.js" type="text/javascript"></script>
<script src="js/bootstrap.min.js"></script>

HTML结构
侧边栏的HTML结构如下:

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
<nav class="navbar navbar-expand-md navbar-dark bg-dark navbar-offcanvas">
  <div class="container-fluid">
      <a class="navbar-brand" href="#">Navbar</a>
      <ul class="navbar-nav navbar-top">
          <li class="nav-item active">
              <a class="nav-link" href="#">Home <span class="sr-only">(current)</span></a>
          </li>
          <li class="nav-item">
              <a class="nav-link" href="#">Link</a>
          </li>
      </ul>
      <button class="navbar-toggler navbar-toggler-right navbar-icon" type="button" data-toggle="collapse" data-target="#navbar-mobile" aria-controls="navbar-mobile" aria-expanded="false" aria-label="Toggle navigation">
          <span class="icon-bar bar1"></span>
          <span class="icon-bar bar2"></span>
          <span class="icon-bar bar3"></span>
      </button>
      <div class="navbar-collapse collapse ml-auto" id="navbar-mobile">
          <ul class="navbar-nav ml-auto">
              <li class="nav-image">
                  <img src="logo.png" alt="">
              </li>
              <li class="nav-item">
                  <a href="#!" class="nav-link">Link 1</a>
              </li>
              <li class="nav-item">
                  <a href="#!" class="nav-link">Link 2</a>
              </li>
              <li class="nav-item">
                  <a href="#!" class="nav-link">Link 3</a>
              </li>
          </ul>
      </div>
  </div>
</nav>

页面的内容放置在wrapperclass的div容器中。

1
2
3
<div class="wrapper">
...
</div>

CSS样式
为侧边栏添加下面的CSS样式。

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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
.wrapper { transition: all 0.5s cubic-bezier(0.685, 0.0473, 0.346, 1); }
.navbar {
  height: 75px;
  transition: all .5s .1s;
}
.navbar-offcanvas { z-index: 1030; }
.navbar-offcanvas .container-fluid {
  position: relative;
  padding: 0;
  transform: translate3d(0px, 0, 0);
  transition: all 0.5s cubic-bezier(0.685, 0.0473, 0.346, 1);
}
.navbar-offcanvas .navbar-top { display: none; }
@media (min-width: 992px) {
 
.navbar-offcanvas .navbar-top {
  display: flex;
  margin-left: auto;
}
}
.navbar-offcanvas .navbar-top .nav-item {
  margin-right: 22px;
  text-align: center;
}
@media (max-width: 991px) {
.navbar-offcanvas .navbar-top .nav-item .nav-link { color: #000; }
}
.navbar-offcanvas .navbar-toggler {
  padding: 0;
  border: 0;
  outline: none;
}
.navbar-offcanvas .navbar-toggler:hover, .navbar-offcanvas .navbar-toggler:focus { cursor: pointer; }
 
@media (min-width: 768px) {
 
.navbar-offcanvas .navbar-toggler { display: block; }
}
 
.navbar-offcanvas .navbar-toggler .icon-bar {
  display: block;
  position: relative;
  width: 24px;
  height: 2px;
  border-radius: 1px;
  background-color: #fff;
}
 
.navbar-offcanvas .navbar-toggler .icon-bar + .icon-bar { margin-top: 4px; }
 
.navbar-offcanvas .navbar-toggler .icon-bar.bar1 {
  top: 0;
  outline: 1px solid transparent;
  animation: topbar-back 500ms 0s;
  animation-fill-mode: forwards;
}
 
.navbar-offcanvas .navbar-toggler .icon-bar.bar2 {
  outline: 1px solid transparent;
  opacity: 1;
}
 
.navbar-offcanvas .navbar-toggler .icon-bar.bar3 {
  bottom: 0;
  outline: 1px solid transparent;
  animation: bottombar-back 500ms 0s;
  animation-fill-mode: forwards;
}
 
.navbar-offcanvas .navbar-toggler.toggled .icon-bar.bar1 {
  top: 6px;
  animation: topbar-x 500ms 0s;
  animation-fill-mode: forwards;
}
 
.navbar-offcanvas .navbar-toggler.toggled .icon-bar.bar2 { opacity: 0; }
 
.navbar-offcanvas .navbar-toggler.toggled .icon-bar.bar3 {
  bottom: 6px;
  animation: bottombar-x 500ms 0s;
  animation-fill-mode: forwards;
}
 
.navbar-offcanvas .navbar-collapse.collapse, .navbar-offcanvas .navbar-collapse.collapse.in, .navbar-offcanvas .navbar-collapse.collapsing { display: none !important; }
 
.nav-open .navbar-collapse { transform: translate3d(0px, 0, 0); }
 
.nav-open .navbar > .container-fluid { transform: translate3d(-424px, 0, 0); }
 
@media (max-width: 991px) {
 
.nav-open .navbar > .container-fluid { transform: translate3d(-282.6666666667px, 0, 0); }
}
 
.nav-open .wrapper { transform: translate3d(-150px, 0, 0); }
 
body > .navbar-collapse {
  display: block !important;
  position: fixed;
  top: 0;
  right: -10px;
  width: 424px;
  height: 100%;
  padding: 60px 1rem;
  background-color: #fff;
 border-left: 1px solid #e3e3e3;
 text-align: center;
  visibility: visible;
  overflow-y: visible;
  transform: translate3d(424px, 0, 0);
  transition: all 0.5s cubic-bezier(0.685, 0.0473, 0.346, 1);
  z-index: 1032;
}
 
body > .navbar-collapse:after {
  content: "";
  position: absolute;
  top: 28px;
  left: -20px;
  border-left: 10px solid #fff;
 border-top: 10px solid transparent;
  border-bottom: 10px solid transparent;
  border-right: 10px solid transparent;
  transform: rotate(180deg);
  transition: all 0.5s cubic-bezier(0.685, 0.0473, 0.346, 1);
  z-index: 1032;
}
 
@media (max-width: 991px) {
 
body > .navbar-collapse { width: 282.6666666667px; }
}
 
body > .navbar-collapse .nav-image { margin-bottom: 65px; }
 
body > .navbar-collapse .nav-image img {
  display: block;
  margin: 0 auto;
  border: 1px solid rgba(0, 0, 0, 0.14);
  width: 91px;
  height: 69px;
}
 
body > .navbar-collapse .navbar-top {
  margin: 0 !important;
  flex-direction: column;
}
 
@media (min-width: 992px) {
 
body > .navbar-collapse .navbar-top { display: none; }
}
 
body > .navbar-collapse .navbar-top li { text-align: center; }
 
body > .navbar-collapse .navbar-top li a {
  display: block;
 padding: .5rem 1rem;
  font-weight: 700;
  color: #000;
}
 
body > .navbar-collapse .navbar-top li a:hover, body > .navbar-collapse .navbar-top li a:focus { text-decoration: none; }
 
body > .navbar-collapse .nav-link, body > .navbar-collapse .dropdown-toggle {
  font-weight: 700;
  color: #222;
 transition: color .2s ease-out;
}
 
body > .navbar-collapse .nav-link:hover, body > .navbar-collapse .nav-link:focus, body > .navbar-collapse .dropdown-toggle:hover, body > .navbar-collapse .dropdown-toggle:focus { text-decoration: none; }
 
body > .navbar-collapse .nav-link.disabled, body > .navbar-collapse .dropdown-toggle.disabled { color: rgba(0, 0, 0, 0.35); }
 
body > .navbar-collapse .nav-link.disabled:hover, body > .navbar-collapse .nav-link.disabled:focus, body > .navbar-collapse .dropdown-toggle.disabled:hover, body > .navbar-collapse .dropdown-toggle.disabled:focus { cursor: not-allowed; }
 @media (min-width: 992px) {
 
body > .navbar-top { display: none; }
}
 
body > #overlay {
 content: "";
  position: fixed;
  top: 0;
  left: auto;
  right: calc(282.6666666667px - 10px);
  width: 100%;
  height: 100%;
  opacity: 0;
  overflow-x: hidden;
  z-index: 1029;
}
 
@media (min-width: 992px) {
 
body > #overlay { right: calc(424px - 10px); }
}
 
@keyframes
topbar-x {  0% {
 top: 0px;
 transform: rotate(0deg);
}
 45% {
 top: 6px;
 transform: rotate(145deg);
}
 75% {
 transform: rotate(130deg);
}
 100% {
 transform: rotate(135deg);
}
}
 
@keyframes
topbar-back {  0% {
 top: 6px;
 transform: rotate(135deg);
}
 45% {
 transform: rotate(-10deg);
}
 75% {
 transform: rotate(5deg);
}
 100% {
 top: 0px;
 transform: rotate(0);
}
}
 
@keyframes
bottombar-x {  0% {
 bottom: 0px;
 transform: rotate(0deg);
}
 45% {
 bottom: 6px;
 transform: rotate(-145deg);
}
 75% {
 transform: rotate(-130deg);
}
 100% {
 transform: rotate(-135deg);
}
}
 
@keyframes
bottombar-back {  0% {
 bottom: 6px;
 transform: rotate(-135deg);
}
 45% {
 transform: rotate(10deg);
}
 75% {
 transform: rotate(-5deg);
}
 100% {
 bottom: 0px;
 transform: rotate(0);
}
}

文件信息:

    0

    发表评论