-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathscript.js
163 lines (137 loc) · 5.59 KB
/
script.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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
console.log('Code is Poetry');
const toggleThemeBtn = document.querySelector('.header__theme-button');
const storiesContent = document.querySelector('.stories__content');
const storiesLeftButton = document.querySelector('.stories__left-button');
const storiesRightButton = document.querySelector('.stories__right-button');
const posts = document.querySelectorAll('.post');
const postsContent = document.querySelectorAll('.post__content');
document.onload = setInitialTheme(localStorage.getItem('theme'));
function setInitialTheme(themeKey) {
if (themeKey === 'dark') {
document.documentElement.classList.add('darkTheme');
} else {
document.documentElement.classList.remove('darkTheme');
}
}
toggleThemeBtn.addEventListener('click', () => {
document.documentElement.classList.toggle('darkTheme');
if (document.documentElement.classList.contains('darkTheme')) {
localStorage.setItem('theme', 'dark');
} else {
localStorage.setItem('theme', 'light');
}
});
// Stories scroll button
storiesLeftButton.addEventListener('click', () => {
storiesContent.scrollLeft -= 320;
});
storiesRightButton.addEventListener('click', () => {
storiesContent.scrollLeft += 320;
});
// Checking if screen has minimun size of 1024px
if (window.matchMedia('(min-width: 1024px)').matches) {
// Observer to hide buttons when necessary
const storiesObserver = new IntersectionObserver(
function (entries) {
entries.forEach((entry) => {
if (entry.target === document.querySelector('.story:first-child')) {
storiesLeftButton.style.display = entry.isIntersecting
? 'none'
: 'unset';
} else if (
entry.target === document.querySelector('.story:last-child')
) {
storiesRightButton.style.display = entry.isIntersecting
? 'none'
: 'unset';
}
});
},
{ root: storiesContent, threshold: 1 }
);
// Calling the observer with the first and last stories
storiesObserver.observe(document.querySelector('.story:first-child'));
storiesObserver.observe(document.querySelector('.story:last-child'));
}
posts.forEach((post) => {
if (post.querySelectorAll('.post__media').length > 1) {
const leftButtonElement = document.createElement('button');
leftButtonElement.classList.add('post__left-button');
leftButtonElement.innerHTML = `
`;
const rightButtonElement = document.createElement('button');
rightButtonElement.classList.add('post__right-button');
rightButtonElement.innerHTML = `
`;
post.querySelector('.post__content').appendChild(leftButtonElement);
post.querySelector('.post__content').appendChild(rightButtonElement);
post.querySelectorAll('.post__media').forEach(function () {
const postMediaIndicatorElement = document.createElement('div');
postMediaIndicatorElement.classList.add('post__indicator');
post
.querySelector('.post__indicators')
.appendChild(postMediaIndicatorElement);
});
// Observer to change the actual media indicator
const postMediasContainer = post.querySelector('.post__medias');
const postMediaIndicators = post.querySelectorAll('.post__indicator');
const postIndicatorObserver = new IntersectionObserver(
function (entries) {
entries.forEach((entry) => {
if (entry.isIntersecting) {
// Removing all the indicators
postMediaIndicators.forEach((indicator) =>
indicator.classList.remove('post__indicator--active')
);
// Adding the indicator that matches the current post media
postMediaIndicators[
Array.from(postMedias).indexOf(entry.target)
].classList.add('post__indicator--active');
}
});
},
{ root: postMediasContainer, threshold: 0.5 }
);
// Calling the observer for every post media
const postMedias = post.querySelectorAll('.post__media');
postMedias.forEach((media) => {
postIndicatorObserver.observe(media);
});
}
});
// Adding buttons features on every post with multiple medias
postsContent.forEach((post) => {
if (post.querySelectorAll('.post__media').length > 1) {
const leftButton = post.querySelector('.post__left-button');
const rightButton = post.querySelector('.post__right-button');
const postMediasContainer = post.querySelector('.post__medias');
// Functions for left and right buttons
leftButton.addEventListener('click', () => {
postMediasContainer.scrollLeft -= 400;
});
rightButton.addEventListener('click', () => {
postMediasContainer.scrollLeft += 400;
});
// Observer to hide button if necessary
const postButtonObserver = new IntersectionObserver(
function (entries) {
entries.forEach((entry) => {
if (entry.target === post.querySelector('.post__media:first-child')) {
leftButton.style.display = entry.isIntersecting ? 'none' : 'unset';
} else if (
entry.target === post.querySelector('.post__media:last-child')
) {
rightButton.style.display = entry.isIntersecting ? 'none' : 'unset';
}
});
},
{ root: postMediasContainer, threshold: 0.5 }
);
if (window.matchMedia('(min-width: 1024px)').matches) {
postButtonObserver.observe(
post.querySelector('.post__media:first-child')
);
postButtonObserver.observe(post.querySelector('.post__media:last-child'));
}
}
});