Перейти к содержимому
Color:
Blue
Red
Green
Yellow
Black
Shape:
Circle
Square
Pill
Rectangle
Border:
Background:
const colorButtons = document.getElementsByClassName("color-button"); const shapeButtons = document.getElementsByClassName("shape-button"); const borderCheckbox = document.getElementById("border-checkbox"); const backgroundCheckbox = document.getElementById("background-checkbox"); const preview = document.getElementById("preview"); let currentColor = "blue"; let activeButton; for (let i = 0; i < colorButtons.length; i++) { colorButtons[i].addEventListener("click", function() { if (activeButton) { activeButton.classList.remove('active'); } this.classList.add('active'); activeButton = this; currentColor = this.innerHTML.toLowerCase(); preview.style.backgroundColor = currentColor; }); } for (let i = 0; i < shapeButtons.length; i++) { shapeButtons[i].addEventListener("click", function() { if (activeButton) { activeButton.classList.remove('active'); } this.classList.add('active'); activeButton = this; currentColor = this.innerHTML.toLowerCase(); preview.className = "shape " + this.innerHTML.toLowerCase(); }); } borderCheckbox.addEventListener("change", function() { if (this.checked) { preview.style.border = "1px solid white"; preview.style.outline = "2px solid white"; } else { preview.style.visibility = "visible"; preview.style.border = "none"; preview.style.outline = "none"; } }); backgroundCheckbox.addEventListener("change", function() { if (this.checked) { preview.style.visibility = "hidden"; preview.style.border = "1px solid black"; } else { preview.style.visibility = "visible"; preview.style.border = "none"; } }); let activeShapeButton; for (let i = 0; i < shapeButtons.length; i++) { shapeButtons[i].addEventListener("click", function() { if (activeShapeButton) { activeShapeButton.classList.remove('active'); } this.classList.add('active'); activeShapeButton = this; preview.className = "shape " + this.innerHTML.toLowerCase(); }); }
body { background-color: rgb(164, 164, 164); } .shape { width: 100px; height: 100px; margin: 10px; } .circle { border-radius: 50%; } .square { border-radius: 0; } .pill { border-radius: 50px; width: 150px; } .rectangle { border-radius: 0; width: 150px; } .color-button { position: relative; background-color: white; color: black; padding: 10px 20px; border: none; cursor: pointer; } .color-button:after { content: ''; position: absolute; top: 50%; left: 10px; width: 10px; height: 10px; border-radius: 50%; background-color: white; } .color-button.active { background-color: rgb(94, 94, 255); color: white; } .color-button.active:hover { background-color: rgb(94, 94, 255); color: white; } .color-button.active:hover:after { background-color: darkblue; } .shape-button { position: relative; background-color: white; color: black; padding: 10px 20px; border: none; cursor: pointer; } .shape-button:after { content: ''; position: absolute; top: 50%; left: 10px; width: 10px; height: 10px; border-radius: 50%; background-color: white; } .shape-button.active { background-color: rgb(94, 94, 255); color: white; } .shape-button.active:hover { background-color: rgb(94, 94, 255); color: white; } .shape-button.active:hover:after { background-color: darkblue; }
Results:
Color:
Blue
Red
Green
Yellow
Black
Shape:
Circle
Square
Pill
Rectangle
Border:
Background: