Skip to content

Commit a49277f

Browse files
committed
4-behavior-tooltip
1 parent 9825952 commit a49277f

3 files changed

Lines changed: 36 additions & 36 deletions

File tree

2-ui/2-events/03-event-delegation/4-behavior-tooltip/solution.view/index.html

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
<style>
77
body {
88
height: 2000px;
9-
/* make body scrollable, the tooltip should work after the scroll */
9+
/* доавим body прокрутку, подсказка должна работать и с прокруткой */
1010
}
1111

1212
.tooltip {
@@ -25,13 +25,13 @@
2525

2626
<body>
2727

28-
<p>LaLaLa LaLaLa LaLaLa LaLaLa LaLaLa LaLaLa LaLaLa LaLaLa LaLaLa</p>
29-
<p>LaLaLa LaLaLa LaLaLa LaLaLa LaLaLa LaLaLa LaLaLa LaLaLa LaLaLa</p>
28+
<p>ЛяЛяЛя ЛяЛяЛя ЛяЛяЛя ЛяЛяЛя ЛяЛяЛя ЛяЛяЛя ЛяЛяЛя ЛяЛяЛя ЛяЛяЛя</p>
29+
<p>ЛяЛяЛя ЛяЛяЛя ЛяЛяЛя ЛяЛяЛя ЛяЛяЛя ЛяЛяЛя ЛяЛяЛя ЛяЛяЛя ЛяЛяЛя</p>
3030

31-
<button data-tooltip="the tooltip is longer than the element">Short button</button>
32-
<button data-tooltip="HTML<br>tooltip">One more button</button>
31+
<button data-tooltip="эта подсказка должна быть длиннее, чем элемент">Маленькая кнопка</button>
32+
<button data-tooltip="HTML<br>подсказка">Ещё одна</button>
3333

34-
<p>Scroll the page to make buttons appear on the top, check if the tooltips show up correctly.</p>
34+
<p>Прокрутите страницу, чтобы кнопки оказались сверху, проверьте правильно ли выводятся подсказки.</p>
3535

3636

3737
<script>
@@ -40,25 +40,25 @@
4040
document.onmouseover = function(event) {
4141
let target = event.target;
4242

43-
// if we have tooltip HTML...
43+
// получим содержимое подсказки...
4444
let tooltipHtml = target.dataset.tooltip;
4545
if (!tooltipHtml) return;
4646

47-
// ...create the tooltip element
47+
// ...создадим элемент подсказки
4848

4949
tooltipElem = document.createElement('div');
5050
tooltipElem.className = 'tooltip';
5151
tooltipElem.innerHTML = tooltipHtml;
5252
document.body.append(tooltipElem);
5353

54-
// position it above the annotated element (top-center)
54+
// спозиционируем его сверху от аннотируемого элемента (top-center)
5555
let coords = target.getBoundingClientRect();
5656

5757
let left = coords.left + (target.offsetWidth - tooltipElem.offsetWidth) / 2;
58-
if (left < 0) left = 0; // don't cross the left window edge
58+
if (left < 0) left = 0; // не заезжать за левый край окна
5959

6060
let top = coords.top - tooltipElem.offsetHeight - 5;
61-
if (top < 0) { // if crossing the top window edge, show below instead
61+
if (top < 0) { // если подсказка не помещается сверху, то отображать её снизу
6262
top = coords.top + target.offsetHeight + 5;
6363
}
6464

2-ui/2-events/03-event-delegation/4-behavior-tooltip/source.view/index.html

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,11 @@
66
<style>
77
body {
88
height: 2000px;
9-
/* make body scrollable, the tooltip should work after the scroll */
9+
/* доавим body прокрутку, подсказка должна работать и с прокруткой */
1010
}
1111

1212
.tooltip {
13-
/* some styles for the tooltip, you can use your own instead */
13+
/* немного стилизуем подсказку, можете использовать свои вместо этих */
1414
position: fixed;
1515
padding: 10px 20px;
1616
border: 1px solid #b3c9ce;
@@ -26,17 +26,17 @@
2626

2727
<body>
2828

29-
<p>LaLaLa LaLaLa LaLaLa LaLaLa LaLaLa LaLaLa LaLaLa LaLaLa LaLaLa</p>
30-
<p>LaLaLa LaLaLa LaLaLa LaLaLa LaLaLa LaLaLa LaLaLa LaLaLa LaLaLa</p>
29+
<p>ЛяЛяЛя ЛяЛяЛя ЛяЛяЛя ЛяЛяЛя ЛяЛяЛя ЛяЛяЛя ЛяЛяЛя ЛяЛяЛя ЛяЛяЛя</p>
30+
<p>ЛяЛяЛя ЛяЛяЛя ЛяЛяЛя ЛяЛяЛя ЛяЛяЛя ЛяЛяЛя ЛяЛяЛя ЛяЛяЛя ЛяЛяЛя</p>
3131

32-
<button data-tooltip="the tooltip is longer than the element">Short button</button>
33-
<button data-tooltip="HTML<br>tooltip">One more button</button>
32+
<button data-tooltip="эта подсказка должна быть длиннее, чем элемент">Маленькая кнопка</button>
33+
<button data-tooltip="HTML<br>подсказка">Ещё одна</button>
3434

35-
<p>Scroll the page to make buttons appear on the top, check if the tooltips show up correctly.</p>
35+
<p>Прокрутите страницу, чтобы кнопки оказались сверху, проверьте правильно ли выводятся подсказки.</p>
3636

3737

3838
<script>
39-
// ...your code...
39+
// ...ваш код...
4040
</script>
4141

4242
</body>

2-ui/2-events/03-event-delegation/4-behavior-tooltip/task.md

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,35 +2,35 @@ importance: 5
22

33
---
44

5-
# Tooltip behavior
5+
# Поведение "подсказка"
66

7-
Create JS-code for the tooltip behavior.
7+
Напишите JS-код, чтобы придать подсказке поведение.
88

9-
When a mouse comes over an element with `data-tooltip`, the tooltip should appear over it, and when it's gone then hide.
9+
При наведении мыши на элемент с атрибутом `data-tooltip`, над ним должна показываться подсказка и скрываться при переходе на другой элемент.
1010

11-
An example of annotated HTML:
11+
Пример аннотированного HTML:
1212
```html
13-
<button data-tooltip="the tooltip is longer than the element">Short button</button>
14-
<button data-tooltip="HTML<br>tooltip">One more button</button>
13+
<button data-tooltip="эта подсказка длиннее, чем элемент">Короткая кнопка</button>
14+
<button data-tooltip="HTML<br>подсказка">Ещё кнопка</button>
1515
```
1616

17-
Should work like this:
17+
Результат в ифрейме с документом:
1818

1919
[iframe src="solution" height=200 border=1]
2020

21-
In this task we assume that all elements with `data-tooltip` have only text inside. No nested tags.
21+
В этой задаче можно полагать, что в элементе с атрибутом `data-tooltip` -- только текст, то есть нет подэлементов.
2222

23-
Details:
23+
Детали оформления:
2424

25-
- The tooltip should not cross window edges. Normally it should be above the element, but if the element is at the page top and there's no space for the tooltip, then below it.
26-
- The tooltip is given in the `data-tooltip` attribute. It can be arbitrary HTML.
25+
- Подсказка не должна вылезать за границы экрана, в том числе если страница частично прокручена. Если нельзя показать сверху -- показывать снизу элемента.
26+
- Текст подсказки брать из значения атрибута `data-tooltip`. Это может быть произвольный HTML.
2727

28-
You'll need two events here:
29-
- `mouseover` triggers when a pointer comes over an element.
30-
- `mouseout` triggers when a pointer leaves an element.
28+
Для решения вам понадобятся два события:
29+
- `mouseover` срабатывает, когда указатель мыши заходит на элемент.
30+
- `mouseout` срабатывает, когда указатель мыши уходит с элемента.
3131

32-
Please use event delegation: set up two handlers on `document` to track all "overs" and "outs" from elements with `data-tooltip` and manage tooltips from there.
32+
Примените делегацию событий: установите оба обработчика на элемент `document`, чтобы отслеживать "заход" и "уход" курсора на элементы с атрибутом `data-tooltip` и управлять подсказками с их же помощью.
3333

34-
After the behavior is implemented, even people unfamiliar with JavaScript can add annotated elements.
34+
После реализации поведения -- люди, даже не знакомые с JavaScript смогут аннотировать элементы.
3535

36-
P.S. To keep things natural and simple: only one tooltip may show up at a time.
36+
P.S. Чтобы всё выглядело естественно и просто: показывайте одновременно лишь одну подсказку.

0 commit comments

Comments
 (0)