Skip to content

Commit 84157f4

Browse files
committed
day 30 🥳
1 parent 4bf738d commit 84157f4

File tree

4 files changed

+166
-0
lines changed

4 files changed

+166
-0
lines changed

each day build day!/Day 30/README.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# Click and Drag n Scroll
2+
3+
The idea is inspired from a website which lets you scroll horizontally using click and drag.
4+
I tried implementing it using javascript and css. It's functional yet buggy.
5+
6+
# Challenges
7+
- scroll Positions
8+
- PageOffset, offsetLeft
9+
-

each day build day!/Day 30/index.html

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

each day build day!/Day 30/main.js

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/* steps : 1. get the hold of divs,
2+
2. get the click position
3+
3. get the scrollLeft value
4+
4. capture the difference between scrollLeft and clickX
5+
5. move the div to left by value equals the difference
6+
7+
*/
8+
9+
const scrollArea = document.querySelector('.items')
10+
11+
let mouseClicked = false;
12+
let startX;
13+
let scrollLeft;
14+
15+
16+
scrollArea.addEventListener('mousedown', (e)=>{
17+
mouseClicked = true;
18+
scrollArea.classList.add('active')
19+
//get the scroll value
20+
startX = e.pageX - scrollArea.offsetLeft //by how much it has scrolled
21+
scrollLeft = scrollArea.offsetLeft;
22+
console.log(startX,scrollLeft)
23+
})
24+
25+
scrollArea.addEventListener('mouseup', ()=>{
26+
mouseClicked = false;
27+
scrollArea.classList.remove('active')
28+
29+
})
30+
31+
scrollArea.addEventListener('mouseleave', ()=>{
32+
mouseClicked = false;
33+
scrollArea.classList.remove('active')
34+
})
35+
36+
scrollArea.addEventListener('mousemove', (e)=>{
37+
38+
if(!mouseClicked) return;
39+
40+
e.preventDefault()
41+
//cuurent position after mousemove
42+
const currentPosX = e.pageX - scrollArea.offsetLeft
43+
scrollAmount = (currentPosX - startX ) * 2
44+
//finally the update the div's position either to right or left
45+
scrollArea.scrollLeft = scrollLeft - scrollAmount ;//Initial scrollArea.scrollLeft //(scrolled factor) to avoid jumpy animation
46+
})
47+

each day build day!/Day 30/style.css

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
html {
2+
box-sizing: border-box;
3+
background: rgb(52, 61, 65);
4+
background-size: cover;
5+
}
6+
7+
*, *:before, *:after {
8+
box-sizing: inherit;
9+
}
10+
11+
body {
12+
min-height: 100vh;
13+
display: flex;
14+
justify-content: center;
15+
align-items: center;
16+
font-family: sans-serif;
17+
font-size: 20px;
18+
margin: 0;
19+
}
20+
21+
.items {
22+
height: 600px;
23+
padding: 100px;
24+
width: 100%;
25+
border: 1px solid white;
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+
will-change: transform;
34+
position: relative;
35+
background: rgba(255,255,255,0.1);
36+
font-size: 0;
37+
perspective: 500px;
38+
}
39+
40+
.items.active {
41+
background: rgba(255,255,255,0.3);
42+
cursor: grabbing;
43+
cursor: -webkit-grabbing;
44+
transform: scale(1);
45+
}
46+
47+
.item {
48+
width: 200px;
49+
height: calc(100% - 40px);
50+
display: inline-flex;
51+
align-items: center;
52+
justify-content: center;
53+
font-size: 80px;
54+
font-weight: 100;
55+
color: rgba(0,0,0,0.15);
56+
box-shadow: inset 0 0 0 10px rgba(0,0,0,0.15);
57+
}
58+
59+
.item:nth-child(9n+1) { background: dodgerblue;}
60+
.item:nth-child(9n+2) { background: goldenrod;}
61+
.item:nth-child(9n+3) { background: paleturquoise;}
62+
.item:nth-child(9n+4) { background: gold;}
63+
.item:nth-child(9n+5) { background: cadetblue;}
64+
.item:nth-child(9n+6) { background: tomato;}
65+
.item:nth-child(9n+7) { background: lightcoral;}
66+
.item:nth-child(9n+8) { background: darkslateblue;}
67+
.item:nth-child(9n+9) { background: rebeccapurple;}
68+
69+
.item:nth-child(even) { transform: scaleX(1.31) rotateY(40deg); }
70+
.item:nth-child(odd) { transform: scaleX(1.31) rotateY(-40deg); }
71+

0 commit comments

Comments
 (0)