You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
First we need to choose a method of positioning the ball.
2
+
Pertama kita perlu memilih metode untuk memposisikan bola.
3
3
4
-
We can't use `position:fixed`for it, because scrolling the page would move the ball from the field.
4
+
Kita tidak dapat menggunakan `position:fixed`untuk itu, karena pada saat halaman digulir bola akan berpindah dari lapangan.
5
5
6
-
So we should use`position:absolute`and, to make the positioning really solid, make `field` itself positioned.
6
+
Jadi kita sebaiknya menggunakan`position:absolute`dan, untuk membuat posisinya benar-benar tepat, buat lapangan(_`field`_) itu sendiri diposisikan.
7
7
8
-
Then the ball will be positioned relatively to the field:
8
+
Kemudian bola akan diposisikan relatif terhadap lapangan(_`field`_):
9
9
10
10
```css
11
11
#field {
@@ -16,36 +16,36 @@ Then the ball will be positioned relatively to the field:
16
16
17
17
#ball {
18
18
position: absolute;
19
-
left: 0; /*relative to the closest positioned ancestor (field) */
19
+
left: 0; /*relatif ke posisi terdekat blok atas (lapangan) */
20
20
top: 0;
21
-
transition: 1sall; /* CSS animation for left/top makes the ball fly*/
21
+
transition: 1sall; /*Animasi CSS untuk left/top untuk membuat bolanya terbang*/
22
22
}
23
23
```
24
24
25
-
Next we need to assign the correct `ball.style.left/top`. They contain field-relative coordinates now.
25
+
Selanjutnya kita harus mengatur dengan benar `ball.style.left/top`. Mereka memiliki koordinat relatif ke lapangan(_`field`_) sekarang.
26
26
27
-
Here's the picture:
27
+
Berikut ini gambarnya:
28
28
29
29

