Skip to content

Commit 5a09308

Browse files
authored
Merge pull request bradtraversy#68 from siyi0211/master
Update the transition of image
2 parents af3b536 + 8ae0561 commit 5a09308

File tree

3 files changed

+35
-7
lines changed

3 files changed

+35
-7
lines changed

image-carousel/index.html

+7
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@
99
<body>
1010
<div class="carousel">
1111
<div class="image-container" id="imgs">
12+
<img
13+
src="https://images.unsplash.com/photo-1599561046251-bfb9465b4c44?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=1492&q=80"
14+
alt="fourth-image"
15+
/>
1216
<img src="https://images.unsplash.com/photo-1599394022918-6c2776530abb?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=1458&q=80"
1317
alt="first-image"
1418
/>
@@ -24,6 +28,9 @@
2428
src="https://images.unsplash.com/photo-1599561046251-bfb9465b4c44?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=1492&q=80"
2529
alt="fourth-image"
2630
/>
31+
<img src="https://images.unsplash.com/photo-1599394022918-6c2776530abb?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=1458&q=80"
32+
alt="first-image"
33+
/>
2734
</div>
2835

2936
<div class="buttons-container">

image-carousel/script.js

+27-5
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ const rightBtn = document.getElementById('right')
44

55
const img = document.querySelectorAll('#imgs img')
66

7-
let idx = 0
7+
let idx = 1
88

99
let interval = setInterval(run, 2000)
1010

@@ -15,12 +15,34 @@ function run() {
1515

1616
function changeImage() {
1717
if(idx > img.length - 1) {
18-
idx = 0
19-
} else if(idx < 0) {
2018
idx = img.length - 1
21-
}
2219

23-
imgs.style.transform = `translateX(${-idx * 500}px)`
20+
imgs.style.transition = 'all, linear, 1s';
21+
imgs.style.transform = `translateX(${-idx * 500}px)`
22+
23+
setTimeout(function() {
24+
idx = 1;
25+
imgs.style.transition = '';
26+
imgs.style.transform = `translateX(${-500}px)`
27+
},0)
28+
}
29+
else if(idx < 0){
30+
idx = 0
31+
32+
imgs.style.transition = 'all, linear, 1s';
33+
imgs.style.transform = `translateX(${-idx * 500}px)`
34+
35+
setTimeout(function() {
36+
idx = img.length - 2;
37+
imgs.style.transition = '';
38+
imgs.style.transform = `translateX(${-idx * 500}px)`
39+
},0)
40+
}
41+
else{
42+
imgs.style.transition = 'all, linear, 1s';
43+
imgs.style.transform = `translateX(${-idx * 500}px)`
44+
}
45+
2446
}
2547

2648
function resetInterval() {

image-carousel/style.css

+1-2
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,7 @@ img {
2828

2929
.image-container {
3030
display: flex;
31-
transform: translateX(0);
32-
transition: transform 0.5s ease-in-out;
31+
transform: translateX(-500px);
3332
}
3433

3534
.buttons-container {

0 commit comments

Comments
 (0)