Zagadnienie 2: Zmiana stylu (kolor + rozmiar)

Suwaki RGB zmieniają kolor tekstu, pole number zmienia rozmiar czcionki – na żywo.

Wersja A – najprostsza (egzaminowa)

Egzamin



function zmienKolorA() {
    let r = document.getElementById("rA").value;
    let g = document.getElementById("gA").value;
    let b = document.getElementById("bA").value;
    document.getElementById("tekstA").style.color = "rgb(" + r + "," + g + "," + b + ")";
}

function zmienRozmiarA() {
    let roz = document.getElementById("rozA").value;
    document.getElementById("tekstA").style.fontSize = roz + "px";
}

Wersja B – addEventListener

Egzamin



const tekstB = document.getElementById("tekstB");
const rB = document.getElementById("rB");
const gB = document.getElementById("gB");
const bB = document.getElementById("bB");
const rozB = document.getElementById("rozB");

function aktualizujKolorB() {
    tekstB.style.color = "rgb(" + rB.value + "," + gB.value + "," + bB.value + ")";
}

rB.addEventListener("input", aktualizujKolorB);
gB.addEventListener("input", aktualizujKolorB);
bB.addEventListener("input", aktualizujKolorB);

rozB.addEventListener("input", function() {
    tekstB.style.fontSize = rozB.value + "px";
});

Wersja C – kompaktowa

Egzamin



const tC = document.getElementById("tekstC");

const kolorC = () => {
    tC.style.color = `rgb(${rC.value},${gC.value},${bC.value})`;
};

rC.oninput = gC.oninput = bC.oninput = kolorC;

rozC.oninput = function() {
    tC.style.fontSize = this.value + "px";
};