-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
189 lines (146 loc) · 6.1 KB
/
index.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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
const allSections = document.querySelectorAll('section');
const menuButton = document.querySelector('.header__menu-btn-container');
const menu = document.querySelector('.menu');
const menuItems = document.querySelectorAll('.menu__item');
const header = document.querySelector('.header');
const menuCheckbox = document.querySelector('.header__checkbox');
const priceTable = document.querySelector('.price__table');
const priceTableHeader = document.querySelector('.table-header');
const priceTableRows = document.querySelectorAll('.price__table-row');
const priceBtnRow = document.querySelector('.price__btn-row');
const priceTableFirstRow = document.querySelector('#price__table-first-row');
const priceTablePriceDesc = document.querySelector('#price__table-price-desc');
let priceTableTop;
let priceTablePriceDescTop;
let maxMarginValue;
const getOffsetRect = (el) => {
let rect = el.getBoundingClientRect();
// add window scroll position to get the offset position
let left = rect.left + window.scrollX;
let top = rect.top + window.scrollY;
let right = rect.right + window.scrollX;
let bottom = rect.bottom + window.scrollY;
// polyfill missing 'x' and 'y' rect properties not returned
// from getBoundingClientRect() by older browsers
let x = rect.x === undefined ? left : rect.x + window.scrollX;
let y = rect.y === undefined ? top : rect.y + window.scrollY;
// width and height are the same
let width = rect.width;
let height = rect.height;
return {left, top, right, bottom, x, y, width, height};
};
const scrollSpy = () => {
const scrollTop = window.scrollY + header.offsetHeight;
allSections.forEach(section => {
const id = section.id;
const offset = getOffsetRect(section).top - 1;
const height = section.offsetHeight;
if (scrollTop > offset && scrollTop < offset + height) {
menuItems.forEach(item => {
item.classList.remove('menu__item_type_active');
})
const menuItemActive = menu.querySelector('[data-scroll="' + id + '"]');
if (menuItemActive) menuItemActive.classList.add('menu__item_type_active');
}
});
}
const handleMenuToggle = (open = true) => {
// 3TO MY>I< HAPE(I)AKTOPNIL
const method = open ? 'toggle' : 'remove';
menuItems.forEach(item => item.classList[method]('menu__item_opened'));
menu.classList[method]('menu_opened');
if (!open) menuCheckbox.checked = false;
};
const handlePriceTableHeader = () => {
if ((window.scrollY + header.offsetHeight) >= priceTableTop) {
priceTableFirstRow.classList.add('price__table-row_style_margin');
if ((window.scrollY + header.offsetHeight + priceTableHeader.offsetHeight) >= priceTablePriceDescTop) {
priceTableHeader.style.top = (priceTablePriceDescTop - priceTableHeader.offsetHeight) + 'px';
priceTableHeader.style.position = 'absolute';
} else {
priceTableHeader.style.top = header.offsetHeight + 'px';
priceTableHeader.style.position = 'fixed';
}
} else {
priceTableHeader.style.position = 'static';
priceTableFirstRow.classList.remove('price__table-row_style_margin');
}
}
const checkWindowSize = () => {
if (window.innerWidth > 767 && menuCheckbox.checked) {
handleMenuToggle(false);
} else {
maxMarginValue = priceTable.offsetWidth - window.innerWidth;
priceTableTop = getOffsetRect(priceTable).top;
priceTablePriceDescTop = getOffsetRect(priceTablePriceDesc).top;
}
}
document.addEventListener('scroll', scrollSpy);
window.addEventListener('resize', checkWindowSize);
menu.addEventListener('click', () => {
handleMenuToggle(false)
});
menuButton.addEventListener('click', () => {
handleMenuToggle()
});
window.addEventListener("load", () => {
maxMarginValue = priceTable.offsetWidth - window.innerWidth;
let priceTableRowOffset = 0;
const rows = [...priceTableRows, priceBtnRow];
rows.forEach(o => {
o.style.marginLeft = priceTableRowOffset;
});
let touchstartScreenX = 0;
priceTable.addEventListener('touchstart', e => {
touchstartScreenX = e.touches[0] || e.changedTouches[0];
});
priceTable.addEventListener('touchmove', e => {
let touch = e.touches[0] || e.changedTouches[0];
let touchScreenX = touch.screenX;
priceTableRowOffset = touchScreenX - touchstartScreenX;
let newMargin = parseInt(rows[0].style.marginLeft) + priceTableRowOffset;
if (newMargin <= 0 && Math.abs(newMargin) < maxMarginValue) {
rows.forEach(o => {
o.style.marginLeft = newMargin + 'px';
});
}
touchstartScreenX = touchScreenX;
});
priceTableTop = getOffsetRect(priceTable).top;
priceTablePriceDescTop = getOffsetRect(priceTablePriceDesc).top;
document.addEventListener('scroll', handlePriceTableHeader);
});
/**
* yandexMap
*/
const yandexMapScript = document.createElement('script');
yandexMapScript.src = "https://api-maps.yandex.ru/2.1/?lang=ru_RU";
document.querySelector('head').appendChild(yandexMapScript);
if (yandexMapScript.readyState) { //IE
yandexMapScript.onreadystatechange = () => {
if (yandexMapScript.readyState === "loaded" || yandexMapScript.readyState === "complete") {
yandexMapScript.onreadystatechange = null;
ymaps.ready(init);
}
};
} else { //Others
yandexMapScript.onload = () => {
ymaps.ready(init);
};
}
let yandexMap, yandexPlacemark;
const init = () => {
const coords = [59.958264, 30.313757];
yandexMap = new ymaps.Map("map__canvas", {
center: coords,
zoom: 16,
controls: [],
//behaviors: ["scrollZoom"]
});
yandexMap.behaviors.disable('drag');
yandexMap.behaviors.disable("scrollZoom");
yandexPlacemark = new ymaps.Placemark(coords, {
hintContent: '■. █■■■■-█■■■■■■■■, ■■-■ █■■■■■■■■■■■■■■■■, 12, ■■■■■ █, ■■■. 2-█, ■■. 58',
});
yandexMap.geoObjects.add(yandexPlacemark);
}