Compare commits
32 Commits
Author | SHA1 | Date | |
---|---|---|---|
|
42cd4def25 | ||
|
43488eeaea | ||
|
0336132c61 | ||
|
4f58ad1fe5 | ||
|
eb209062e2 | ||
|
71e29f11e0 | ||
|
4764a09973 | ||
|
309de303d0 | ||
|
50ad6cc1c5 | ||
|
2ac097d1ac | ||
|
ba764541e2 | ||
|
f6a2117f78 | ||
|
c6c2d8603f | ||
|
030b3dad90 | ||
|
a66d9832f6 | ||
|
725a770a94 | ||
|
69ab2f12d0 | ||
|
7098962ce3 | ||
|
12ccebe93c | ||
|
a0c30c23ad | ||
|
e1e7877f94 | ||
|
34474839ec | ||
|
25674b5ce6 | ||
|
36ebf6f0b4 | ||
|
8cfc31392b | ||
|
480f94030e | ||
|
a5bc7aa76c | ||
|
d489b8e893 | ||
|
eec0a7ed3d | ||
|
549e267eea | ||
|
a68eb6a985 | ||
|
a3fcfe763f |
103
index.js
103
index.js
@ -1,23 +1,80 @@
|
||||
document.addEventListener("DOMContentLoaded",()=>{
|
||||
document.getElementById("controls").addEventListener("click",(clicked)=>{
|
||||
var lightPrefix = clicked.target.id.replace("Button","");
|
||||
var targetLight = document.getElementById(lightPrefix+"Light");
|
||||
if (!targetLight.classList.contains(lightPrefix)) {
|
||||
targetLight.classList.add(lightPrefix);
|
||||
console.log(lightPrefix+" bulb on.")
|
||||
} else {
|
||||
targetLight.classList.remove(lightPrefix);
|
||||
console.log(lightPrefix+" bulb off.")
|
||||
}
|
||||
})
|
||||
|
||||
var buttons = document.getElementsByClassName("button");
|
||||
for (var i=0;i<buttons.length;i++) {
|
||||
buttons[i].addEventListener("mouseenter",(e)=>{
|
||||
console.log("Entered "+e.target.innerHTML+" button.")
|
||||
})
|
||||
buttons[i].addEventListener("mouseleave",(e)=>{
|
||||
console.log("Left "+e.target.innerHTML+" button.")
|
||||
})
|
||||
}
|
||||
})
|
||||
(function() {
|
||||
'use strict';
|
||||
|
||||
const stopButton = document.querySelector('#stopButton');
|
||||
const slowButton = document.querySelector('#slowButton');
|
||||
const goButton = document.querySelector('#goButton');
|
||||
|
||||
const stopLight = document.querySelector('#stopLight');
|
||||
const slowLight = document.querySelector('#slowLight');
|
||||
const goLight = document.querySelector('#goLight');
|
||||
|
||||
// part 1
|
||||
stopButton.addEventListener('click', () => {
|
||||
stopLight.classList.toggle('stop');
|
||||
|
||||
// or...
|
||||
// if (stopLight.classList.contains('stop')) {
|
||||
// stopLight.classList.remove('stop');
|
||||
// }
|
||||
// else {
|
||||
// stopLight.classList.add('stop');
|
||||
// }
|
||||
});
|
||||
|
||||
slowButton.addEventListener('click', () => {
|
||||
slowLight.classList.toggle('slow');
|
||||
});
|
||||
|
||||
goButton.addEventListener('click', () => {
|
||||
goLight.classList.toggle('go');
|
||||
});
|
||||
|
||||
// part 2
|
||||
const handleMouseEnter = (event) => {
|
||||
console.log(`Entered ${event.target.textContent} button`);
|
||||
};
|
||||
|
||||
const handleMouseLeave = (event) => {
|
||||
console.log(`Left ${event.target.textContent} button`);
|
||||
};
|
||||
|
||||
stopButton.addEventListener('mouseenter', handleMouseEnter);
|
||||
slowButton.addEventListener('mouseenter', handleMouseEnter);
|
||||
goButton.addEventListener('mouseenter', handleMouseEnter);
|
||||
|
||||
stopButton.addEventListener('mouseleave', handleMouseLeave);
|
||||
slowButton.addEventListener('mouseleave', handleMouseLeave);
|
||||
goButton.addEventListener('mouseleave', handleMouseLeave);
|
||||
|
||||
// bonus
|
||||
const controls = document.querySelector('#controls');
|
||||
|
||||
controls.addEventListener('click', (event) => {
|
||||
if (event.target === controls) {
|
||||
return;
|
||||
}
|
||||
|
||||
let status;
|
||||
|
||||
if (event.target === stopButton) {
|
||||
status = stopLight.classList.contains('stop') ? 'on' : 'off';
|
||||
|
||||
// or...
|
||||
// if (stopLight.classList.contains('stop')) {
|
||||
// status = 'on';
|
||||
// }
|
||||
// else {
|
||||
// status = 'off';
|
||||
// }
|
||||
}
|
||||
else if (event.target === slowButton) {
|
||||
status = slowLight.classList.contains('slow') ? 'on' : 'off';
|
||||
}
|
||||
else {
|
||||
status = goLight.classList.contains('go') ? 'on' : 'off';
|
||||
}
|
||||
|
||||
console.log(`${event.target.textContent} bulb ${status}`);
|
||||
});
|
||||
})();
|
||||
|
Loading…
x
Reference in New Issue
Block a user