Skip to content

Commit 4c08f02

Browse files
committed
up
1 parent 508969c commit 4c08f02

92 files changed

Lines changed: 4268 additions & 0 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎2-ui/2-events/1-introduction-browser-events/1-hide-other/solution.md‎

Whitespace-only changes.
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<!DOCTYPE HTML>
2+
<html>
3+
4+
<head>
5+
<meta charset="utf-8">
6+
</head>
7+
8+
<body>
9+
10+
<input type="button" id="hider" value="Click to hide the text" />
11+
12+
<div id="text">Text</div>
13+
14+
<script>
15+
// Here it doesn't matter how we hide the text,
16+
// could also use style.display:
17+
document.getElementById('hider').onclick = function() {
18+
document.getElementById('text').hidden = true;
19+
}
20+
</script>
21+
</body>
22+
</html>
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<!DOCTYPE HTML>
2+
<html>
3+
4+
<head>
5+
<meta charset="utf-8">
6+
</head>
7+
8+
<body>
9+
10+
<input type="button" id="hider" value="Нажмите, чтобы спрятать текст" />
11+
12+
<div id="text">Текст</div>
13+
14+
<script>
15+
/* ваш код */
16+
</script>
17+
</body>
18+
19+
</html>
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
importance: 5
2+
3+
---
4+
5+
# Hide on click
6+
7+
Add JavaScript to the `button` to make `<div id="text">` disappear when we click it.
8+
9+
The demo:
10+
11+
[iframe border=1 src="solution" height=80]
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
Can use `this` in the handler to reference "itself" here:
2+
3+
```html run height=50
4+
<input type="button" onclick="this.hidden=true" value="Click to hide">
5+
```
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
importance: 5
2+
3+
---
4+
5+
# Hide self
6+
7+
Create a button that hides itself on click.
8+
9+
```online
10+
Like this:
11+
<input type="button" onclick="this.hidden=true" value="Click to hide">
12+
```
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
The answer: `1` and `2`.
2+
3+
The first handler triggers, because it's not removed by `removeEventListener`. To remove the handler we need to pass exactly the function that was assigned. And in the code a new function is passed, that looks the same, but is still another function.
4+
5+
To remove a function object, we need to store a reference to it, like this:
6+
7+
```js
8+
function handler() {
9+
alert(1);
10+
}
11+
12+
button.addEventListener("click", handler);
13+
button.removeEventListener("click", handler);
14+
```
15+
16+
The handler `button.onclick` works independantly and in addition to `addEventListener`.
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
importance: 5
2+
3+
---
4+
5+
# Which handlers run?
6+
7+
There's a button in the variable. There are no handlers on it.
8+
9+
Which handlers run on click after the following code? Which alerts show up?
10+
11+
```js no-beautify
12+
button.addEventListener("click", () => alert("1"));
13+
14+
button.removeEventListener("click", () => alert("1"));
15+
16+
button.onclick = () => alert(2);
17+
```
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
2+
# HTML/CSS
3+
First let's create HTML/CSS.
4+
5+
A menu is a standalone graphical component on the page, so its better to put it into a single DOM element.
6+
7+
A list of menu items can be layed out as a list `ul/li`.
8+
9+
Here's the example structure:
10+
11+
```html
12+
<div class="menu">
13+
<span class="title">Sweeties (click me)!</span>
14+
<ul>
15+
<li>Cake</li>
16+
<li>Donut</li>
17+
<li>Honey</li>
18+
</ul>
19+
</div>
20+
```
21+
22+
We use `<span>` for the title, because `<div>` has an implicit `display:block` on it, and it will occupy 100% of the horizontal width.
23+
24+
Like this:
25+
26+
```html autorun height=50
27+
<div style="border: solid red 1px" onclick="alert(1)">Sweeties (click me)!</div>
28+
```
29+
30+
So if we set `onclick` on it, then it will catch clicks to the right of the text.
31+
32+
...but `<span>` has an implicit `display: inline`, so it occupies exactly enough place to fit all the text:
33+
34+
```html autorun height=50
35+
<span style="border: solid red 1px" onclick="alert(1)">Sweeties (click me)!</span>
36+
```
37+
38+
# Toggling the menu
39+
40+
Toggling the menu should change the arrow and show/hide the menu list.
41+
42+
All these changes are perfectly handled by CSS. In JavaScript we should label the current state of the menu by adding/removing the class `.open`.
43+
44+
Without it, the menu will be closed:
45+
46+
```css
47+
.menu ul {
48+
margin: 0;
49+
list-style: none;
50+
padding-left: 20px;
51+
display: none;
52+
}
53+
54+
.menu .title::before {
55+
content: '▶ ';
56+
font-size: 80%;
57+
color: green;
58+
}
59+
```
60+
61+
...And with `.open` the arrow changes and the list shows up:
62+
63+
```css
64+
.menu.open .title::before {
65+
content: '▼ ';
66+
}
67+
68+
.menu.open ul {
69+
display: block;
70+
}
71+
```
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
<!DOCTYPE HTML>
2+
<html>
3+
4+
<head>
5+
<meta charset="utf-8">
6+
<style>
7+
.menu ul {
8+
margin: 0;
9+
list-style: none;
10+
padding-left: 20px;
11+
display: none;
12+
}
13+
14+
.menu .title {
15+
font-size: 18px;
16+
cursor: pointer;
17+
}
18+
19+
.menu .title::before {
20+
content: '▶ ';
21+
font-size: 80%;
22+
color: green;
23+
}
24+
25+
.menu.open .title::before {
26+
content: '▼ ';
27+
}
28+
29+
.menu.open ul {
30+
display: block;
31+
}
32+
</style>
33+
</head>
34+
35+
<body>
36+
37+
<div id="sweeties" class="menu">
38+
<span class="title">Sweeties (click me)!</span>
39+
<ul>
40+
<li>Cake</li>
41+
<li>Donut</li>
42+
<li>Honey</li>
43+
</ul>
44+
45+
</div>
46+
47+
<script>
48+
let menuElem = document.getElementById('sweeties');
49+
let titleElem = menuElem.querySelector('.title');
50+
51+
titleElem.onclick = function() {
52+
menuElem.classList.toggle('open');
53+
};
54+
</script>
55+
56+
</body>
57+
</html>

0 commit comments

Comments
 (0)