-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
130 lines (106 loc) · 3.03 KB
/
main.js
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
const allStories = [
{
thumbUrl: "images/1-thumb.png",
imageUrl: "images/1.png",
title: "Title No. 1",
},
{
thumbUrl: "images/2-thumb.png",
imageUrl: "images/2.png",
title: "Title No. 2",
},
{
thumbUrl: "images/3-thumb.png",
imageUrl: "images/3.png",
title: "Title No. 3",
},
{
thumbUrl: "images/4-thumb.png",
imageUrl: "images/4.png",
title: "Title No. 4",
},
{
thumbUrl: "images/5-thumb.png",
imageUrl: "images/5.png",
title: "Title No. 5",
},
{
thumbUrl: "images/6-thumb.png",
imageUrl: "images/6.png",
title: "Title No. 6",
},
{
thumbUrl: "images/7-thumb.png",
imageUrl: "images/7.png",
title: "Title No. 7",
},
{
thumbUrl: "images/8-thumb.png",
imageUrl: "images/8.png",
title: "Title No. 8",
},
];
const storiesContainer = document.querySelector(".stories-container");
const storyFull = document.querySelector(".story-full");
const storyFullImage = document.querySelector(".story-full img");
const storyFullTitle = document.querySelector(".story-full .title");
const closeBtn = document.querySelector(".story-full .close-btn");
const leftArrow = document.querySelector(".story-full .left-arrow");
const rightArrow = document.querySelector(".story-full .right-arrow");
let currentIndex = 0;
let timer;
allStories.forEach((s, i) => {
const content = document.createElement("div");
content.classList.add("content");
const img = document.createElement("img");
img.setAttribute("src", s.thumbUrl);
storiesContainer.appendChild(content);
content.appendChild(img);
content.addEventListener("click", () => {
currentIndex = i;
storyFull.classList.add("active");
storyFullImage.setAttribute("src", s.imageUrl);
if (!s.title) {
storyFullTitle.style.display = "none";
} else {
storyFullTitle.style.display = "block";
storyFullTitle.innerHTML = s.title;
}
clearInterval(timer);
timer = setInterval(nextStory, 5000);
});
});
closeBtn.addEventListener("click", () => {
storyFull.classList.remove("active");
});
leftArrow.addEventListener("click", () => {
if (currentIndex > 0) {
currentIndex -= 1;
storyFullImage.setAttribute("src", allStories[currentIndex].imageUrl);
if (!allStories[currentIndex].title) {
storyFullTitle.style.display = "none";
} else {
storyFullTitle.style.display = "block";
storyFullTitle.innerHTML = allStories[currentIndex].title;
}
clearInterval(timer);
timer = setInterval(nextStory, 5000);
}
});
const nextStory = () => {
if (currentIndex < allStories.length - 1) {
currentIndex += 1;
storyFullImage.setAttribute("src", allStories[currentIndex].imageUrl);
if (!allStories[currentIndex].title) {
storyFullTitle.style.display = "none";
} else {
storyFullTitle.style.display = "block";
storyFullTitle.innerHTML = allStories[currentIndex].title;
}
}
};
rightArrow.addEventListener("click", () => {
nextStory();
clearInterval(timer);
timer = setInterval(nextStory, 5000);
});