Skip to content

Commit b64b077

Browse files
committed
add touch slider
1 parent ad32b33 commit b64b077

File tree

5 files changed

+185
-0
lines changed

5 files changed

+185
-0
lines changed

51-video background/index.html

+3
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
<!-- Based on Create a Website With Video Background | HTML & CSS by Brad Traversy (2021)
2+
see: https://www.youtube.com/watch?v=8MgpE2DTTKA -->
3+
14
<!DOCTYPE html>
25
<html lang="en">
36
<head>

53-touch slider/index.html

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<!-- Based on Build a Touch Slider with HTML, CSS & JavaScript by Brad Traversy (2021)
2+
see: https://www.youtube.com/watch?v=5bxFSOA5JYo
3+
original idea by Will Adams: https://github.com/bushblade/Full-Screen-Touch-Slider -->
4+
5+
<!DOCTYPE html>
6+
<html lang="en">
7+
<head>
8+
<meta charset="UTF-8" />
9+
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
10+
<link rel="stylesheet" href="style.css" />
11+
<title>Touch Slider</title>
12+
</head>
13+
<body>
14+
<div class="slider-container">
15+
<div class="slide">
16+
<h2>AirPods</h2>
17+
<h4>$199</h4>
18+
<img src="https://i.ibb.co/y08W0Jx/image1.png" alt="" />
19+
<a href="#" class="btn">Buy Now</a>
20+
</div>
21+
<div class="slide">
22+
<h2>iPhone 12</h2>
23+
<h4>$799</h4>
24+
<img src="https://i.ibb.co/NYdGg2q/image2.png" alt="" />
25+
<a href="#" class="btn">Buy Now</a>
26+
</div>
27+
<div class="slide">
28+
<h2>iPad</h2>
29+
<h4>$599</h4>
30+
<img src="https://i.ibb.co/Jd3t4hQ/image3.png" alt="" />
31+
<a href="#" class="btn">Buy Now</a>
32+
</div>
33+
</div>
34+
<script src="script.js"></script>
35+
</body>
36+
</html>

53-touch slider/script.js

+76
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
const slider = document.querySelector(".slider-container"),
2+
slides = Array.from(document.querySelectorAll(".slide"));
3+
4+
let isDragging = false,
5+
startPos = 0,
6+
currentTranslate = 0,
7+
prevTranslate = 0,
8+
animationID = 0,
9+
currentIndex = 0;
10+
11+
// Disable context menu
12+
window.oncontextmenu = (event) => {
13+
event.preventDefault();
14+
event.stopPropagation();
15+
return false;
16+
};
17+
18+
const getPositionX = (event) =>
19+
event.type.includes("mouse") ? event.pageX : event.touches[0].clientX;
20+
21+
const setSliderPosition = () => {
22+
slider.style.transform = `translateX(${currentTranslate}px)`;
23+
};
24+
25+
const animation = () => {
26+
setSliderPosition();
27+
if (isDragging) requestAnimationFrame(animation);
28+
};
29+
30+
const setPositionByIndex = () => {
31+
currentTranslate = currentIndex * -window.innerWidth;
32+
prevTranslate = currentTranslate;
33+
setSliderPosition();
34+
};
35+
36+
const touchStart = (index) => {
37+
return (event) => {
38+
currentIndex = index;
39+
startPos = getPositionX(event);
40+
isDragging = true;
41+
// https://css-tricks.com/using-requestanimationframe/
42+
animationID = requestAnimationFrame(animation);
43+
slider.classList.add("grabbing");
44+
};
45+
};
46+
47+
const touchEnd = () => {
48+
isDragging = false;
49+
cancelAnimationFrame(animationID);
50+
const movedBy = currentTranslate - prevTranslate;
51+
if (movedBy < -100 && currentIndex < slides.length - 1) currentIndex += 1;
52+
if (movedBy > 100 && currentIndex > 0) currentIndex -= 1;
53+
setPositionByIndex();
54+
slider.classList.remove("grabbing");
55+
};
56+
57+
const touchMove = (event) => {
58+
if (isDragging) {
59+
const currentPosition = getPositionX(event);
60+
currentTranslate = prevTranslate + currentPosition - startPos;
61+
}
62+
};
63+
64+
slides.forEach((slide, index) => {
65+
const slideImage = slide.querySelector("img");
66+
slideImage.addEventListener("dragstart", (e) => e.preventDefault());
67+
// Touch events
68+
slide.addEventListener("touchstart", touchStart(index));
69+
slide.addEventListener("touchend", touchEnd);
70+
slide.addEventListener("touchmove", touchMove);
71+
// Mouse events
72+
slide.addEventListener("mousedown", touchStart(index));
73+
slide.addEventListener("mouseup", touchEnd);
74+
slide.addEventListener("mouseleave", touchEnd);
75+
slide.addEventListener("mousemove", touchMove);
76+
});