30
30
31
-
We have`event.clientX/clientY` -- window-relative coordinates of the click.
31
+
Kita memiliki`event.clientX/clientY` -- koordinat relatif jendela (_window_) dari klik.
32
32
33
-
To get field-relative `left` coordinate of the click, we can substract the field left edge and the border width:
33
+
Untuk mendapatkan koordinat kiri relatif terhadap lapangan(_`field`_), kita kurangkan dengan nilai dari ujung dan pembatas kiri lapangan:
34
34
35
35
```js
36
36
let left =event.clientX-fieldCoords.left-field.clientLeft;
37
37
```
38
38
39
-
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.
39
+
Biasanya, `ball.style.left`artinya "ujung kiri dari elemen" (bola). Jadi kita mengatur nilai `left`, kemudian ujung bola, bukan tengah, akan berada tepat dibawah kursor mouse.
40
40
41
-
We need to move the ball half-width left and half-height up to make it center.
41
+
Kita perlu memindahkan bola setengah lebar kiri dan setengah tinggi atas untuk membuatnya di tengah.
42
42
43
-
So the final`left`would be:
43
+
Jadi nilai terakhir`left`akan menjadi:
44
44
45
45
```js
46
46
let left =event.clientX-fieldCoords.left-field.clientLeft-ball.offsetWidth/2;
47
47
```
48
48
49
-
The vertical coordinate is calculated using the same logic.
49
+
Koordinat vertikal di ukur dengan menggunakan logika yang sama.
50
50
51
-
Please note that the ball width/height must be known at the time we access`ball.offsetWidth`. Should be specified in HTML or CSS.
51
+
Harap dicatat bahwa lebar/tinggi bola harus di ketahui pada saat kita mengakses`ball.offsetWidth`. Sebaiknya di atur pada HTML atau CSS.
Copy file name to clipboardExpand all lines: 2-ui/2-events/01-introduction-browser-events/article.md
+26-26Lines changed: 26 additions & 26 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -350,44 +350,44 @@ Ini terjadi karena pada saat peramban membaca atribut, itu membuat sebuah penang
350
350
````
351
351
352
352
353
-
## Object handlers: handleEvent
353
+
## Objek penangan: handleEvent
354
354
355
-
We can assign not just a function, but an object as an event handler using `addEventListener`. When an event occurs, its `handleEvent` method is called.
355
+
Kita bisa mengatur bukan hanya fungsi, tapi sebuah objek sebagai penangan peristiwa menggunakan `addEventListener`. Pada saat sebuah peristiwa terjadi, Itu memanggil metode `handleEvent`.
356
356
357
-
For instance:
357
+
Contohnya:
358
358
359
359
360
360
```html run
361
-
<buttonid="elem">Click me</button>
361
+
<buttonid="elem">Klik saya</button>
362
362
363
363
<script>
364
364
let obj = {
365
365
handleEvent(event) {
366
-
alert(event.type+"at"+event.currentTarget);
366
+
alert(event.type+"pada"+event.currentTarget);
367
367
}
368
368
};
369
369
370
370
elem.addEventListener('click', obj);
371
371
</script>
372
372
```
373
373
374
-
As we can see, when `addEventListener`receives an object as the handler, it calls `obj.handleEvent(event)`in case of an event.
374
+
Seperti yang bisa kita lihat, pada saat `addEventListener`menerima objek sebagai penangan, itu akan memanggil `obj.handleEvent(event)`jika sebuah peristiwa terjadi.
375
375
376
-
We could also use a class for that:
376
+
Kita juga dapat menggunakan Kelas (_class_) untuk hal itu:
377
377
378
378
379
379
```html run
380
-
<buttonid="elem">Click me</button>
380
+
<buttonid="elem">Klik saya</button>
381
381
382
382
<script>
383
383
classMenu {
384
384
handleEvent(event) {
385
385
switch(event.type) {
386
386
case'mousedown':
387
-
elem.innerHTML="Mouse button pressed";
387
+
elem.innerHTML="Tombol mouse ditekan";
388
388
break;
389
389
case'mouseup':
390
-
elem.innerHTML+="...and released.";
390
+
elem.innerHTML+="...dan dilepas.";
391
391
break;
392
392
}
393
393
}
@@ -401,12 +401,12 @@ We could also use a class for that:
401
401
</script>
402
402
```
403
403
404
-
Here the same object handles both events. Please note that we need to explicitly setup the events to listen using`addEventListener`. The`menu`object only gets`mousedown`and`mouseup`here, not any other types of events.
404
+
Disini objek yang sama menanggani kedua peristiwa. Tolong di catat bahwa kita harus secara eksplisit mengatur peristiwa untuk mendengar menggunakan`addEventListener`. Objek`menu`hanya dapat menerima`mousedown`dan`mouseup`pada contoh diatas, bukan tipe peristiwa lainnya.
405
405
406
-
The method `handleEvent`does not have to do all the job by itself. It can call other event-specific methods instead, like this:
406
+
Metode `handleEvent`tidak harus melakukan semua proses secara mandiri. Itu dapat memanggil metode lain yang menanggani peristiwa secara spesifik, seperti ini:
407
407
408
408
```html run
409
-
<buttonid="elem">Click me</button>
409
+
<buttonid="elem">Klik saya</button>
410
410
411
411
<script>
412
412
classMenu {
@@ -417,11 +417,11 @@ The method `handleEvent` does not have to do all the job by itself. It can call
417
417
}
418
418
419
419
onMousedown() {
420
-
elem.innerHTML="Mouse button pressed";
420
+
elem.innerHTML="Tombol mouse ditekan";
421
421
}
422
422
423
423
onMouseup() {
424
-
elem.innerHTML+="...and released.";
424
+
elem.innerHTML+="...dan dilepas.";
425
425
}
426
426
}
427
427
@@ -431,22 +431,22 @@ The method `handleEvent` does not have to do all the job by itself. It can call
431
431
</script>
432
432
```
433
433
434
-
Now event handlers are clearly separated, that may be easier to support.
434
+
Sekarang penangan peristiwa berbeda, dan memudahkan proses pendukungan.
HTML attributes are used sparingly, because JavaScript in the middle of an HTML tag looks a little bit odd and alien. Also can't write lots of code in there.
444
+
Atribute HTML digunakan untuk kasus tertentu, karena Javascript ditengah tag HTML akan kelihatan aneh. Dan juga akan sulit untuk menulis banyak kode di dalam tag HTML.
445
445
446
-
DOM properties are ok to use, but we can't assign more than one handler of the particular event. In many cases that limitation is not pressing.
446
+
Properti DOM boleh digunakan, tapi kita tidak dapat mengatur lebih dari 1 penangan untuk peristiwa tertentu. Namun tidak sering kita membutuhkan lebih dari 2 penangan.
447
447
448
-
The last way is the most flexible, but it is also the longest to write. There are few events that only work with it, for instance`transitionend`and`DOMContentLoaded` (to be covered). Also `addEventListener` supports objects as event handlers. In that case the method `handleEvent`is called in case of the event.
448
+
Cara terakhir lebih fleksible, tapi itu juga merupakan cara terpanjang untuk menulis. Ada beberapa peristiwa yang hanya akan bisa digunakan pada cara ini, seperti misalnya`transitionend`dan`DOMContentLoaded` (akan di bahas). Dan juga objek dapat digunakan sebagai penangan pada `addEventListener`. Pada kasus ini metode `handleEvent`akan dipanggil pada saat peristiwa terjadi.
449
449
450
-
No matter how you assign the handler -- it gets an event object as the first argument. That object contains the details about what's happened.
450
+
Tidak penting bagaimana kamu mengatur penangan -- itu akan mendapat sebuah objek peristiwa sebagai argumen pertama. Objek itu memiliki detail tentang apa yang terjadi.
451
451
452
-
We'll learn more about events in general and about different types of events in the next chapters.
452
+
Kita akan mempelajari lebih lanjut tentang peristiwa secara umum dan perbedaan tipe peristiwa di bab selanjutnya.
0 commit comments