Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 180
Introduction to browser events#292
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Uh oh!
There was an error while loading. Please reload this page.
Changes from all commits
a11e2a3d073b926f63dff690516fd1d20f9126b55b1808c2fb7f4746c31ff5168d53eb019602e550b1fb787568b82f0f9acbda1c03f73a4b007d5ac0a58cb515efbc07758975ca0c3b086fccb41ea7283676d942eff8fea5cb6b184a1c393c987f1db5a26f7163d7e63063331ca8f7bb4684a0d9266f243b740f2bda1c775777c31599737fcdf34abbee83028e473d3d5f632ec0286d4e1ae78c2397c96ef0bd682c446c1b3cabe49321de25ac3d8723c6012a092cc68d4eba57f57848f73b996712918a89acea953a2b306d97cea9efb2ebf4bb145dc40c13caef75f274b1552605ab67da7a93dbf1f1bFile filter
Filter by extension
Conversations
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,5 @@ | ||
| Can use `this` in the handler to reference "the element itself" here: | ||
| Можемо використати `this` в обробнику для доступу до самого елемента: | ||
| ```html run height=50 | ||
| <input type="button" onclick="this.hidden=true" value="Click to hide"> | ||
| <input type="button" onclick="this.hidden=true" value="Сховати"> | ||
| ``` |
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -1,12 +1,12 @@ | ||||||
| importance: 5 | ||||||
| важливість: 5 | ||||||
Collaborator There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
| ||||||
| --- | ||||||
| # Which handlers run? | ||||||
| # Який обробник запуститься? | ||||||
Collaborator There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
| ||||||
| There's a button in the variable. There are no handlers on it. | ||||||
| У змінній `button` знаходиться кнопка. Спочатку на ній немає обробників. | ||||||
| Which handlers run on click after the following code? Which alerts show up? | ||||||
| Який з обробників запуститься? Що буде виведено під час кліку після виконання коду? | ||||||
Collaborator There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
| ||||||
| ```js no-beautify | ||||||
| button.addEventListener("click", () => alert("1")); | ||||||
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -1,11 +1,11 @@ | ||||||
| First we need to choose a method of positioning the ball. | ||||||
| Спочатку ми маємо вибрати метод позиціювання м’яча. | ||||||
| We can't use `position:fixed` for it, because scrolling the page would move the ball from the field. | ||||||
| Ми не можемо використати `position:fixed` для цього, оскільки прокручування сторінки переміщатиме м’яч поля. | ||||||
Collaborator There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
| ||||||
| So we should use `position:absolute` and, to make the positioning really solid, make `field` itself positioned. | ||||||
| Правильніше використовувати `position:absolute` і, щоб зробити позиціювання справді надійним, зробимо саме поле `field` позиціонованим. | ||||||
| Then the ball will be positioned relatively to the field: | ||||||
| Тоді м’яч буде позиціонований щодо поля: | ||||||
Collaborator There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
| ||||||
| ```css | ||||||
| #field { | ||||||
| @@ -16,36 +16,36 @@ Then the ball will be positioned relatively to the field: | ||||||
| #ball { | ||||||
| position: absolute; | ||||||
| left: 0; /* relative to the closest positioned ancestor (field) */ | ||||||
| left: 0; /* по відношенню до найближчого розташованого предка (field) */ | ||||||
| top: 0; | ||||||
| transition: 1s all; /* CSS animation for left/top makes the ball fly */ | ||||||
| transition: 1s all; /* CSS-анімація для значень left/top робить пересування м’яча плавним */ | ||||||
| } | ||||||
| ``` | ||||||
| Next we need to assign the correct `ball.style.left/top`. They contain field-relative coordinates now. | ||||||
| Далі ми маємо призначити коректні значення `ball.style.left/top`. Зараз вони містять координати щодо поля. | ||||||
Collaborator There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
Здається, що так краще, як вважаєте? | ||||||
| Here's the picture: | ||||||
| Як на зображенні: | ||||||
|  | ||||||
| We have `event.clientX/clientY` -- window-relative coordinates of the click. | ||||||
| Ми маємо `event.clientX/clientY` -- координати натискання мишки щодо вікна браузера. | ||||||
| To get field-relative `left` coordinate of the click, we can substract the field left edge and the border width: | ||||||
| Щоб отримати значення `left` для м’яча після натискання мишки щодо поля, ми повинні від координати натискання мишки відняти координату лівого краю поля та ширину межі: | ||||||
Collaborator There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
| ||||||
| ```js | ||||||
| let left = event.clientX - fieldCoords.left - field.clientLeft; | ||||||
| ``` | ||||||
| Normally, `ball.style.left` means the "left edge of the element" (the ball). So if we assign that `left`, then the ball edge, not center, would be under the mouse cursor. | ||||||
| Значення `ball.style.left` означає «лівий край елемента» (м’яча). І якщо ми призначимо такий `left` для м’яча, то його ліва межа, а не центр, буде під курсором миші. | ||||||
Collaborator There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
| ||||||
| We need to move the ball half-width left and half-height up to make it center. | ||||||
| Нам потрібно зрушити м'яч на половину його висоти вгору та половину його ширини вліво, щоб центр м’яча точно збігався з точкою натискання мишки. | ||||||
Collaborator There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
| ||||||
| So the final `left` would be: | ||||||
| У результаті значення для `left` буде таким: | ||||||
Collaborator There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
| ||||||
| ```js | ||||||
| let left = event.clientX - fieldCoords.left - field.clientLeft - ball.offsetWidth/2; | ||||||
| ``` | ||||||
| The vertical coordinate is calculated using the same logic. | ||||||
| Вертикальна координата обчислюватиметься за тією ж логікою. | ||||||
Collaborator There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
| ||||||
| Please note that the ball width/height must be known at the time we access `ball.offsetWidth`. Should be specified in HTML or CSS. | ||||||
| Слід пам’ятати, що ширина та висота м’яча має бути відома в той момент, коли ми отримуємо значення `ball.offsetWidth`. Це значення може бути задано в HTML або CSS. | ||||||
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -27,7 +27,7 @@ | ||||||
| <body style="height:2000px"> | ||||||
| Click on a field to move the ball there. | ||||||
| Натисніть поле, щоб перемістити м’яч. | ||||||
Collaborator There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
| ||||||
| <br> | ||||||
| @@ -39,29 +39,29 @@ | ||||||
| <script> | ||||||
| field.onclick = function(event) { | ||||||
| // window-relative field coordinates | ||||||
| // координати поля щодо вікна браузера | ||||||
Collaborator There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
| ||||||
| let fieldCoords = this.getBoundingClientRect(); | ||||||
| // the ball has position:absolute, the field: position:relative | ||||||
| // so ball coordinates are relative to the field inner left-upper corner | ||||||
| // м’яч має абсолютне позиціювання (position:absolute), поле -- відносне (position:relative) | ||||||
| // таким чином, координати м’яча розраховуються відносно внутрішнього, верхнього лівого кута поля. | ||||||
| let ballCoords = { | ||||||
| top: event.clientY - fieldCoords.top - field.clientTop - ball.clientHeight / 2, | ||||||
| left: event.clientX - fieldCoords.left - field.clientLeft - ball.clientWidth / 2 | ||||||
| }; | ||||||
| // prevent crossing the top field boundary | ||||||
| // забороняємо перетинати верхню межу поля | ||||||
| if (ballCoords.top < 0) ballCoords.top = 0; | ||||||
| // prevent crossing the left field boundary | ||||||
| // забороняємо перетинати ліву межу поля | ||||||
| if (ballCoords.left < 0) ballCoords.left = 0; | ||||||
| // // prevent crossing the right field boundary | ||||||
| // забороняємо перетинати праву межу поля | ||||||
| if (ballCoords.left + ball.clientWidth > field.clientWidth) { | ||||||
| ballCoords.left = field.clientWidth - ball.clientWidth; | ||||||
| } | ||||||
| // prevent crossing the bottom field boundary | ||||||
| // забороняємо перетинати нижню межу поля | ||||||
| if (ballCoords.top + ball.clientHeight > field.clientHeight) { | ||||||
| ballCoords.top = field.clientHeight - ball.clientHeight; | ||||||
| } | ||||||
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -1,12 +1,12 @@ | ||||||
| To add the button we can use either `position:absolute` (and make the pane `position:relative`) or `float:right`. The `float:right` has the benefit that the button never overlaps the text, but `position:absolute` gives more freedom. So the choice is yours. | ||||||
| Щоб додати кнопку закриття, ми можемо використовувати або `position:absolute` (і зробити плитку (pane) `position:relative`) або `float:right`. Перевага варіанта з `float:right` у тому, що кнопка закриття ніколи не перекриє текст, але варіант `position:absolute` дає більше свободи для дій. Загалом вибір за вами. | ||||||
Collaborator There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
| ||||||
| Then for each pane the code can be like: | ||||||
| Тоді для кожного повідомлення(pane) код може бути таким: | ||||||
| ```js | ||||||
| pane.insertAdjacentHTML("afterbegin", '<button class="remove-button">[x]</button>'); | ||||||
| ``` | ||||||
| Then the `<button>` becomes `pane.firstChild`, so we can add a handler to it like this: | ||||||
| Елемент `<button>` стає `pane.firstChild`, таким чином ми можемо додати до нього обробник події: | ||||||
| ```js | ||||||
| pane.firstChild.onclick = () => pane.remove(); | ||||||
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.