新手村27 - Click and Drag

27 - Click and Drag

俗話說的好,一天一蘋果,醫生遠離我

一天一 JS,What the f*ck JavaScript?

small steps every day - 記錄著新手村日記

完成目標

  • 功能
    • 拖拉互動要有拖拉畫面的功能
  • 畫面
    • 拖拉的程度要和畫面移動程度相同的px

index_START.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
35
36
37
38
39
40
41
42
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Click and Drag</title>
<link rel="stylesheet" href="style.css">
</head>
<body>
<div class="items">
<div class="item item1">01</div>
<div class="item item2">02</div>
<div class="item item3">03</div>
<div class="item item4">04</div>
<div class="item item5">05</div>
<div class="item item6">06</div>
<div class="item item7">07</div>
<div class="item item8">08</div>
<div class="item item9">09</div>
<div class="item item10">10</div>
<div class="item item11">11</div>
<div class="item item12">12</div>
<div class="item item13">13</div>
<div class="item item14">14</div>
<div class="item item15">15</div>
<div class="item item16">16</div>
<div class="item item17">17</div>
<div class="item item18">18</div>
<div class="item item19">19</div>
<div class="item item20">20</div>
<div class="item item21">21</div>
<div class="item item22">22</div>
<div class="item item23">23</div>
<div class="item item24">24</div>
<div class="item item25">25</div>
</div>

<script>
</script>

</body>
</html>

CSS_START.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
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
html {
box-sizing: border-box;
background: url('https://source.unsplash.com/NFs6dRTBgaM/2000x2000') fixed;
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;
overflow-x: scroll;
overflow-y: hidden;
white-space: nowrap;
user-select: none;
cursor: pointer;
transition: all 0.2s;
transform: scale(0.98);
will-change: transform;
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); }

JS - step by step

首先,一樣也是透過 querySelector 選取 Class items,可以將到所有的卡片外的 div

1
2
3
4
5
<script>
const slider = document.querySelector('.items');
console.log(slider);
// 抓到25個div
</script>

並先來把觸發對象和條件先建構出來吧!在這個範例中,我們會分別用到 mousedownmousemovemouseup 三個觸發事件,分別是當滑鼠按下、移動、按起來事件會被觸發。

1
2
3
4
5
6
7
8
9
10
<script>
const slider = document.querySelector('.items');
console.log(slider);
// 抓到25個div

slider.addEventListener('mousedown', (e) => {});
slider.addEventListener('mouseup', (e) => {});
slider.addEventListener('mousemove', (e) => {});

</script>

先來判斷使用者是不是按下按鈕的,宣告變數 isDown 來判斷使用者有沒有按下滑鼠,預設一開始為 false,當使用者 mousedown 的時候表示按下, isDown = true ,使用者 mouseup 的時候表示按起, isDown = false,而在 mousemove 的能持續觸發事件的時候,應該判斷使用者是不是在按下的情況,否則應該停止 Function

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<script>
const slider = document.querySelector('.items');
console.log(slider);
let isDown = false;
// 抓到25個div

slider.addEventListener('mousedown', (e) => {
isDown = true;
});
slider.addEventListener('mouseup', (e) => {
isDown = false;
});
slider.addEventListener('mousemove', (e) => {
if (!isDown) return;
});

</script>

接下來我們透過加上 Class active 來產生特效,分別在滑鼠按下及按起時候新增移除 Class

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
<script>
const slider = document.querySelector('.items');
console.log(slider);
let isDown = false;
// 抓到25個div

slider.addEventListener('mousedown', (e) => {
isDown = true;
slider.classList.add('active');
});
slider.addEventListener('mouseup', (e) => {
isDown = false;
slider.classList.remove('active');
});
slider.addEventListener('mousemove', (e) => {
if (!isDown) return;
});

</script>

最後再記錄現在的 offsetX & pageX 為變數 startXslider.scrollLeft 為變數 scrollLeft,最後在移動X軸時處理位置即可~

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
<script>
const slider = document.querySelector('.items');
console.log(slider);
let isDown = false;
let startX;
let scrollLeft;

slider.addEventListener('mousedown', (e) => {
isDown = true;
slider.classList.add('active');
startX = e.pageX + slider.offsetLeft;
scrollLeft = slider.scrollLeft;
});

slider.addEventListener('mouseup', (e) => {
isDown = false;
slider.classList.remove('active');
});

slider.addEventListener('mousemove', (e) => {
if (!isDown) return;
e.preventDefault();
const x = e.pageX - slider.offsetLeft;
const walk = (x - startX) * 3;
slider.scrollLeft = scrollLeft - walk;
});

</script>

就大功告成啦!

JS - final

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
<script>
const slider = document.querySelector('.items');
console.log(slider);
let isDown = false;
let startX;
let scrollLeft;

slider.addEventListener('mousedown', (e) => {
isDown = true;
slider.classList.add('active');
startX = e.pageX + slider.offsetLeft;
scrollLeft = slider.scrollLeft;
});

slider.addEventListener('mouseup', (e) => {
isDown = false;
slider.classList.remove('active');
});

slider.addEventListener('mousemove', (e) => {
if (!isDown) return;
e.preventDefault();
const x = e.pageX - slider.offsetLeft;
const walk = (x - startX) * 3;
slider.scrollLeft = scrollLeft - walk;
});

</script>

本刊同步於個人網站:http://chestertang.site/

本次範例程式碼原作者來源:https://tinyurl.com/yavm5f5n