Skip to content

Commit 19684ec

Browse files
authored
update the beyond-beginner, document, events and ui-events page (#59)
1 parent 0182b6e commit 19684ec

4 files changed

Lines changed: 49 additions & 64 deletions

File tree

pages/beyond-beginner.md

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -415,16 +415,11 @@ The Range API allows you to manipulate portions of the document. Perfect for tex
415415
function highlight() {
416416
const range = document.createRange()
417417
const node = document.getElementById('content').firstChild
418-
range.setStart(node, 0)
419-
range.setEnd(node, 5)
418+
range.setStart(node, 0);range.setEnd(node, 5);
420419
const span = document.createElement('span')
421-
span.style.backgroundColor = 'yellow'
422-
range.surroundContents(span)
423-
}
424-
425-
function reset() {
426-
document.getElementById('content').innerHTML = 'Hello <strong>World</strong>!'
420+
span.style.backgroundColor = 'yellow';range.surroundContents(span);
427421
}
422+
function reset() { document.getElementById('content').innerHTML = 'Hello <strong>World</strong>!' }
428423
</script>
429424
```
430425

pages/document.md

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ clicksStart: 1
7676
<v-clicks>
7777

7878
```js {monaco-run} {autorun: false}
79-
const element = document.querySelector(`[data-slidev-no="256"]`)
79+
const element = document.querySelector(`[data-slidev-no="262"]`)
8080
const h1 = element.querySelector('h1');
8181
// console.log(h1.textContent)
8282

@@ -136,7 +136,7 @@ You can now traverse the DOM tree using the following based the relationships be
136136
- `nodeType`, `nodeName`, `nodeValue`
137137

138138
```js {monaco-run} {autorun: false}
139-
const element = document.querySelector(`[data-slidev-no="258"]`)
139+
const element = document.querySelector(`[data-slidev-no="264"]`)
140140

141141
// console.log(element.parentNode);console.log(element.parentElement)
142142
// console.log(element.childNodes.length);console.log(element.children.length)
@@ -169,7 +169,7 @@ clicksStart: 1
169169

170170
```js {monaco-run} {autorun: false}
171171
// TODO: Fix the bug on the querySelector
172-
const table = document.querySelector(`[data-slidev-no="259"]`)
172+
const table = document.querySelector(`[data-slidev-no="265"]`)
173173
// console.log(table);
174174
// console.log(table.rows); console.log(table.tBodies); console.log(table.tHead); console.log(table.tFoot);
175175
// console.log(table.rows[0]); console.log(table.rows[0].cells); console.log(table.rows[0].cells[0]); //tbody //tr //td //th
@@ -225,7 +225,7 @@ You can search for elements in the DOM tree using the following methods:
225225
- `matches`, `closest`
226226

227227
```js {monaco-run} {autorun: false}
228-
const element = document.querySelector(`[data-slidev-no="261"]`);
228+
const element = document.querySelector(`[data-slidev-no="267"]`);
229229
// add a id, class to the element
230230
// console.log(element.id); console.log(element.className); console.log(element.tagName); console.log(element.tagName.toLowerCase());console.log(element.name);
231231
```
@@ -244,15 +244,15 @@ clicksStart: 1
244244
Important classes to understand the node properties includes - `EventTarget`, `Node`, `Element`, `HTMLElement`-`HTMLBodyElement`-other interface discussed in the [HTML Note](https://karatu.oluwasetemi.dev/85), `Document`-`HTMLDocument`-`DocumentFragment`, `CharacterData`-`Text`-`Comment`.
245245

246246
```js {monaco-run} {autorun: false}
247-
const element = document.querySelector(`[data-slidev-no="262"]`);
247+
const element = document.querySelector(`[data-slidev-no="268"]`);
248248

249249
console.dir(element)
250250
```
251251

252252
## `innerHTML`, `outerHTML`, `textContent`, `innerText`, `nodeValue` or `data`, `hidden`
253253

254254
```js {monaco-run} {autorun: false}
255-
const element = document.querySelector(`[data-slidev-no="262"]`);
255+
const element = document.querySelector(`[data-slidev-no="268"]`);
256256

257257
// console.log(element.innerHTML); console.log(element.outerHTML); console.log(element.textContent); console.log(element.innerText); console.log(element.nodeValue); console.log(element.data); console.log(element.hidden);
258258
```
@@ -272,15 +272,15 @@ clicksStart: 1
272272
Attributes are the properties of the elements. They are the key-value pairs of the elements from the [convertion]{.text-xl.font-fast.text-red title='parsed or read'} of HTML to DOM. You can add your own properties to the element but the html attributes set is fixed according to the [specification](https://html.spec.whatwg.org/){.text-gradient}.
273273

274274
```js {monaco-run} {autorun: false}
275-
const element = document.querySelector(`[data-slidev-no="263"]`);
275+
const element = document.querySelector(`[data-slidev-no="269"]`);
276276

277277
// console.log(element.getAttribute('data-slidev-no')); console.log(element.getAttribute('data-slidev-no') === element.dataset.slidevNo); console.log(element.dataset.slidevNo);
278278
```
279279

280280
## `setAttribute`, `removeAttribute`, `hasAttribute`, `attributes`
281281

282282
```js {monaco-run} {autorun: false}
283-
const element = document.querySelector(`[data-slidev-no="263"]`);
283+
const element = document.querySelector(`[data-slidev-no="269"]`);
284284

285285
// element.setAttribute('data-slidev-no', '96'); console.log(element.dataset.slidevNo);
286286
// element.removeAttribute('data-slidev-no'); console.log(element.dataset.slidevNo);
@@ -364,7 +364,7 @@ strong.appendChild(textNode1);
364364
div.appendChild(strong);
365365
div.appendChild(textNode2);
366366

367-
const element = document.querySelector(`[data-slidev-no="265"] .default`);
367+
const element = document.querySelector(`[data-slidev-no="271"] .default`);
368368
element.prepend(div);
369369
```
370370

@@ -399,7 +399,7 @@ div.appendChild(textNode2);
399399
fragment.appendChild(div);
400400
// fragment.appendChild(div.cloneNode(true));
401401

402-
const element = document.querySelector(`[data-slidev-no="266"] .default`);
402+
const element = document.querySelector(`[data-slidev-no="272"] .default`);
403403
element.prepend(fragment);
404404
```
405405

@@ -421,7 +421,7 @@ You can modify the styles and classes of the elements using the following method
421421

422422
```js {monaco-run} {autorun: false}
423423

424-
const element = document.querySelector(`[data-slidev-no="267"]`);
424+
const element = document.querySelector(`[data-slidev-no="273"]`);
425425

426426
element.style.color = 'red'; element.style.backgroundColor = 'yellow'; element.style.padding = '15px'; element.style.border = '1px solid #d6e9c6'; element.style.borderRadius = '4px';
427427
element.style.setProperty('--uno', 'p-[15px] border border-[#d6e9c6] rounded-[4px] bg-[#dff0d8] text-[#3c763d]');
@@ -449,7 +449,7 @@ You can get the size and position of the elements using the following methods:
449449
- `scrollWidth`, `scrollHeight`, `scrollLeft`, `scrollTop`
450450

451451
```js {monaco-run} {autorun: false}
452-
const element = document.querySelector(`[data-slidev-no="268"] .view-lines`);
452+
const element = document.querySelector(`[data-slidev-no="274"] .view-lines`);
453453

454454
// console.log(element.offsetWidth); console.log(element.offsetHeight); console.log(element.offsetLeft); console.log(element.offsetTop); console.log(element.offsetParent);
455455
// console.log(element.clientWidth); console.log(element.clientHeight); console.log(element.clientLeft); console.log(element.clientTop);
@@ -552,7 +552,7 @@ Remember that element appended to a page using coordinates from another element
552552

553553
```js {monaco-run} {autorun: false}
554554
// FIX: use the deprecated pageXOffset and pageYOffset with the right values
555-
const element = document.querySelector(`[data-slidev-no="272"] h1`);
555+
const element = document.querySelector(`[data-slidev-no="278"] h1`);
556556
function getCoords(elem) { let box = elem.getBoundingClientRect(); return { top: box.top + window.pageYOffset, right: box.right + window.pageXOffset, bottom: box.bottom + window.pageYOffset, left: box.left + window.pageXOffset }; }
557557
// console.log(getCoords(element));
558558
```

pages/events.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ interface AddEventListenerOptions extends EventListenerOptions {
9292
```
9393

9494
```js {monaco-run} {autorun: false}
95-
const elem = document.querySelector(`[data-slidev-no="279"] h1`)
95+
const elem = document.querySelector(`[data-slidev-no="285"] h1`)
9696
const handler = () => console.log('Click!')
9797
elem.addEventListener('click', handler, { once: true })
9898
// elem.addEventListener('mouseover', handler);
@@ -115,7 +115,7 @@ When an event happens, the browser creates an event object, puts details into it
115115
<div grid="~ cols-2" gap="2">
116116

117117
```js {monaco-run} {autorun: false}
118-
const elem = document.querySelector(`[data-slidev-no="280"] h1`)
118+
const elem = document.querySelector(`[data-slidev-no="286"] h1`)
119119

120120
elem.addEventListener('click', function (event) {
121121
// show the event type, the element and the coordinates of the click
@@ -183,7 +183,7 @@ hideInToc: true
183183
<v-clicks>
184184

185185
```js {monaco-run} {autorun: false}
186-
const elem = document.querySelector(`[data-slidev-no="282"] h1`)
186+
const elem = document.querySelector(`[data-slidev-no="288"] h1`)
187187
const parent = elem.parentElement
188188
const grandParent = parent.parentElement
189189

@@ -344,15 +344,15 @@ To prevent the default action, we can use `event.preventDefault()`. returning `f
344344

345345
<!-- prettier-ignore -->
346346
```js {monaco-run} {lineNumbers: true, autorun: false}
347-
const link = document.querySelector(`[data-slidev-no="286"] a`)
347+
const link = document.querySelector(`[data-slidev-no="292"] a`)
348348
link.addEventListener('click', function(event) {
349349
event.preventDefault(); event.stopPropagation();
350350
console.log('Link click!');
351351
});
352352
```
353353

354354
```js {monaco-run} {autorun: false}
355-
const h1 = document.querySelector(`[data-slidev-no="286"] h1`)
355+
const h1 = document.querySelector(`[data-slidev-no="292"] h1`)
356356
h1.oncontextmenu = function (event) {
357357
console.log('Content menu clicked')
358358
}

pages/ui-events.md

Lines changed: 28 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -237,57 +237,47 @@ url: https://www.toptal.com/developers/keycode
237237
---
238238
hideInToc: true
239239
layout: full
240-
dragPos:
241-
square: Left,Top,Width,Height,Rotate
242240
---
243241

244242
# Drag'n'Drop with mouse events
245243

246244
<v-clicks>
247245

248-
Drag-and-drop is a user interface interaction that allows users to grab an object and move it to a different location on the screen. This interaction is common in file management on your computer, arranging items in games, or editing tools online. Double click the JS logo and drag.The basic Drag’n’Drop algorithm looks like this:
246+
Drag-and-drop is a user interface interaction that allows users to grab an object and move it to a different location on the screen. The basic algorithm: (1) `mousedown` - prepare element, (2) `mousemove` - move element, (3) `mouseup` - drop and cleanup.
249247

250-
<div class="overflow-auto h-full pb40 grid grid-cols-[70%_30%] gap-10">
248+
<div class="overflow-auto h-80">
251249

252-
```js
253-
ball.onmousedown = function (event) {
254-
// (1) prepare to moving: make absolute and on top by z-index
255-
ball.style.position = 'absolute'
256-
ball.style.zIndex = 1000
257-
258-
// move it out of any current parents directly into body
259-
// to make it positioned relative to the body
260-
document.body.append(ball)
261-
262-
// centers the ball at (pageX, pageY) coordinates
263-
function moveAt(pageX, pageY) {
264-
ball.style.left = pageX - ball.offsetWidth / 2 + 'px'
265-
ball.style.top = pageY - ball.offsetHeight / 2 + 'px'
266-
}
267-
268-
// move our absolutely positioned ball under the pointer
269-
moveAt(event.pageX, event.pageY)
270-
271-
function onMouseMove(event) {
272-
moveAt(event.pageX, event.pageY)
273-
}
274-
275-
// (2) move the ball on mousemove
276-
document.addEventListener('mousemove', onMouseMove)
277-
278-
// (3) drop the ball, remove unneeded handlers
279-
ball.onmouseup = function () {
280-
document.removeEventListener('mousemove', onMouseMove)
281-
ball.onmouseup = null
250+
```html {monaco-run} {autorun: false}
251+
<div id="container" style="position:relative; height:150px; border:2px dashed #ccc; background:#f9f9f9;">
252+
<div id="ball" style="position:absolute; width:50px; height:50px; background:#4CAF50; border-radius:50%; cursor:grab; left:20px; top:20px; display:flex; align-items:center; justify-content:center; color:white; font-size:12px;">Drag</div>
253+
</div>
254+
<script>
255+
const ball = document.getElementById('ball')
256+
ball.onmousedown = function(event) {
257+
event.preventDefault() // prevent text selection
258+
ball.style.cursor = 'grabbing'
259+
const shiftX = event.clientX - ball.getBoundingClientRect().left
260+
const shiftY = event.clientY - ball.getBoundingClientRect().top
261+
function moveAt(pageX, pageY) {
262+
ball.style.left = pageX - shiftX + 'px'
263+
ball.style.top = pageY - shiftY + 'px'
264+
}
265+
function onMouseMove(event) { moveAt(event.pageX, event.pageY) }
266+
function onMouseUp() {
267+
document.removeEventListener('mousemove', onMouseMove)
268+
document.removeEventListener('mouseup', onMouseUp)
269+
ball.style.cursor = 'grab'
270+
}
271+
document.addEventListener('mousemove', onMouseMove)
272+
document.addEventListener('mouseup', onMouseUp)
282273
}
283-
}
274+
ball.ondragstart = () => false // disable native drag
275+
</script>
284276
```
285277

286-
<div class="grid place-items-center">
287-
<img v-drag="square" class="i-logos-javascript text-7xl "></img>
288278
</div>
289279

290-
</div>
280+
**Key points:** Use `mousedown` to start, `mousemove` on document to track, `mouseup` to stop. Disable native drag with `ondragstart = () => false`.
291281

292282
</v-clicks>
293283

0 commit comments

Comments
 (0)