-
Notifications
You must be signed in to change notification settings - Fork 0
/
Popup.js
30 lines (26 loc) · 1.03 KB
/
Popup.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
export default class Popup {
constructor(popupElement) {
this._popup = popupElement; // Селектор попапа
}
// Метод открытия попапа
open() {
this._popup.classList.add('popup_opened');
document.addEventListener('keydown', this._handleEscClose);
}
// Метод закрытия попапа
close() {
this._popup.classList.remove('popup_opened');
document.removeEventListener('keydown', this._handleEscClose);
}
// Метод закрытия попапа клавишей "Escape" (чтобы не потерять контекст, переделано на стрелочную функцию)
_handleEscClose = evt => {
if (evt.key === 'Escape') this.close();
}
// Метод установки обработчиков событий
setEventListeners() {
this._popup.addEventListener('mousedown', (evt) => {
if (evt.target.classList.contains('popup_opened') || evt.target.classList.contains('popup__closing-button'))
this.close();
})
}
}