Skip to content

Commit ce331d9

Browse files
committed
完成了day26的任务,更新了readme.md
1 parent b3ea6c2 commit ce331d9

File tree

5 files changed

+533
-3
lines changed

5 files changed

+533
-3
lines changed

26 - Strip Follow Along Nav/README.md

+68
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
# 26 Strip Follow Along Nav 中文指南
2+
3+
> 本篇作者:©[大史快跑Dashrun](https://github.com/dashrun)——Chinasoft Frontend Developer
4+
5+
> 简介:[JavaScript30](https://javascript30.com)[Wes Bos](https://github.com/wesbos) 推出的一个 30 天挑战。项目免费提供了 30 个视频教程、30 个挑战的起始文档和 30 个挑战解决方案源代码。目的是帮助人们用纯 JavaScript 来写东西,不借助框架和库,也不使用编译器和引用。现在你看到的是这系列指南的第 26 篇。完整指南在 [GitHub](https://github.com/soyaine/JavaScript30),喜欢请 Star 哦♪(^∇^*)
6+
7+
> 创建时间:2017-10-16
8+
最后更新:2017-10-17
9+
10+
## 挑战任务
11+
初始文档`index-start.html`中提供了一组导航按钮,本次的编程任务需要实现的效果是当鼠标悬停于导航按钮时,显示对应下拉菜单的内容。(说明:下拉菜单内容及白色背景已写好,只需要根据需要改变其CSS属性使元素显示出来或改变其位置即可)。
12+
13+
## 实现效果
14+
![结果展示](https://github.com/dashrun/vanilla-javascript-30/blob/master/26%20-%20Strip%20Follow%20Along%20Nav/effect.png)
15+
16+
## 编程思路
17+
监听每一个导航栏按钮绑定鼠标移入和移出的事件,在对应的回调函数中为相应的元素增加已经编写好的类名即可。
18+
19+
## 过程指南
20+
1.监听每一个导航栏按钮的鼠标移入移出事件
21+
```js
22+
var mainUl = document.querySelectorAll('.cool > li >a');
23+
var navArr = Array.from(mainUl);
24+
var bg = document.querySelector('.dropdownBackground');
25+
26+
navArr.map(function(item,index){
27+
item.onmouseover = function (){
28+
item.parentNode.classList.add('trigger-enter');
29+
item.parentNode.classList.add('trigger-enter-active');
30+
toggleBackground(index+1);
31+
}
32+
33+
item.onmouseout = function (){
34+
item.parentNode.classList.remove('trigger-enter');
35+
item.parentNode.classList.remove('trigger-enter-active');
36+
toggleBackground();
37+
}
38+
});
39+
```
40+
2.toggleBackground()函数为自定义函数,当传入整数参数时,根据对应的下拉菜单序号获取下拉菜单的尺寸,将绝对定位的白色背景衬底改编为对应大小,并移动至相应位置;当不传入任何参数时,表示鼠标没有悬停于任何按钮上,此时需要将下拉菜单的背景置为透明。
41+
```js
42+
function toggleBackground(item){
43+
var itemPos;
44+
if(item === 1 || item === 2 || item ===3){
45+
//计算位置
46+
itemPos = document.querySelector('.dropdown'+item).getBoundingClientRect();
47+
bg.style.left = `${itemPos.left}px`;
48+
bg.style.top = `${itemPos.top-60}px`;
49+
bg.classList.add('open');
50+
bg.style.width = `${itemPos.width}px`;
51+
bg.style.height = `${itemPos.height}px`;
52+
}else{
53+
bg.style.height = 0;
54+
bg.style.width = 0;
55+
bg.classList.remove('open');
56+
}
57+
}
58+
```
59+
60+
## 延伸思考
61+
1.本例中将相应的样式编写在一个css类中,通过添加和移出类来实现需要的效果,js编程实践中非常推荐这种将DOM操作集中化的做法。
62+
2.除了使用js脚本来实现交互效果外,也可以使用下面的css来实现鼠标悬浮在按钮上时显示下拉菜单的效果(代码中`~`表示同级的元素,由于`index-start.html`中给定的结构中下拉菜单的白色沉底为独立元素,需要动态获取其对应尺寸,故使用js脚本更容易实现)
63+
```css
64+
.cool>li>a:hover ~.dropdown{
65+
display:block;
66+
opacity:1;
67+
}
68+
```
957 KB
Loading
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,248 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8">
5+
<title>Follow Along Nav</title>
6+
</head>
7+
<body>
8+
<h2>Cool</h2>
9+
<nav class="top">
10+
<div class="dropdownBackground">
11+
<span class="arrow"></span>
12+
</div>
13+
14+
<ul class="cool">
15+
<li>
16+
<a href="#">About Me</a>
17+
<div class="dropdown dropdown1">
18+
<div class="bio">
19+
<img src="https://logo.clearbit.com/wesbos.com">
20+
<p>Wes Bos sure does love web development. He teaches things like JavaScript, CSS and BBQ. Wait. BBQ isn't part of web development. It should be though!</p>
21+
</div>
22+
</div>
23+
</li>
24+
<li>
25+
<a href="#">Courses</a>
26+
<ul class="dropdown courses dropdown2">
27+
<li>
28+
<span class="code">RFB</span>
29+
<a href="https://ReactForBeginners.com">React For Beginners</a>
30+
</li>
31+
<li>
32+
<span class="code">ES6</span>
33+
<a href="https://ES6.io">ES6 For Everyone</a>
34+
</li>
35+
<li>
36+
<span class="code">STPU</span>
37+
<a href="https://SublimeTextBook.com">Sublime Text Power User</a>
38+
</li>
39+
<li>
40+
<span class="code">WTF</span>
41+
<a href="http://flexbox.io">What The Flexbox?!</a>
42+
</li>
43+
<li>
44+
<span class="code">LRX</span>
45+
<a href="http://LearnRedux.com">Learn Redux</a>
46+
</li>
47+
<li>
48+
<span class="code">CLPU</span>
49+
<a href="http://CommandLinePowerUser.com">Command Line Power User</a>
50+
</li>
51+
<li>
52+
<span class="code">MMD</span>
53+
<a href="http://MasteringMarkdown.com">Mastering Markdown</a>
54+
</li>
55+
</ul>
56+
</li>
57+
<li>
58+
<a href="#">Other Links</a>
59+
<ul class="dropdown dropdown3">
60+
<li><a class="button" href="http://twitter.com/wesbos">Twiter</a></li>
61+
<li><a class="button" href="http://facebook.com/wesbos.developer">Facebook</a></li>
62+
<li><a class="button" href="http://wesbos.com">Blog</a></li>
63+
<li><a class="button" href="http://wesbos.com/courses">Course Catalog</a></li>
64+
</ul>
65+
</li>
66+
</ul>
67+
</nav>
68+
69+
<style>
70+
html {
71+
box-sizing: border-box;
72+
font-family: "Arial Rounded MT Bold","Helvetica Rounded",Arial,sans-serif
73+
}
74+
*, *:before, *:after {
75+
box-sizing: inherit;
76+
}
77+
body {
78+
min-height: 100vh;
79+
background:
80+
linear-gradient(45deg, hsla(340, 100%, 55%, 1) 0%, hsla(340, 100%, 55%, 0) 70%),
81+
linear-gradient(135deg, hsla(225, 95%, 50%, 1) 10%, hsla(225, 95%, 50%, 0) 80%),
82+
linear-gradient(225deg, hsla(140, 90%, 50%, 1) 10%, hsla(140, 90%, 50%, 0) 80%),
83+
linear-gradient(315deg, hsla(35, 95%, 55%, 1) 100%, hsla(35, 95%, 55%, 0) 70%);
84+
}
85+
86+
nav {
87+
position: relative;
88+
perspective: 600px;
89+
}
90+
91+
.cool > li > a {
92+
color: yellow;
93+
text-decoration: none;
94+
font-size: 20px;
95+
background: rgba(0,0,0,0.2);
96+
padding:10px 20px;
97+
display: inline-block;
98+
margin:20px;
99+
border-radius:5px;
100+
}
101+
102+
nav ul {
103+
list-style: none;
104+
margin: 0;
105+
padding: 0;
106+
display: flex;
107+
justify-content: center;
108+
}
109+
110+
.cool > li {
111+
position: relative;
112+
display:flex;
113+
justify-content: center;
114+
}
115+
116+
.dropdown {
117+
opacity: 0;
118+
position: absolute;
119+
overflow: hidden;
120+
padding:20px;
121+
top:-20px;
122+
border-radius:2px;
123+
transition: all 0.5s;
124+
transform: translateY(100px);
125+
will-change: transform;
126+
display: none;
127+
}
128+
129+
.trigger-enter .dropdown {
130+
display: block;
131+
}
132+
133+
.trigger-enter-active .dropdown {
134+
opacity: 1;
135+
}
136+
137+
138+
139+
.dropdownBackground {
140+
width:100px;
141+
height:100px;
142+
position: absolute;
143+
background: #fff;
144+
border-radius: 4px;
145+
box-shadow: 0 50px 100px rgba(50,50,93,.1), 0 15px 35px rgba(50,50,93,.15), 0 5px 15px rgba(0,0,0,.1);
146+
transition:all 0.3s, opacity 0.1s, translate 0.2s;
147+
transform-origin: 50% 0%;
148+
display: flex;
149+
justify-content: center;
150+
opacity:0;
151+
}
152+
153+
.dropdownBackground.open {
154+
opacity: 1;
155+
}
156+
157+
.arrow {
158+
position: absolute;
159+
width:20px;
160+
height:20px;
161+
display: block;
162+
background:white;
163+
transform: translateY(-50%) rotate(45deg);
164+
}
165+
166+
.bio {
167+
min-width:500px;
168+
display:flex;
169+
justify-content: center;
170+
align-items: center;
171+
line-height: 1.7;
172+
}
173+
174+
.bio img {
175+
float:left;
176+
margin-right:20px;
177+
}
178+
179+
.courses {
180+
min-width:300px;
181+
}
182+
183+
.courses li {
184+
padding: 10px 0;
185+
display: block;
186+
border-bottom: 1px solid rgba(0,0,0,0.2);
187+
}
188+
189+
.dropdown a {
190+
text-decoration: none;
191+
color: #ffc600;
192+
}
193+
194+
a.button {
195+
background:black;
196+
display: block;
197+
padding:10px;
198+
color:white;
199+
margin-bottom: 10px;
200+
}
201+
202+
203+
/* Matches Twitter, TWITTER, twitter, tWitter, TWiTTeR... */
204+
.button[href*=twitter] { background: #019FE9; }
205+
.button[href*=facebook] { background: #3B5998; }
206+
.button[href*=courses] { background: #ffc600; }
207+
208+
</style>
209+
210+
<script>
211+
var mainUl = document.querySelectorAll('.cool > li >a');
212+
var navArr = Array.from(mainUl);
213+
var bg = document.querySelector('.dropdownBackground');
214+
215+
navArr.map(function(item,index){
216+
item.onmouseover = function (){
217+
item.parentNode.classList.add('trigger-enter');
218+
item.parentNode.classList.add('trigger-enter-active');
219+
toggleBackground(index+1);
220+
}
221+
222+
item.onmouseout = function (){
223+
item.parentNode.classList.remove('trigger-enter');
224+
item.parentNode.classList.remove('trigger-enter-active');
225+
toggleBackground();
226+
}
227+
});
228+
229+
function toggleBackground(item){
230+
var itemPos;
231+
if(item === 1 || item === 2 || item ===3){
232+
//计算位置
233+
itemPos = document.querySelector('.dropdown'+item).getBoundingClientRect();
234+
bg.style.left = `${itemPos.left}px`;
235+
bg.style.top = `${itemPos.top-60}px`;
236+
bg.classList.add('open');
237+
bg.style.width = `${itemPos.width}px`;
238+
bg.style.height = `${itemPos.height}px`;
239+
}else{
240+
bg.style.height = 0;
241+
bg.style.width = 0;
242+
bg.classList.remove('open');
243+
}
244+
}
245+
</script>
246+
247+
</body>
248+
</html>

0 commit comments

Comments
 (0)