53-touch slider/style.css

+69
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
@import url("https://fonts.googleapis.com/css2?family=Open+Sans&display=swap");
2+
3+
* {
4+
box-sizing: border-box;
5+
margin: 0;
6+
padding: 0;
7+
}
8+
9+
html,
10+
body {
11+
font-family: "Open Sans", sans-serif;
12+
height: 100%;
13+
width: 100%;
14+
overflow: hidden;
15+
background-color: #6c60b3;
16+
color: #fff;
17+
line-height: 1.7;
18+
}
19+
20+
.slider-container {
21+
height: 100vh;
22+
display: inline-flex;
23+
overflow: hidden;
24+
transform: translateX(0);
25+
transition: transform 0.3s ease-out;
26+
cursor: grab;
27+
}
28+
29+
.slide {
30+
max-height: 100vh;
31+
width: 100vw;
32+
display: flex;
33+
flex-direction: column;
34+
align-items: center;
35+
justify-content: center;
36+
padding: 1rem;
37+
user-select: none;
38+
}
39+
40+
.slide img {
41+
max-width: 100%;
42+
max-height: 60%;
43+
transition: transform 0.3s ease-in-out;
44+
}
45+
46+
.slide h2 {
47+
font-size: 2.5rem;
48+
margin-bottom: 0.5rem;
49+
}
50+
51+
.slide h4 {
52+
font-size: 1.3rem;
53+
}
54+
55+
.btn {
56+
background-color: #020202;
57+
color: #fff;
58+
text-decoration: none;
59+
padding: 1rem 1.5rem;
60+
border-radius: 2rem;
61+
}
62+
63+
.grabbing {
64+
cursor: grabbing;
65+
}
66+
67+
.grabbing .slide img {
68+
transform: scale(0.9);
69+
}

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -58,5 +58,6 @@
5858
| 50 | [Insect Catch Game](https://github.com/solygambas/html-css-fifty-projects/tree/master/50-insect%20catch%20game) | [Live Demo](https://codepen.io/solygambas/full/oNzRbKx) |
5959
| 51 | [Video Background](https://github.com/solygambas/html-css-fifty-projects/tree/master/51-video%20background) | [Live Demo](https://codepen.io/solygambas/full/oNYNLwL) |
6060
| 52 | [Portfolio with CSS Grid](https://github.com/solygambas/html-css-fifty-projects/tree/master/52-portfolio%20grid) | [Live Demo](https://codepen.io/solygambas/full/MWbKzzO) |
61+
| 53 | [Touch Slider](https://github.com/solygambas/html-css-fifty-projects/tree/master/53-touch%20slider) | [Live Demo](https://codepen.io/solygambas/full/QWGEyLK) |
6162

6263
Based on [50 Projects In 50 Days - HTML, CSS & JavaScript](https://www.udemy.com/course/50-projects-50-days/) by Brad Traversy (2020).

0 commit comments

Comments
 (0)