Skip to content

Commit 145aee4

Browse files
committed
Smooth Scroll Done!
1 parent cbf960e commit 145aee4

File tree

6 files changed

+160
-41
lines changed

6 files changed

+160
-41
lines changed

package-lock.json

Lines changed: 43 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
"react": "^17.0.2",
1010
"react-dom": "^17.0.2",
1111
"react-scripts": "4.0.3",
12+
"smooth-scrollbar": "^8.6.3",
1213
"web-vitals": "^1.1.2"
1314
},
1415
"scripts": {

src/App.css

Lines changed: 32 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,44 @@
1+
2+
13
.App {
2-
text-align: center;
4+
5+
6+
display:flex;
7+
flex-direction: column;
8+
padding:5rem;
39
}
410

5-
.App-logo {
6-
height: 40vmin;
7-
pointer-events: none;
11+
.title{
12+
text-transform: uppercase;
13+
align-self: center;
14+
margin-bottom:8rem;
15+
font-size:4vw;
816
}
917

10-
@media (prefers-reduced-motion: no-preference) {
11-
.App-logo {
12-
animation: App-logo-spin infinite 20s linear;
13-
}
18+
.imgContainer{
19+
max-width:50vw;
20+
margin:4rem 0;
1421
}
1522

16-
.App-header {
17-
background-color: #282c34;
18-
min-height: 100vh;
19-
display: flex;
20-
flex-direction: column;
21-
align-items: center;
22-
justify-content: center;
23-
font-size: calc(10px + 2vmin);
24-
color: white;
23+
img{
24+
width:100%;
25+
height:auto;
26+
2527
}
2628

27-
.App-link {
28-
color: #61dafb;
29+
30+
.scrollbar-track{
31+
background-color: transparent !important;
2932
}
3033

31-
@keyframes App-logo-spin {
32-
from {
33-
transform: rotate(0deg);
34-
}
35-
to {
36-
transform: rotate(360deg);
37-
}
34+
.scrollbar-track-y{
35+
width:12px !important;
3836
}
37+
38+
39+
.scrollbar-thumb{
40+
width:12px !important;
41+
background-color: black !important;
42+
border-radius: 0 !important;
43+
}
44+

src/App.js

Lines changed: 28 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,36 @@
1-
import logo from './logo.svg';
1+
import { useEffect, useState } from 'react';
2+
import Scroll from './components/SmoothScroll';
3+
24
import './App.css';
35

46
function App() {
7+
const [images,setImages] = useState();
8+
9+
10+
11+
useEffect(() => {
12+
fetch('https://picsum.photos/v2/list?limit=10').then(res => res.json())
13+
.then(json => {
14+
//console.log(json);
15+
setImages(json);
16+
});
17+
18+
},[])
19+
20+
const random = () => {
21+
return Math.floor(Math.random()*50);
22+
}
523
return (
624
<div className="App">
7-
<header className="App-header">
8-
<img src={logo} className="App-logo" alt="logo" />
9-
<p>
10-
Edit <code>src/App.js</code> and save to reload.
11-
</p>
12-
<a
13-
className="App-link"
14-
href="https://reactjs.org"
15-
target="_blank"
16-
rel="noopener noreferrer"
17-
>
18-
Learn React
19-
</a>
20-
</header>
25+
<h1 className="title">React Smooth Scroll</h1>
26+
<Scroll />
27+
{
28+
images && images.map(
29+
img => <div style={{marginLeft:`${random()}rem`}} key={img.id} className="imgContainer">
30+
<img src={img.download_url} alt={img.author} />
31+
</div>
32+
)
33+
}
2134
</div>
2235
);
2336
}

src/components/SmoothScroll.js

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
import Scrollbar from 'smooth-scrollbar';
2+
import { useEffect } from 'react';
3+
import OverscrollPlugin from 'smooth-scrollbar/plugins/overscroll';
4+
5+
6+
7+
8+
const overscrollOptions = {
9+
enable: true,
10+
effect: 'bounce',
11+
damping: 0.15,
12+
maxOverscroll: 150,
13+
glowColor: '#fff',
14+
};
15+
16+
// const overscrollOptions = {
17+
// enable: true,
18+
// effect: 'glow',
19+
// damping: 0.1,
20+
// maxOverscroll: 200,
21+
// glowColor: '#222a2d',
22+
// };
23+
24+
25+
const options = {
26+
damping : 0.07,
27+
thumbMinSize: 100,
28+
plugins: {
29+
overscroll: { ...overscrollOptions },
30+
},
31+
32+
}
33+
34+
const Scroll = () => {
35+
36+
useEffect(() => {
37+
38+
Scrollbar.use(OverscrollPlugin);
39+
40+
Scrollbar.init(document.body, options);
41+
42+
43+
44+
45+
return () => {
46+
if (Scrollbar) Scrollbar.destroy(document.body)
47+
} },[])
48+
49+
50+
51+
return null;
52+
}
53+
54+
export default Scroll;

src/index.css

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ body {
55
sans-serif;
66
-webkit-font-smoothing: antialiased;
77
-moz-osx-font-smoothing: grayscale;
8+
height:100vh;
9+
width:100vw;
810
}
911

1012
code {

0 commit comments

Comments
 (0)