Skip to content

Commit d2b55db

Browse files
committed
完成了day27的挑战并更新了主页readme.md
1 parent ce331d9 commit d2b55db

File tree

6 files changed

+253
-3
lines changed

6 files changed

+253
-3
lines changed

27 - Click and Drag/README.md

+66
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
# 27 Click And Drag 中文指南
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 来写东西,不借助框架和库,也不使用编译器和引用。现在你看到的是这系列指南的第 27 篇。完整指南在 [GitHub](https://github.com/soyaine/JavaScript30),喜欢请 Star 哦♪(^∇^*)
6+
7+
> 创建时间:2017-10-24
8+
最后更新:2017-10-229
9+
10+
## 挑战任务
11+
初始文档`index-start.html`中提供了一组条幅,本次的编程任务需要实现的效果是当鼠标拖动画面移动时,条幅同步向水平方向移动。
12+
13+
## 实现效果
14+
![结果展示](https://github.com/dashrun/vanilla-javascript-30/blob/master/27%20-%20Click%20and%20Drag/effect.png)
15+
16+
## 编程思路
17+
在最外层的items元素上监听鼠标的按下,移动,弹起事件并编写相应的回调函数即可,在对应的回调函数中获取到鼠标横向滑动的距离,将该距离值翻倍后赋值予条幅的scrollLeft属性可调整元素在水平方向上滚动的位置。
18+
>style.css中已为条幅编写好active类,当鼠标移动时只需要将此类添加给对应的类即可看到很棒的CSS特效。
19+
20+
## 过程指南
21+
1.在条幅容器上监听鼠标按下事件
22+
```js
23+
const slider = document.querySelector(".items");
24+
let isMouseDown = false;//记录鼠标是否按下
25+
let startX;//按下时位置的x坐标
26+
let scrollLeft;//记录视口相对于items最左侧已经滚过的距离
27+
28+
slider.addEventListener('mousedown',(e) =>{
29+
isMouseDown = true;
30+
slider.classList.add('active');
31+
startX = e.pageX - slider.offsetLeft;//记录鼠标按下时的相对位置
32+
scrollLeft = slider.scrollLeft;
33+
});
34+
```
35+
2.在条幅容器上监听鼠标弹起事件
36+
```js
37+
slider.addEventListener('mouseup',(e) =>{
38+
isMouseDown = false;
39+
slider.classList.remove('active');
40+
});
41+
```
42+
3.在条幅容器上监听鼠标移动事件
43+
```js
44+
slider.addEventListener('mousemove',(e) =>{
45+
if (!isMouseDown) {
46+
return;
47+
}//若鼠标未按下,则不进行操作
48+
e.preventDefault();
49+
const x = e.pageX - slider.offsetLeft;
50+
const walk = (x - startX) * 3; //将鼠标移动距离放大后作为条幅的移动距离
51+
slider.scrollLeft = scrollLeft - walk;
52+
});
53+
54+
```
55+
56+
## 延伸思考
57+
本例中的js部分并不复杂,令人感兴趣的是`style.css`样式部分使用了较多CSS3高级用法,使得元素在滚动过程中呈现出透视效果,笔者在此对CSS的实现方法作以简单说明,感兴趣的小伙伴可以自行查找资料深入学习。
58+
除去颜色部分,`active`类中的动态效果其实只用到了3行代码:
59+
```css
60+
/*透视距离,即视点位于垂直距离屏幕的距离,数值越大,离的越远,变形效果看起来越微小*/
61+
.items{perspective: 500px;}
62+
/*所有奇数序号的子元素沿X轴放大,并绕Y轴旋转(相当于人绕柱子转)*/
63+
.item:nth-child(even) { transform: scaleX(1.31) rotateY(40deg); }
64+
/*所有奇数序号的子元素沿X轴放大,并绕Y轴逆向旋转(相当于人绕柱子反转一定角度)*/
65+
.item:nth-child(odd) { transform: scaleX(1.31) rotateY(-40deg); }
66+
```

27 - Click and Drag/effect.png

51.2 KB
Loading
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8">
5+
<title>Click and Drag</title>
6+
<link rel="stylesheet" href="style.css">
7+
</head>
8+
<body>
9+
<div class="items">
10+
<div class="item item1">01</div>
11+
<div class="item item2">02</div>
12+
<div class="item item3">03</div>
13+
<div class="item item4">04</div>
14+
<div class="item item5">05</div>
15+
<div class="item item6">06</div>
16+
<div class="item item7">07</div>
17+
<div class="item item8">08</div>
18+
<div class="item item9">09</div>
19+
<div class="item item10">10</div>
20+
<div class="item item11">11</div>
21+
<div class="item item12">12</div>
22+
<div class="item item13">13</div>
23+
<div class="item item14">14</div>
24+
<div class="item item15">15</div>
25+
<div class="item item16">16</div>
26+
<div class="item item17">17</div>
27+
<div class="item item18">18</div>
28+
<div class="item item19">19</div>
29+
<div class="item item20">20</div>
30+
<div class="item item21">21</div>
31+
<div class="item item22">22</div>
32+
<div class="item item23">23</div>
33+
<div class="item item24">24</div>
34+
<div class="item item25">25</div>
35+
</div>
36+
37+
<script>
38+
const slider = document.querySelector(".items");
39+
let isMouseDown = false;//记录鼠标是否按下
40+
let startX;//按下时位置的x坐标
41+
let scrollLeft;//记录视口相对于items最左侧已经滚过的距离
42+
43+
slider.addEventListener('mousedown',(e) =>{
44+
isMouseDown = true;
45+
slider.classList.add('active');
46+
startX = e.pageX - slider.offsetLeft;
47+
scrollLeft = slider.scrollLeft;
48+
});
49+
50+
slider.addEventListener('mouseup',(e) =>{
51+
isMouseDown = false;
52+
slider.classList.remove('active');
53+
});
54+
55+
slider.addEventListener('mousemove',(e) =>{
56+
if (!isMouseDown) {
57+
return;
58+
}
59+
e.preventDefault();
60+
const x = e.pageX - slider.offsetLeft;
61+
const walk = (x - startX) * 3;
62+
slider.scrollLeft = scrollLeft - walk;
63+
});
64+
65+
</script>
66+
67+
</body>
68+
</html>

27 - Click and Drag/index-start.html

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8">
5+
<title>Click and Drag</title>
6+
<link rel="stylesheet" href="style.css">
7+
</head>
8+
<body>
9+
<div class="items">
10+
<div class="item item1">01</div>
11+
<div class="item item2">02</div>
12+
<div class="item item3">03</div>
13+
<div class="item item4">04</div>
14+
<div class="item item5">05</div>
15+
<div class="item item6">06</div>
16+
<div class="item item7">07</div>
17+
<div class="item item8">08</div>
18+
<div class="item item9">09</div>
19+
<div class="item item10">10</div>
20+
<div class="item item11">11</div>
21+
<div class="item item12">12</div>
22+
<div class="item item13">13</div>
23+
<div class="item item14">14</div>
24+
<div class="item item15">15</div>
25+
<div class="item item16">16</div>
26+
<div class="item item17">17</div>
27+
<div class="item item18">18</div>
28+
<div class="item item19">19</div>
29+
<div class="item item20">20</div>
30+
<div class="item item21">21</div>
31+
<div class="item item22">22</div>
32+
<div class="item item23">23</div>
33+
<div class="item item24">24</div>
34+
<div class="item item25">25</div>
35+
</div>
36+
37+
<script>
38+
</script>
39+
40+
</body>
41+
</html>

27 - Click and Drag/style.css

+75
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
html {
2+
box-sizing: border-box;
3+
background-size: cover;
4+
}
5+
6+
*, *:before, *:after {
7+
box-sizing: inherit;
8+
}
9+
10+
body {
11+
min-height: 100vh;
12+
display: flex;
13+
justify-content: center;
14+
align-items: center;
15+
font-family: sans-serif;
16+
font-size: 20px;
17+
margin: 0;
18+
}
19+
20+
.items {
21+
height:800px;
22+
padding: 100px;
23+
width:100%;
24+
border:1px solid white;
25+
box-shadow: 0 0 10px 7px rgba(0, 0, 0, 0.09);
26+
overflow-x: scroll;
27+
overflow-y: hidden;
28+
white-space: nowrap;
29+
user-select: none;
30+
cursor: pointer;
31+
transition: all 0.2s;
32+
transform: scale(0.98);
33+
position: relative;
34+
background: rgba(255,255,255,0.1);
35+
font-size: 0;
36+
perspective: 500px;
37+
}
38+
39+
.items.active {
40+
background: rgba(255,255,255,0.3);
41+
cursor: grabbing;
42+
cursor: -webkit-grabbing;
43+
transform: scale(1);
44+
}
45+
46+
.item {
47+
width:200px;
48+
height: calc(100% - 40px);
49+
display: inline-flex;
50+
align-items: center;
51+
justify-content: center;
52+
font-size: 80px;
53+
font-weight: 100;
54+
color:rgba(0,0,0,0.15);
55+
box-shadow: inset 0 0 0 10px rgba(0,0,0,0.15);
56+
}
57+
58+
.item:nth-child(9n+1) { background: dodgerblue;}
59+
.item:nth-child(9n+2) { background: goldenrod;}
60+
.item:nth-child(9n+3) { background: paleturquoise;}
61+
.item:nth-child(9n+4) { background: gold;}
62+
.item:nth-child(9n+5) { background: cadetblue;}
63+
.item:nth-child(9n+6) { background: tomato;}
64+
.item:nth-child(9n+7) { background: lightcoral;}
65+
.item:nth-child(9n+8) { background: darkslateblue;}
66+
.item:nth-child(9n+9) { background: rebeccapurple;}
67+
68+
.item:nth-child(even) { transform: scaleX(1.31) rotateY(40deg); }
69+
.item:nth-child(odd) { transform: scaleX(1.31) rotateY(-40deg); }
70+
71+
.wrap {
72+
width: auto;
73+
border:2px solid green;
74+
height: 100%;
75+
}

README.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# JavaScript30 - 一个月纯 JS 挑战中文指南
22

33
创建日期:2016-12-20
4-
最后更新:2017-09-16
4+
最后更新:2017-10-29
55

66
> Repo by: [缉熙Soyaine](https://github.com/soyaine)
77
> [JavaScript30](https://javascript30.com) 教程作者:[Wes Bos](https://github.com/wesbos)
@@ -65,7 +65,7 @@ No | Guide | Demo
6565
23 | [Speech Synthesis指南](https://github.com/soyaine/JavaScript30/blob/master/23%20-%20Speech%20Synthesis/README.md) | [Speech Synthesis效果](https://github.com/soyaine/JavaScript30/blob/master/23%20-%20Speech%20Synthesis/index-finished-Dashrun.html)
6666
24 | [Sticky Nav指南](https://github.com/soyaine/JavaScript30/blob/master/24%20-%20Sticky%20Nav/README.md) | [Sticky Nav效果](https://github.com/soyaine/JavaScript30/blob/master/24%20-%20Sticky%20Nav/index-finished-Dashrun.html)
6767
25 | [Event Related指南](https://github.com/soyaine/JavaScript30/blob/master/25%20-%20Event%20Related/README.md) | [Event Related效果](https://github.com/soyaine/JavaScript30/blob/master/25%20-%20Event%20Related/index-finished-Dashrun.html)
68-
26 | Stripe Follow Along Nav | -
68+
26 | [Stripe Follow Along Nav指南](https://github.com/soyaine/JavaScript30/blob/master/26%20-%20Stripe%20Follow%20Along%20Nav/README.md) | [Strip Follow Along Nav效果](https://github.com/soyaine/JavaScript30/blob/master/26%20-%20Stripe%20Follow%20Along%20Nav/index-finished-Dashrun.html)
6969
27 | Click and Drag | -
7070
28 | Video Speed Controller | -
7171
29 | Countdown Timer | -
@@ -80,7 +80,7 @@ Name | Contribution
8080
[@DrakeXiang](https://github.com/DrakeXiang) | No.[11](https://github.com/soyaine/JavaScript30/tree/master/11%20-%20Custom%20Video%20Player)
8181
[@zzh466](http://github.com/zzh466) | Review
8282
[@Xing Liu](https://github.com/S1ngS1ng) | Review
83-
[@大史快跑Dashrun](https://github.com/dashrun) | No.[16](https://github.com/soyaine/JavaScript30/tree/master/16%20-%20Mouse%20Move%20Shadow).[17](https://github.com/soyaine/JavaScript30/tree/master/17%20-%20Sort%20Without%20Articles).[18](https://github.com/soyaine/JavaScript30/tree/master/18%20-%20AddingUpTimesWithReduce).[19](https://github.com/soyaine/JavaScript30/blob/master/19%20-%20Webcam%20Fun).[20](https://github.com/soyaine/JavaScript30/tree/master/20%20-%20Speech%20Detection).[21](https://github.com/soyaine/JavaScript30/tree/master/21%20-%20Geolocation).[22](https://github.com/soyaine/JavaScript30/tree/master/22%20-%20Follow%20Along%20Link%20Highlighter).[23](https://github.com/soyaine/JavaScript30/tree/master/23%20-%20Speech%20Synthesis).[24](https://github.com/soyaine/JavaScript30/tree/master/24%20-%20Sticky%20Nav).[25](https://github.com/soyaine/JavaScript30/tree/master/25%20-%20Event%20Related)
83+
[@大史快跑Dashrun](https://github.com/dashrun) | No.[16](https://github.com/soyaine/JavaScript30/tree/master/16%20-%20Mouse%20Move%20Shadow).[17](https://github.com/soyaine/JavaScript30/tree/master/17%20-%20Sort%20Without%20Articles).[18](https://github.com/soyaine/JavaScript30/tree/master/18%20-%20AddingUpTimesWithReduce).[19](https://github.com/soyaine/JavaScript30/blob/master/19%20-%20Webcam%20Fun).[20](https://github.com/soyaine/JavaScript30/tree/master/20%20-%20Speech%20Detection).[21](https://github.com/soyaine/JavaScript30/tree/master/21%20-%20Geolocation).[22](https://github.com/soyaine/JavaScript30/tree/master/22%20-%20Follow%20Along%20Link%20Highlighter).[23](https://github.com/soyaine/JavaScript30/tree/master/23%20-%20Speech%20Synthesis).[24](https://github.com/soyaine/JavaScript30/tree/master/24%20-%20Sticky%20Nav).[25](https://github.com/soyaine/JavaScript30/tree/master/25%20-%20Event%20Related).[26](https://github.com/soyaine/JavaScript30/tree/master/26%20-%20Strip%20Follow%20Along%20Nav)
8484
[@缉熙Soyaine](https://github.com/soyaine) | No.[1](https://github.com/soyaine/JavaScript30/tree/master/01%20-%20JavaScript%20Drum%20Kit).[2](https://github.com/soyaine/JavaScript30/tree/master/02%20-%20JS%20%2B%20CSS%20Clock).[3](https://github.com/soyaine/JavaScript30/tree/master/03%20-%20CSS%20%Variables).[4](https://github.com/soyaine/JavaScript30/tree/master/04%20-%20Array%20Cardio%20Day%201).[5](https://github.com/soyaine/JavaScript30/blob/master/05%20-%20Flex%20Panel%20Gallery).[6](https://github.com/soyaine/JavaScript30/blob/master/06%20-%20Type%20Ahead).[7](https://github.com/soyaine/JavaScript30/tree/master/07%20-%20Array%20Cardio%20Day%202).[8](https://github.com/soyaine/JavaScript30/tree/master/08%20-%20Fun%20with%20HTML5%20Canvas).[9](https://github.com/soyaine/JavaScript30/blob/master/09%20-%20Dev%20Tools%20Domination).[10](https://github.com/soyaine/JavaScript30/blob/master/10%20-%20Hold%20Shift%20and%20Check%20Checkboxes/README.md).[12](https://github.com/soyaine/JavaScript30/tree/master/12%20-%20Key%20Sequence%20Detection/README.md).[13](https://github.com/soyaine/JavaScript30/blob/master/13%20-%20Slide%20in%20on%20Scroll/README.md).[14](https://github.com/soyaine/JavaScript30/tree/master/14%20-%20JavaScript%20References%20VS%20Copying)
8585

8686
## JOIN US

0 commit comments

Comments
 (0)