目录
先看效果:
实现:
总结:
先看效果:
实现:
1.定义导航栏的文字标签:
1
2
3
4
5
6
7
8
9
10
2.导航栏整体的样式:
1
2
3
4
5
6
7
8
9
10
11
.tou{
position: fixed;
top: 0;
left: 0;
padding: 25px 100px;
width: 100%;
display: flex;
justify-content: space-between;
align-items: center;
transition: 0.5s;
}
transition 过渡效果
3.北极光这个logo的样式:
1
2
3
4
5
6
7
.logo{
position: relative;
font-size: 22px;
font-weight: 900;
letter-spacing: 1px;
color: rgb(28, 36, 148);
}
letter-spacing:文字(字母)间距
4.给北极光logo定位一个图片在文字左边:
1
2
3
4
5
6
7
8
9
10
.logo::before{
content: ”;
position: absolute;
left: -50px;
top: -15px;
width: 50px;
height: 50px;
background-image: url(logo.png);
background-size: 100%;
}
5.右边导航标签的一些样式,样式等都不做详细说明了,毕竟每个人的都不一样~:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
.biao{
position: relative;
display: flex;
justify-content: center;
align-content: center;
list-style: none;
}
.biao li{
position: relative;
}
.biao a{
position: relative;
margin: 0 10px;
font-size: 18px;
font-family: ‘fangsong’;
font-weight: bold;
color: rgb(28, 36, 148);
text-decoration: none;
}
6.当页面有滚动后导航栏的样式,padding上下变小,字体颜色变,有了蓝背景色:
1
2
3
4
5
6
7
.bian{
padding: 15px 100px;
background-color: rgb(71, 105, 219);
}
.bian .logo,.tou.bian a{
color: rgb(252, 247, 247);
}
7.简单js,实现部分:
第一种:
1
2
3
4
5
6
7
8
9
window.addEventListener(‘scroll’,function(){
let tou = document.querySelector(‘.tou’);
if(window.scrollY>0)
{
tou.classList.add(“bian”);
}else{
tou.classList.remove(“bian”);
}
})
第二种:直接这样:
1
2
3
4
5
window.addEventListener(‘scroll’,function(){
let tou = document.querySelector(‘.tou’);
tou.classList.toggle(“bian”,window.scrollY>0);
})
解释:
scrollY属性:
Window接口的只读scrollY属性返回文档当前垂直滚动的像素数。
classList属性:
add(class1, class2, …) 在元素中添加一个或多个类名。如果指定的类名已存在,则不会添加;
remove(class1, class2, …) 移除元素中一个或多个类名。
toggle(class, true|false) 第一个参数为如果已存在类名则中移除的类名,并返回 false。如果该类名不存在则会在元素中添加类名,并返回 true。第二个是可选参数,是个布尔值用于设置元素是否强制添加或移除类,不管该类名是否存在。
所以:
第一种js写法就是有滚动>0时就添加类.biao而实现渐变效果,当滚动<=0时就移除.biao类回到原来;
第二种就是布尔值判断,当滚动>0就强制添加.biao类,当滚动<=0就移除.biao类;
完整代码:
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
总结:
到此这篇关于html+css+js实现导航栏滚动渐变效果的文章就介绍到这了,更多相关html css js 导航栏滚动渐变内容请搜索脚本之家以前的文章或继续浏览下面的相关文章,希望大家以后多多支持脚本之家!