2021-01-31 11:05:37 +01:00
|
|
|
|
# Events & Animation
|
|
|
|
|
|
|
|
|
|
## Events
|
|
|
|
|
|
|
|
|
|
Event Types:
|
|
|
|
|
|
|
|
|
|
- **Mouse Events**: `mousedown`, `mouseup`, `click`, `dbclick`, `mousemove`, `mouseover`, `mousewheel`, `mouseout`, `contextmenu`, ...
|
|
|
|
|
- **Touch Events**: `touchstart`, `touchmove`, `touchend`, `touchcancel`, ...
|
|
|
|
|
- **Keyboard Events**: `keydown`, `keypress`, `keyup`, ...
|
|
|
|
|
- **Form Events**: `focus`, `blur`, `change`, `submit`, ...
|
|
|
|
|
- **Window Events**: `scroll`, `resize`, `hashchange`, `load`, `unload`, ...
|
|
|
|
|
|
|
|
|
|
### Managing Event Listeners
|
|
|
|
|
|
|
|
|
|
```js
|
|
|
|
|
var domNode = document.getElementById("id");
|
|
|
|
|
|
|
|
|
|
var onEvent = function(event) { // parameter contains info on the triggered event
|
2021-03-29 19:34:47 +02:00
|
|
|
|
event.preventDefault(); // block execution of default action
|
2021-01-31 11:05:37 +01:00
|
|
|
|
// logic here
|
|
|
|
|
}
|
|
|
|
|
|
2021-09-20 19:35:32 +02:00
|
|
|
|
domNode.addEventListener(eventType, callback);
|
|
|
|
|
domNode.removeEventListener(eventType, callback);
|
2021-01-31 11:05:37 +01:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
### Bubbling & Capturing
|
|
|
|
|
|
|
|
|
|
Events in Javascript propagate through the DOM tree.
|
|
|
|
|
|
|
|
|
|
[Bubbling and Capturing](https://javascript.info/bubbling-and-capturing)
|
|
|
|
|
[What Is Event Bubbling in JavaScript? Event Propagation Explained](https://www.sitepoint.com/event-bubbling-javascript/)
|
|
|
|
|
|
|
|
|
|
### Dispatching Custom Events
|
|
|
|
|
|
|
|
|
|
Event Options:
|
|
|
|
|
|
|
|
|
|
- `bubbles` (bool): whether the event propagates through bubbling
|
|
|
|
|
- `cancellable` (bool): if `true` the "default action" may be prevented
|
|
|
|
|
|
|
|
|
|
```js
|
|
|
|
|
let event = new Event(type [,options]); // create the event, type can be custom
|
2021-03-29 19:34:47 +02:00
|
|
|
|
let event = new CustomEvent(type, { detail: /* custom data */ }); // create event w/ custom data
|
2021-01-31 11:05:37 +01:00
|
|
|
|
domNode.dispatchEvent(event); // launch the event
|
|
|
|
|
```
|
|
|
|
|
|
2023-03-13 11:33:51 +00:00
|
|
|
|

|
2021-03-29 19:34:47 +02:00
|
|
|
|
|
2021-01-31 11:05:37 +01:00
|
|
|
|
## Animation
|
|
|
|
|
|
|
|
|
|
The window object is the assumed global object on a page.
|
|
|
|
|
|
|
|
|
|
Animation in JavascriptThe standard way to animate in JS is to use window methods.
|
|
|
|
|
It's possible to animate CSS styles to change size, transparency, position, color, etc.
|
|
|
|
|
|
|
|
|
|
```js
|
|
|
|
|
//Calls a function once after a delay
|
|
|
|
|
window.setTimeout(callbackFunction, delayMilliseconds);
|
|
|
|
|
|
|
|
|
|
//Calls a function repeatedly, with specified interval between each call
|
|
|
|
|
window.setInterval(callbackFunction, delayMilliseconds);
|
|
|
|
|
|
|
|
|
|
//To stop an animation store the timer into a variable and clear it
|
|
|
|
|
window.clearTimeout(timer);
|
|
|
|
|
window.clearInterval(timer);
|
2021-07-12 16:18:53 +02:00
|
|
|
|
|
|
|
|
|
// execute a callback at each frame
|
|
|
|
|
window.requestAnimationFrame(callbackFunction);
|
2021-01-31 11:05:37 +01:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
### Element Position & dimensions
|
|
|
|
|
|
|
|
|
|
[StackOverflow](https://stackoverflow.com/a/294273/8319610)
|
|
|
|
|
[Wrong dimensions at runtime](https://stackoverflow.com/a/46772849/8319610)
|
|
|
|
|
|
|
|
|
|
```js
|
|
|
|
|
> console.log(document.getElementById('id').getBoundingClientRect());
|
|
|
|
|
DOMRect {
|
|
|
|
|
bottom: 177,
|
|
|
|
|
height: 54.7,
|
|
|
|
|
left: 278.5,
|
|
|
|
|
right: 909.5,
|
|
|
|
|
top: 122.3,
|
|
|
|
|
width: 631,
|
|
|
|
|
x: 278.5,
|
|
|
|
|
y: 122.3,
|
|
|
|
|
}
|
|
|
|
|
```
|