-
Notifications
You must be signed in to change notification settings - Fork 51
/
script.js
171 lines (154 loc) · 6.53 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
164
165
166
167
168
169
170
171
// Elements
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');
// ===================================
// DARK/LIGHT THEME
// Set initial theme from LocalStorage
document.onload = setInitialTheme(localStorage.getItem('theme'));
function setInitialTheme(themeKey) {
if (themeKey === 'dark') {
document.documentElement.classList.add('darkTheme');
} else {
document.documentElement.classList.remove('darkTheme');
}
}
// Toggle theme button
toggleThemeBtn.addEventListener('click', () => {
// Toggle root class
document.documentElement.classList.toggle('darkTheme');
// Saving current theme on LocalStorage
if (document.documentElement.classList.contains('darkTheme')) {
localStorage.setItem('theme', 'dark');
} else {
localStorage.setItem('theme', 'light');
}
});
// ===================================
// STORIES SCROLL BUTTONS
// Scrolling stories content
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'));
}
// ===================================
// POST MULTIPLE MEDIAS
// Creating scroll buttons and indicators when post has more than one media
posts.forEach((post) => {
if (post.querySelectorAll('.post__media').length > 1) {
const leftButtonElement = document.createElement('button');
leftButtonElement.classList.add('post__left-button');
leftButtonElement.innerHTML = `
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 512 512">
<path fill="#fff" d="M256 504C119 504 8 393 8 256S119 8 256 8s248 111 248 248-111 248-248 248zM142.1 273l135.5 135.5c9.4 9.4 24.6 9.4 33.9 0l17-17c9.4-9.4 9.4-24.6 0-33.9L226.9 256l101.6-101.6c9.4-9.4 9.4-24.6 0-33.9l-17-17c-9.4-9.4-24.6-9.4-33.9 0L142.1 239c-9.4 9.4-9.4 24.6 0 34z"></path>
</svg>
`;
const rightButtonElement = document.createElement('button');
rightButtonElement.classList.add('post__right-button');
rightButtonElement.innerHTML = `
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 512 512">
<path fill="#fff" d="M256 8c137 0 248 111 248 248S393 504 256 504 8 393 8 256 119 8 256 8zm113.9 231L234.4 103.5c-9.4-9.4-24.6-9.4-33.9 0l-17 17c-9.4 9.4-9.4 24.6 0 33.9L285.1 256 183.5 357.6c-9.4 9.4-9.4 24.6 0 33.9l17 17c9.4 9.4 24.6 9.4 33.9 0L369.9 273c9.4-9.4 9.4-24.6 0-34z"></path>
</svg>
`;
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'));
}
}
});