diff --git a/27 - Click and Drag/README.md b/27 - Click and Drag/README.md new file mode 100644 index 0000000..b33fb5b --- /dev/null +++ b/27 - Click and Drag/README.md @@ -0,0 +1,66 @@ +# 27 Click And Drag 中文指南 + +> 本篇作者:©[大史快跑Dashrun](https://github.com/dashrun)——Chinasoft Frontend Developer + +> 简介:[JavaScript30](https://javascript30.com) 是 [Wes Bos](https://github.com/wesbos) 推出的一个 30 天挑战。项目免费提供了 30 个视频教程、30 个挑战的起始文档和 30 个挑战解决方案源代码。目的是帮助人们用纯 JavaScript 来写东西,不借助框架和库,也不使用编译器和引用。现在你看到的是这系列指南的第 27 篇。完整指南在 [GitHub](https://github.com/soyaine/JavaScript30),喜欢请 Star 哦♪(^∇^*) + +> 创建时间:2017-10-24 +最后更新:2017-10-229 + +## 挑战任务 +初始文档`index-start.html`中提供了一组条幅,本次的编程任务需要实现的效果是当鼠标拖动画面移动时,条幅同步向水平方向移动。 + +## 实现效果 +![结果展示](https://github.com/dashrun/vanilla-javascript-30/blob/master/27%20-%20Click%20and%20Drag/effect.png) + +## 编程思路 +在最外层的items元素上监听鼠标的按下,移动,弹起事件并编写相应的回调函数即可,在对应的回调函数中获取到鼠标横向滑动的距离,将该距离值翻倍后赋值予条幅的scrollLeft属性可调整元素在水平方向上滚动的位置。 +>style.css中已为条幅编写好active类,当鼠标移动时只需要将此类添加给对应的类即可看到很棒的CSS特效。 + +## 过程指南 +1.在条幅容器上监听鼠标按下事件 +```js + const slider = document.querySelector(".items"); + let isMouseDown = false;//记录鼠标是否按下 + let startX;//按下时位置的x坐标 + let scrollLeft;//记录视口相对于items最左侧已经滚过的距离 + + slider.addEventListener('mousedown',(e) =>{ + isMouseDown = true; + slider.classList.add('active'); + startX = e.pageX - slider.offsetLeft;//记录鼠标按下时的相对位置 + scrollLeft = slider.scrollLeft; + }); + ``` +2.在条幅容器上监听鼠标弹起事件 +```js + slider.addEventListener('mouseup',(e) =>{ + isMouseDown = false; + slider.classList.remove('active'); + }); +``` +3.在条幅容器上监听鼠标移动事件 +```js + slider.addEventListener('mousemove',(e) =>{ + if (!isMouseDown) { + return; + }//若鼠标未按下,则不进行操作 + e.preventDefault(); + const x = e.pageX - slider.offsetLeft; + const walk = (x - startX) * 3; //将鼠标移动距离放大后作为条幅的移动距离 + slider.scrollLeft = scrollLeft - walk; + }); + +``` + +## 延伸思考 +本例中的js部分并不复杂,令人感兴趣的是`style.css`样式部分使用了较多CSS3高级用法,使得元素在滚动过程中呈现出透视效果,笔者在此对CSS的实现方法作以简单说明,感兴趣的小伙伴可以自行查找资料深入学习。 +除去颜色部分,`active`类中的动态效果其实只用到了3行代码: +```css +/*透视距离,即视点位于垂直距离屏幕的距离,数值越大,离的越远,变形效果看起来越微小*/ +.items{perspective: 500px;} +/*所有奇数序号的子元素沿X轴放大,并绕Y轴旋转(相当于人绕柱子转)*/ +.item:nth-child(even) { transform: scaleX(1.31) rotateY(40deg); } +/*所有奇数序号的子元素沿X轴放大,并绕Y轴逆向旋转(相当于人绕柱子反转一定角度)*/ +.item:nth-child(odd) { transform: scaleX(1.31) rotateY(-40deg); } +``` diff --git a/27 - Click and Drag/effect.png b/27 - Click and Drag/effect.png new file mode 100644 index 0000000..23ed6cf Binary files /dev/null and b/27 - Click and Drag/effect.png differ diff --git a/27 - Click and Drag/index-finished-Dashrun.html b/27 - Click and Drag/index-finished-Dashrun.html new file mode 100644 index 0000000..3707b81 --- /dev/null +++ b/27 - Click and Drag/index-finished-Dashrun.html @@ -0,0 +1,68 @@ + + + + + Click and Drag + + + +
+
01
+
02
+
03
+
04
+
05
+
06
+
07
+
08
+
09
+
10
+
11
+
12
+
13
+
14
+
15
+
16
+
17
+
18
+
19
+
20
+
21
+
22
+
23
+
24
+
25
+
+ + + + + \ No newline at end of file diff --git a/27 - Click and Drag/index-start.html b/27 - Click and Drag/index-start.html new file mode 100644 index 0000000..b860931 --- /dev/null +++ b/27 - Click and Drag/index-start.html @@ -0,0 +1,41 @@ + + + + + Click and Drag + + + +
+
01
+
02
+
03
+
04
+
05
+
06
+
07
+
08
+
09
+
10
+
11
+
12
+
13
+
14
+
15
+
16
+
17
+
18
+
19
+
20
+
21
+
22
+
23
+
24
+
25
+
+ + + + + diff --git a/27 - Click and Drag/style.css b/27 - Click and Drag/style.css new file mode 100644 index 0000000..348a71b --- /dev/null +++ b/27 - Click and Drag/style.css @@ -0,0 +1,75 @@ +html { + box-sizing: border-box; + background-size: cover; +} + +*, *:before, *:after { + box-sizing: inherit; +} + +body { + min-height: 100vh; + display: flex; + justify-content: center; + align-items: center; + font-family: sans-serif; + font-size: 20px; + margin: 0; +} + +.items { + height:800px; + padding: 100px; + width:100%; + border:1px solid white; + box-shadow: 0 0 10px 7px rgba(0, 0, 0, 0.09); + overflow-x: scroll; + overflow-y: hidden; + white-space: nowrap; + user-select: none; + cursor: pointer; + transition: all 0.2s; + transform: scale(0.98); + position: relative; + background: rgba(255,255,255,0.1); + font-size: 0; + perspective: 500px; +} + +.items.active { + background: rgba(255,255,255,0.3); + cursor: grabbing; + cursor: -webkit-grabbing; + transform: scale(1); +} + +.item { + width:200px; + height: calc(100% - 40px); + display: inline-flex; + align-items: center; + justify-content: center; + font-size: 80px; + font-weight: 100; + color:rgba(0,0,0,0.15); + box-shadow: inset 0 0 0 10px rgba(0,0,0,0.15); +} + +.item:nth-child(9n+1) { background: dodgerblue;} +.item:nth-child(9n+2) { background: goldenrod;} +.item:nth-child(9n+3) { background: paleturquoise;} +.item:nth-child(9n+4) { background: gold;} +.item:nth-child(9n+5) { background: cadetblue;} +.item:nth-child(9n+6) { background: tomato;} +.item:nth-child(9n+7) { background: lightcoral;} +.item:nth-child(9n+8) { background: darkslateblue;} +.item:nth-child(9n+9) { background: rebeccapurple;} + +.item:nth-child(even) { transform: scaleX(1.31) rotateY(40deg); } +.item:nth-child(odd) { transform: scaleX(1.31) rotateY(-40deg); } + +.wrap { + width: auto; + border:2px solid green; + height: 100%; +} diff --git a/README.md b/README.md index 7ab5ef3..8575ede 100644 --- a/README.md +++ b/README.md @@ -1,7 +1,7 @@ # JavaScript30 - 一个月纯 JS 挑战中文指南 创建日期:2016-12-20 -最后更新:2017-09-16 +最后更新:2017-10-29 > Repo by: [缉熙Soyaine](https://github.com/soyaine) > [JavaScript30](https://javascript30.com) 教程作者:[Wes Bos](https://github.com/wesbos) @@ -65,7 +65,7 @@ No | Guide | Demo 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) 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) 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) -26 | Stripe Follow Along Nav | - +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) 27 | Click and Drag | - 28 | Video Speed Controller | - 29 | Countdown Timer | - @@ -80,7 +80,7 @@ Name | Contribution [@DrakeXiang](https://github.com/DrakeXiang) | No.[11](https://github.com/soyaine/JavaScript30/tree/master/11%20-%20Custom%20Video%20Player) [@zzh466](http://github.com/zzh466) | Review [@Xing Liu](https://github.com/S1ngS1ng) | Review -[@大史快跑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) +[@大史快跑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) [@缉熙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) ## JOIN US