Показать сообщение отдельно
  #4 (permalink)  
Старый 17.11.2020, 19:44
Аватар для рони
Профессор
Отправить личное сообщение для рони Посмотреть профиль Найти все сообщения от рони
 
Регистрация: 27.05.2010
Сообщений: 33,126

собери радугу
Сообщение от s4meone
нужно в левом ряду выставить цвета радуги.
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Rainbow</title>
    <style>
        .content-cell{
            width: 10em;
            height: 5em;
            border: 1px solid #000;
        }
        .content-cell div{
            width: 10em;
            height: 5em;
        }
    </style>
</head>
<body>
    <table>
        <tbody>
            <tr>
                <td class="content-cell" ondrop="drop(event)" ondragover="allowDrop(event)">
                    <div id="1" draggable="true" ondragstart="drag(event)"></div>
                </td>
                <td class="content-cell" ondrop="drop(event)" ondragover="allowDrop(event)">
                    <div id="2" draggable="true" ondragstart="drag(event)"></div>
                </td>
            </tr>
            <tr>
                <td class="content-cell" ondrop="drop(event)" ondragover="allowDrop(event)">
                    <div id="3" draggable="true" ondragstart="drag(event)"></div>
                </td>
                <td class="content-cell" ondrop="drop(event)" ondragover="allowDrop(event)">
                    <div id="4" draggable="true" ondragstart="drag(event)"></div>
                </td>
            </tr>
            <tr>
                <td class="content-cell" ondrop="drop(event)" ondragover="allowDrop(event)">
                    <div id="5" draggable="true" ondragstart="drag(event)"></div>
                </td>
                <td class="content-cell" ondrop="drop(event)" ondragover="allowDrop(event)">
                    <div id="6" draggable="true" ondragstart="drag(event)"></div>
                </td>
            </tr>
            <tr>
                <td class="content-cell" ondrop="drop(event)" ondragover="allowDrop(event)">
                    <div id="7" draggable="true" ondragstart="drag(event)"></div>
                </td>
                <td class="content-cell" ondrop="drop(event)" ondragover="allowDrop(event)">
                    <div id="8" draggable="true" ondragstart="drag(event)"></div>
                </td>
            </tr>
            <tr>
                <td class="content-cell" ondrop="drop(event)" ondragover="allowDrop(event)">
                    <div id="9" draggable="true" ondragstart="drag(event)"></div>
                </td>
                <td class="content-cell" ondrop="drop(event)" ondragover="allowDrop(event)">
                    <div id="10" draggable="true" ondragstart="drag(event)"></div>
                </td>
            </tr>
            <tr>
                <td class="content-cell" ondrop="drop(event)" ondragover="allowDrop(event)">
                    <div id="11" draggable="true" ondragstart="drag(event)"></div>
                </td>
                <td class="content-cell" ondrop="drop(event)" ondragover="allowDrop(event)">
                    <div id="12" draggable="true" ondragstart="drag(event)"></div>
                </td>
            </tr>
            <tr>
                <td class="content-cell" ondrop="drop(event)" ondragover="allowDrop(event)">
                    <div id="13" draggable="true" ondragstart="drag(event)"></div>
                </td>
                <td class="content-cell" ondrop="drop(event)" ondragover="allowDrop(event)">
                    <div id="14" draggable="true" ondragstart="drag(event)"></div>
                </td>
            </tr>
        </tbody>
    </table>
    <button onclick="check()" type="button">чек чек чек</button>
    <button onclick="init()" type="button">new</button>
<script>
function allowDrop(ev) {
    ev.preventDefault();
}
function drag(ev) {
    ev.dataTransfer.setData("text", ev.target.id);
}
function drop(ev) {
    ev.preventDefault();
    var id = ev.dataTransfer.getData("text");
    var div = document.getElementById(id);
    var color = div.dataset.color || "";
    var targetColor = ev.target.dataset.color || "";
    div.style.backgroundColor = targetColor;
    div.dataset.color = targetColor;
    ev.target.style.backgroundColor = color;
    ev.target.dataset.color = color;
}
var colors = ["red", "orange", "yellow", "green", "aqua", "blue", "purple"];
var divids = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14];
function init() {
    divids.forEach(function(id) {
        var div = document.getElementById(id);
        div.style.backgroundColor = "";
        div.dataset.color = "";
    });
    shuffle(divids);
    colors.forEach(function(color, i) {
        var id = divids[i];
        var div = document.getElementById(id);
        div.style.backgroundColor = color;
        div.dataset.color = color;
    });
}
function shuffle(array) {
    array.sort(function() {
        return Math.random() - 0.5;
    });
}
function check() {
    var ok = colors.every(function(color, i) {
        return document.getElementById(i * 2 + 1).dataset.color == color;
    }) ? "Верно!" : "Неверно!";
    alert(ok);
}
init();
</script>
</body>
</html>
Ответить с цитированием