forked from jobtrek/dev-24-javascript-exercise-ex-js-empty
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmovements.js
126 lines (111 loc) · 4.18 KB
/
movements.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
/**
* Register a new event listener that will retrieve the position of the mouse on the screen
* and display the coordinates on the p with id "mouse-coordinates".
* You need to display coordinates as follows : "x: 232, y: 332
*/
export function mouseMovements() {
// Write your code here
const mouse = document.getElementById('mouse-coordinates');
document.addEventListener('mousemove', (event) => {
const x = event.clientX;
const y = event.clientY;
mouse.textContent = `x: ${x}, y: ${y}`;
});
}
const randomRGB = () => {
const o = Math.round
const r = Math.random
const s = 255
return `rgba(${o(r() * s)},${o(r() * s)},${o(r() * s)})`
}
let enteringColor = ''
/**
* On the page, you have an input with the id "focus-me".
* You need to add three behaviors to this input.
* First, when you hover it, you have to display a message in his label with
* the text "Yes, you hover me !". and remove the message when the hover is loosed.
* Second, when you focus the input, you have to change hist border color to a random one,
* but different from all the previously used and different from the original one.
* Third, when you loose focus of the field, you need to reset the border color to the default one.
*/
export function hoverFocusAndBlur() {
// Write your code here
const input = document.querySelector('#focus-me')
let alllabels = []
input.addEventListener("mouseover", (event) => {
const labelElement = event.target.labels
for ( const element of labelElement ) {
alllabels.push(element.textContent)
element.textContent = " Yes, you hover me !";
}
})
input.addEventListener("mouseout", (event) => {
const labelElement = event.target.labels
for ( const element of labelElement ) {
element.textContent = alllabels.splice(0,1);
}
})
const originalColor = getComputedStyle(input).borderColor
const usedColors = [originalColor]
input.addEventListener('focus', function () {
this.style.borderColor = getUniqueColor()
})
input.addEventListener('blur', function () {
this.style.borderColor = originalColor
})
function getRandomColor() {
const letters = '0123456789ABCDEF'
let color = '#'
for (let i = 0; i < 6; i++) {
color += letters[Math.floor(Math.random() * 16)]
}
return color
}
function getUniqueColor() {
let newColor
do {
newColor = getRandomColor()
} while (usedColors.includes(newColor))
usedColors.push(newColor)
return newColor
}
}
/**
* On the same input from the previous exercise, you need to add a new behavior :
* Now, each new letter on the input should randomly change the default color of the input border.
* You don't need to change the current border color, which is controlled by the previous exercise,
* but you need to change the original color, the one that will be applied when the
* precedent exercise will lose focus of the field.
* Take the opportunity to also apply this colour to the text of the 2 input labels.
*/
export function changesOnInputEvents() {
// Write your code here
const input = document.getElementById('focus-me');
const labels = document.querySelectorAll('label[for="focus-me"]');
let originalColor = getComputedStyle(input).borderColor;
function getRandomColor() {
const letters = '0123456789ABCDEF';
let color = '#';
for (let i = 0; i < 6; i++) {
color += letters[Math.floor(Math.random() * 16)];
}
return color;
}
input.addEventListener('input', () => {
const newColor = getRandomColor();
originalColor = newColor;
labels.forEach(label => {
label.style.color = newColor;
});
if (document.activeElement !== input) {
input.style.borderColor = newColor;
}
});
const existingBlurListener = input.onblur;
input.onblur = function (event) {
if (existingBlurListener) {
existingBlurListener.call(this, event);
}
this.style.borderColor = originalColor;
};
}