Показать сообщение отдельно
  #3 (permalink)  
Старый 15.05.2021, 20:43
Аватар для рони
Профессор
Отправить личное сообщение для рони Посмотреть профиль Найти все сообщения от рони
 
Регистрация: 27.05.2010
Сообщений: 33,068

календарь по годам
Блондинка,
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title></title>
    <style>
        .content {
            display: flex;
            flex-wrap: wrap;
            justify-content: space-between;
        }
        .content h3 {
            width: 100%;
            text-align: center;
        }
        .day {
            border: 1px solid #ccc;
            width: 30px;
            line-height: 30px;
            text-align: center;
            margin: 1px;
            background-color: hsl(210, 100%, 90%);
            color: hsl(210, 100%, 50%);
        }
        .month {
            height: 220px;
            margin: 5px;
            border: 1px solid #ccc;
            border-radius: 4px;
        }
        .month tr:nth-child(n + 7) td.day {
            background-color: hsl(348, 100%, 90%);
            color: hsl(348, 100%, 50%);
        }
        .month tr:nth-child(1) th {
            text-align: center;
        }
    </style>
</head>
<body>
    <button class="btn year" data-up='1'>+1</button>
    <button class="btn year" data-up='-1'>-1</button>
    <div class="content"></div>
    <script>
        var calendar = {
            update: function(year, month) {
                this.year = year;
                this.month = ['Январь', 'Февраль', 'Март', 'Апрель', 'Май', 'Июнь', 'Июль', 'Август', 'Сентябрь', 'Октябрь', 'Ноябрь', 'Декабрь'][month];
                var startDay = new Date(year, month, 1);
                var offsetDay = (startDay.getDay() || 7) - 1;
                startDay.setDate(startDay.getDate() - offsetDay);
                var lastDay = new Date(startDay);
                var days = (new Date(year, month + 1, 0)).getDate();
                days = Math.ceil((days + offsetDay) / 7) * 7;
                lastDay.setDate(lastDay.getDate() + days);
                this.data = ['Пн', 'Вт', 'Ср', 'Чт', 'Пт', 'Сб', 'Вс'];
                while (startDay < lastDay) {
                    this.data.push(startDay.getDate());
                    startDay.setHours(24);
                }
            },
            render: function() {
                var html = ['<tr>', '<tr>', '<tr>', '<tr>', '<tr>', '<tr>', '<tr>'];
                for (var i = 0; i < this.data.length; i++) {
                    html[i % 7] += '<td class="day">' + this.data[i];
                }
                var title = '<tr><th colspan=7>' + this.month;
                this.element.innerHTML = title + html.join('');
            }
        };
        function createYearCalendar(year, parent) {
            parent.innerHTML = `<h3>${year}</h3>`;
            for (var i = 0; i < 12; i++) {
                var table = document.createElement('table');
                table.className = 'month';
                calendar.element = table;
                calendar.update(thisYear, i);
                calendar.render();
                parent.append(table)
            }
        }
        var thisYear = 2021;
        var parent = document.querySelector('.content')
        createYearCalendar(thisYear, parent);
        document.addEventListener('click', function(event) {
            var target = event.target;
            if (target = target.closest('.btn.year')) {
                event.preventDefault();
                thisYear += +target.dataset.up;
                createYearCalendar(thisYear, parent);
            }
        });
    </script>
</body>
</html>
Ответить с цитированием