This commit is contained in:
@@ -0,0 +1,4 @@
|
||||
.light-bulbs{
|
||||
padding: 1rem;
|
||||
border: 2px solid #555;
|
||||
}
|
||||
Binary file not shown.
|
After Width: | Height: | Size: 8.0 KiB |
Binary file not shown.
|
After Width: | Height: | Size: 9.3 KiB |
@@ -0,0 +1,92 @@
|
||||
// Helper DOM Selektion mit $ & $$
|
||||
const $ = (qs) => document.querySelector(qs);
|
||||
const $$ = (qs) => Array.from(document.querySelectorAll(qs));
|
||||
|
||||
Node.prototype.on = function(name, fn) {
|
||||
this.addEventListener(name, fn);
|
||||
return this;
|
||||
};
|
||||
|
||||
// // anstatt
|
||||
// document.querySelector('button').addEventListener('click', (event) => { });
|
||||
// // wird zu:
|
||||
// document.querySelector('button').on('click', (event) => { });
|
||||
|
||||
Array.prototype.on = Array.prototype.addEventListener = function(name, fn) {
|
||||
this.forEach((elem) => elem.on(name, fn));
|
||||
return this;
|
||||
};
|
||||
|
||||
// anstatt
|
||||
// Array.from(document.querySelectorAll('.menu-main li a')).forEach((elem) =>
|
||||
// elem.addEventListener('click', (event) => {
|
||||
// console.log(event);
|
||||
// })
|
||||
// );
|
||||
// // wird zu
|
||||
// Array.from(document.querySelectorAll('.menu-main li a')).on(
|
||||
// 'click',
|
||||
// (event) => {
|
||||
// console.log(event);
|
||||
// }
|
||||
// );
|
||||
|
||||
|
||||
|
||||
// helper
|
||||
const $on = (el, ev, fn) => {
|
||||
Array.isArray(el) ? el.forEach(ae => $on(ae, ev, fn)) :
|
||||
el.addEventListener(ev, fn);
|
||||
return el;
|
||||
};
|
||||
|
||||
// times - funktion
|
||||
function times(n, fn) {
|
||||
const result = Array(n);
|
||||
for (let i = 0; i < n; i++) {
|
||||
result[i] = fn(i);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
// oder von lodash
|
||||
function baseTimes(n, iteratee) {
|
||||
var index = -1,
|
||||
result = Array(n);
|
||||
|
||||
while (++index < n) {
|
||||
result[index] = iteratee(index);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
/*
|
||||
* range(zahl bis wohin array befüllt werden soll) : array
|
||||
* - array von 0 bis
|
||||
parameterzahl (nicht inklusive)
|
||||
*
|
||||
* range(anfangswert, endwert) : array
|
||||
* - array von anfangswert bis endwert (nicht inklusive)
|
||||
*
|
||||
* range(nfangswert, endwert, übersprungswert) : : array
|
||||
* - array von anfangswert bis endwert (nicht inklusive), zwischenwerte durch übersprungswert entfallen
|
||||
*/
|
||||
function rangeFromStartToEnd(start, end, step = 1) {
|
||||
const length = Math.max(Math.ceil((end - start) / step), 0);
|
||||
const result = new Array(length);
|
||||
const sign = step / Math.abs(step);
|
||||
|
||||
let index = 0;
|
||||
for (let value = start; value * sign < end * sign; value += step) {
|
||||
result[index++] = value;
|
||||
}
|
||||
return result;
|
||||
};
|
||||
|
||||
|
||||
function range(startOrEnd, end, step) {
|
||||
if (end) {
|
||||
return rangeFromStartToEnd(startOrEnd, end, step);
|
||||
} else {
|
||||
return rangeFromStartToEnd(0, startOrEnd);
|
||||
}
|
||||
};
|
||||
File diff suppressed because one or more lines are too long
@@ -0,0 +1,47 @@
|
||||
'use strict';
|
||||
|
||||
(() => {
|
||||
// === DOM & VARS =======
|
||||
const DOM = {
|
||||
lightGroups: Array.from(document.querySelectorAll('.light-bulbs')),
|
||||
};
|
||||
|
||||
console.log(DOM);
|
||||
|
||||
const LIGHT_PATH_ON = 'assets/img/light_on.png';
|
||||
const LIGHT_PATH_OFF = 'assets/img/light_off.png';
|
||||
|
||||
// === INIT =============
|
||||
const init = () => {
|
||||
DOM.lightGroups.forEach(initRunningLight);
|
||||
};
|
||||
|
||||
// === EVENTHANDLER =====
|
||||
|
||||
// === XHR/FETCH ========
|
||||
|
||||
// === FUNCTIONS ========
|
||||
const initRunningLight = (group) => {
|
||||
const bulbs = Array.from(group.querySelectorAll('img'));
|
||||
console.log(bulbs);
|
||||
const eventType = group.dataset.event; //=> 'mouseenter' | 'click'
|
||||
let activeIndex = 0;
|
||||
|
||||
// Jede Reihe startet mit genau einer leuchtenden Birne.
|
||||
bulbs.forEach((bulb, index) => {
|
||||
bulb.src = index === activeIndex ? LIGHT_PATH_ON : LIGHT_PATH_OFF;
|
||||
|
||||
bulb.addEventListener(eventType, () => {
|
||||
// Beim Jagen reagiert nur die gerade leuchtende Birne.
|
||||
if (eventType === 'mouseenter' && index !== activeIndex) return;
|
||||
|
||||
bulbs[activeIndex].src = LIGHT_PATH_OFF;
|
||||
// Modulo sorgt nach der letzten Birne für den Sprung zum Anfang.
|
||||
activeIndex = (activeIndex + 1) % bulbs.length;
|
||||
bulbs[activeIndex].src = LIGHT_PATH_ON;
|
||||
});
|
||||
});
|
||||
};
|
||||
|
||||
init();
|
||||
})();
|
||||
Reference in New Issue
Block a user