Доработать и исправить скрипт
Как изменить высоту двух элементов чтобы общая сумма значений высоты оставалась неизменной?
<!DOCTYPE html> <html lang="ru"> <head> <meta charset="utf-8"> <title></title> <style> body { width: 760px; } body, select, input { font: 14px serif; } #calendar { width: 330px; height: 375px; display: inline-block; border: 1px solid #a9a9a9; border-radius: 6px/4px; padding: 5px; } #navigation_panel { background: hsl(207, 100%, 92%); min-height: 34px; max-height: 78px; border: 1px solid #a9a9a9; border-radius: 6px/4px; padding: 10px; margin-bottom: 8px; text-align: center; vertical-align: middle; display: -webkit-box; display: -webkit-flex; display: flex; -webkit-box-align: center; -webkit-align-items: center; align-items: center; -webkit-box-pack: center; -webkit-justify-content: center; justify-content: center; -webkit-flex-wrap: wrap; flex-wrap: wrap; } #navigation_panel button, #navigation_panel #calendar_year { background: hsl(207, 100%, 85%); color: hsl(207, 100%, 35%); font: 14px serif; border: 1px solid hsl(207, 100%, 35%); } #calendar_month { background: transparent; color: hsl(207, 100%, 35%); font: 14px serif; border: 1px solid hsl(207, 100%, 35%); } #navigation_panel button { height: 34px; vertical-align: middle; } #background_month { background: hsl(207, 100%, 85%); display: inline-block; } button.minus { width: 28px; text-align: center; border-radius: 12px 0 0 12px / 10px 0 0 10px; margin-right: -1px; } button.plus { width: 28px; text-align: center; border-radius: 0 12px 12px 0 / 0 10px 10px 0; margin-left: -1px; } .month_plus { margin-right: 35px; } #presently { width: 288px; margin-top: 10px; border-radius: 12px/10px; -webkit-box-flex: 0; -webkit-flex: 0 0 288px; flex: 0 0 288px; } #calendar_month { width: 89px; display: inline-block; } #calendar_year { width: 54px; display: inline-block; } select { height: 34px; } input { height: 30px; border: 1px solid #a9a9a9; display: inline-block; text-align: center; } #table { font: sans-serif; width: 100%; height: 311px; padding: 2px; } #table, td { border: 1px solid #a9a9a9; border-radius: 6px/4px; } td { margin: 1px; padding: 5px; text-align: center; } .prevMonth { opacity: 0.5; } .curMonth { background: #ff69b4; } .nextMonth { opacity: 0.4; } .nextMonth:nth-child(n + 6) { background: #ffebf5; } .curDay { background: #fffacd; } #presently.hide { display: none; } .week-day { background: #c2d6ff; color: #0069ff; } .week-day.curDay { border: 1px solid #285fcd; background: #6b9cff; color: #e6f5ff; font-weight: bold; } .week-day:nth-child(n + 6) { background: #ffcae3; color: #ff0075; } .week-day.curDay:nth-child(n + 6) { border: 1px solid #ff0075; background: #ff97c8; color: #fff8fc; text-shadow: 1px 1px #ff0075, 1px -1px #ff0075, -1px 1px #ff0075, -1px -1px #ff0075, 1px 0 #ff0075, -1px 0 #ff0075, 0 1px #ff0075, 0 -1px #ff0075; } #table tbody td { background: #def1ff; color: #0091ff; } #table tbody td:nth-child(n + 6) { background: #ffdced; color: #ff0075; } #table tbody td.curMonth.curDay { background: #c2d6ff; border: 1px solid hsl(207, 100%, 35%); font-weight: bold; color: #fff; text-shadow: 1px 1px hsl(207, 100%, 35%), -1px 1px hsl(207, 100%, 35%), 1px -1px hsl(207, 100%, 35%), -1px -1px hsl(207, 100%, 35%), 1px 0 hsl(207, 100%, 35%), 0 1px hsl(207, 100%, 35%), -1px 0 hsl(207, 100%, 35%), 0 -1px hsl(207, 100%, 35%); } #table tbody td.curMonth.curDay:nth-child(n + 6) { background: #ffbadb; border: 1px solid #ff0075; text-shadow: 1px 1px #ff0075, 1px -1px #ff0075, -1px 1px #ff0075, -1px -1px #ff0075, 1px 0 #ff0075, -1px 0 #ff0075, 0 1px #ff0075, 0 -1px #ff0075; } </style> </head> <body> <div id="calendar"> <div id="navigation_panel"> <button class="month_minus minus">‹</button> <span id="background_month"><select id="calendar_month"></select></span> <button class="month_plus plus">›</button> <button class="year_minus minus">‹</button> <input type="text" size="4" id="calendar_year" /> <button class="year_plus plus">›</button><br /> <button id="presently">сегодня</button> </div> <table id="table"> <thead> <tr> <td class="week-day">Пн.</td> <td class="week-day">Вт.</td> <td class="week-day">Ср.</td> <td class="week-day">Чт.</td> <td class="week-day">Пт.</td> <td class="week-day">Сб.</td> <td class="week-day">Вс.</td> </tr> </thead> <tbody></tbody> </table> <script> Date.prototype.reduce = function(callback, value) { var year = this.getFullYear(); var month = this.getMonth(); var step = new Date(year, month, 1); var last = new Date(year, month + 1, 0); step.setHours(24 * (0 - ((step.getDay() + 6) % 7))); last.setHours(24 * (6 - ((last.getDay() + 6) % 7))); for (var i = 0; step <= last; i++) { value = callback(value, new Date(+step), i, this); step.setHours(24); } return value; }; var data = new Date(); var selectMonth = document.querySelector('#calendar_month'); var monthNames = [ 'Январь', 'Февраль', 'Март', 'Апрель', 'Май', 'Июнь', 'Июль', 'Август', 'Сентябрь', 'Октябрь', 'Ноябрь', 'Декабрь', ]; monthNames.forEach(function(monthName, i) { selectMonth.options[i] = new Option(monthName, i); }); selectMonth.addEventListener('change', function() { data.setMonth(this.value); createCalendar(data); }); var minusMonth = document.querySelector('.month_minus'); minusMonth.addEventListener('click', function() { data.setMonth(data.getMonth() - 1); createCalendar(data); }); var plusMonth = document.querySelector('.month_plus'); plusMonth.addEventListener('click', function() { data.setMonth(data.getMonth() + 1); createCalendar(data); }); var minusYear = document.querySelector('.year_minus'); minusYear.addEventListener('click', function() { data.setYear(data.getFullYear() - 1); createCalendar(data); }); var plusYear = document.querySelector('.year_plus'); plusYear.addEventListener('click', function() { data.setYear(data.getFullYear() + 1); createCalendar(data); }); var inputYear = document.querySelector('#calendar_year'); inputYear.addEventListener('input', function() { if (/^d{4}$/.test(this.value)) { data.setYear(this.value); createCalendar(data); } }); var currentButton = document.querySelector('#presently'); currentButton.addEventListener('click', function() { data = new Date(); createCalendar(data); }); var daysTd = document.querySelectorAll('.week-day'); function createCalendar(data) { var now = new Date().setHours(0, 0, 0, 0); var year = data.getFullYear(); inputYear.value = year; var month = data.getMonth(); selectMonth.selectedIndex = month; currentButton.classList.remove('hide'); var dayTd = document.querySelector('.week-day.curDay'); if (dayTd) dayTd.classList.remove('curDay'); var cls = ['prevMonth', 'curMonth', 'nextMonth'], indexCls = 0; var html = data.reduce(function(value, current, index, source) { var date = current.getDate(); if (date == 1) indexCls++; var className = cls[indexCls]; if (+now == +current && indexCls == 1) { className += ' curDay'; currentButton.classList.add('hide'); daysTd[index % 7].classList.add('curDay'); } if (current.getDay() == 1) value += '<tr>'; return value + '<td class="' + className + '">' + date; }, ''); document.querySelector('#table tbody').innerHTML = html; } createCalendar(data); var timer; function refresh() { window.clearTimeout(timer); var finish = new Date().setHours(24, 0, 0, 0); finish -= data; timer = window.setTimeout(function() { createCalendar(data); refresh(); }, finish); } refresh(); </script> </body> </html> |
Надо чтобы высота блока navigatio_panel при текущем месяце была 34 а высота таблицы311, а при просмотре других месяцев высота блока 78 а высота таблицы 267 пикселей, как на js это реализовать?
|
рони,
почему нельзя указать фон/цвет/рамку для элементов с идами prevMonth и nextMonth, только opacity 0.4 ? Как это исправить? |
Цитата:
#table tbody td.prevMonth |
рони, :thanks:
а возможно задать высоту панели и таблицы? |
Цитата:
Цитата:
Цитата:
|
рони,
Цитата:
|
рони,
Надо чтобы при появлении/исчезновении кнопки 'сегодня' таблица и панель меняли высоту, а высота всего календаря оставалась неизменной 375 пк |
Блондинка,
алгоритм 1. придумать название класса для текущего месяца 2. написать css, название класса название блока размеры. 3. заменить в строках 143 и 154 currentButton на div id="calendar" и hide на пункт 1. 4. исправить селектор в строке 31 см. пункт 1 . всё! |
Цитата:
закройте тег в строке 67 </table> </div> <script> |
Цитата:
|
Блондинка,
<!DOCTYPE html> <html lang="ru"> <head> <meta charset="utf-8"> <title></title> <style> body { width: 760px; } body, select, input { font: 14px serif; } #calendar { width: 330px; height: 375px; display: inline-block; border: 1px solid #a9a9a9; border-radius: 6px/4px; padding: 5px; } #navigation_panel { background: hsl(207, 100%, 92%); min-height: 34px; max-height: 78px; border: 1px solid #a9a9a9; border-radius: 6px/4px; padding: 10px; margin-bottom: 8px; text-align: center; vertical-align: middle; display: -webkit-box; display: -webkit-flex; display: flex; -webkit-box-align: center; -webkit-align-items: center; align-items: center; -webkit-box-pack: center; -webkit-justify-content: center; justify-content: center; -webkit-flex-wrap: wrap; flex-wrap: wrap; } #navigation_panel button, #navigation_panel #calendar_year { background: hsl(207, 100%, 85%); color: hsl(207, 100%, 35%); font: 14px serif; border: 1px solid hsl(207, 100%, 35%); } #calendar_month { background: transparent; color: hsl(207, 100%, 35%); font: 14px serif; border: 1px solid hsl(207, 100%, 35%); } #navigation_panel button { height: 34px; vertical-align: middle; } #background_month { background: hsl(207, 100%, 85%); display: inline-block; } button.minus { width: 28px; text-align: center; border-radius: 12px 0 0 12px / 10px 0 0 10px; margin-right: -1px; } button.plus { width: 28px; text-align: center; border-radius: 0 12px 12px 0 / 0 10px 10px 0; margin-left: -1px; } .month_plus { margin-right: 35px; } #presently { width: 288px; margin-top: 10px; border-radius: 12px/10px; -webkit-box-flex: 0; -webkit-flex: 0 0 288px; flex: 0 0 288px; } #calendar_month { width: 89px; display: inline-block; } #calendar_year { width: 54px; display: inline-block; } select { height: 34px; } input { height: 30px; border: 1px solid #a9a9a9; display: inline-block; text-align: center; } #table { font: sans-serif; width: 100%; height: 311px; padding: 2px; } #table, td { border: 1px solid #a9a9a9; border-radius: 6px/4px; } td { margin: 1px; padding: 5px; text-align: center; } .prevMonth { opacity: 0.5; } .curMonth { background: #ff69b4; } .nextMonth { opacity: 0.4; } .nextMonth:nth-child(n + 6) { background: #ffebf5; } .curDay { background: #fffacd; } .week-day { background: #c2d6ff; color: #0069ff; } .week-day.curDay { border: 1px solid #285fcd; background: #6b9cff; color: #e6f5ff; font-weight: bold; } .week-day:nth-child(n + 6) { background: #ffcae3; color: #ff0075; } .week-day.curDay:nth-child(n + 6) { border: 1px solid #ff0075; background: #ff97c8; color: #fff8fc; text-shadow: 1px 1px #ff0075, 1px -1px #ff0075, -1px 1px #ff0075, -1px -1px #ff0075, 1px 0 #ff0075, -1px 0 #ff0075, 0 1px #ff0075, 0 -1px #ff0075; } #table tbody td { background: #def1ff; color: #0091ff; } #table tbody td:nth-child(n + 6) { background: #ffdced; color: #ff0075; } #table tbody td.curMonth.curDay { background: #c2d6ff; border: 1px solid hsl(207, 100%, 35%); font-weight: bold; color: #fff; text-shadow: 1px 1px hsl(207, 100%, 35%), -1px 1px hsl(207, 100%, 35%), 1px -1px hsl(207, 100%, 35%), -1px -1px hsl(207, 100%, 35%), 1px 0 hsl(207, 100%, 35%), 0 1px hsl(207, 100%, 35%), -1px 0 hsl(207, 100%, 35%), 0 -1px hsl(207, 100%, 35%); } #table tbody td.curMonth.curDay:nth-child(n + 6) { background: #ffbadb; border: 1px solid #ff0075; text-shadow: 1px 1px #ff0075, 1px -1px #ff0075, -1px 1px #ff0075, -1px -1px #ff0075, 1px 0 #ff0075, -1px 0 #ff0075, 0 1px #ff0075, 0 -1px #ff0075; } #presently { display: none; } .curMonthShow #table{ height: 267px; } .curMonthShow #presently{ display: block; } </style> </head> <body> <div id="calendar"> <div id="navigation_panel"> <button class="month_minus minus">‹</button> <span id="background_month"><select id="calendar_month"></select></span> <button class="month_plus plus">›</button> <button class="year_minus minus">‹</button> <input type="text" size="4" id="calendar_year" /> <button class="year_plus plus">›</button><br /> <button id="presently">сегодня</button> </div> <table id="table"> <thead> <tr> <td class="week-day">Пн.</td> <td class="week-day">Вт.</td> <td class="week-day">Ср.</td> <td class="week-day">Чт.</td> <td class="week-day">Пт.</td> <td class="week-day">Сб.</td> <td class="week-day">Вс.</td> </tr> </thead> <tbody></tbody> </table> </div> <script> Date.prototype.reduce = function(callback, value) { var year = this.getFullYear(); var month = this.getMonth(); var step = new Date(year, month, 1); var last = new Date(year, month + 1, 0); step.setHours(24 * (0 - ((step.getDay() + 6) % 7))); last.setHours(24 * (6 - ((last.getDay() + 6) % 7))); for (var i = 0; step <= last; i++) { value = callback(value, new Date(+step), i, this); step.setHours(24); } return value; }; var data = new Date(); var selectMonth = document.querySelector('#calendar_month'); var monthNames = [ 'Январь', 'Февраль', 'Март', 'Апрель', 'Май', 'Июнь', 'Июль', 'Август', 'Сентябрь', 'Октябрь', 'Ноябрь', 'Декабрь', ]; monthNames.forEach(function(monthName, i) { selectMonth.options[i] = new Option(monthName, i); }); selectMonth.addEventListener('change', function() { data.setMonth(this.value); createCalendar(data); }); var minusMonth = document.querySelector('.month_minus'); minusMonth.addEventListener('click', function() { data.setMonth(data.getMonth() - 1); createCalendar(data); }); var plusMonth = document.querySelector('.month_plus'); plusMonth.addEventListener('click', function() { data.setMonth(data.getMonth() + 1); createCalendar(data); }); var minusYear = document.querySelector('.year_minus'); minusYear.addEventListener('click', function() { data.setYear(data.getFullYear() - 1); createCalendar(data); }); var plusYear = document.querySelector('.year_plus'); plusYear.addEventListener('click', function() { data.setYear(data.getFullYear() + 1); createCalendar(data); }); var inputYear = document.querySelector('#calendar_year'); inputYear.addEventListener('input', function() { if (/^d{4}$/.test(this.value)) { data.setYear(this.value); createCalendar(data); } }); var currentButton = document.querySelector('#presently'); currentButton.addEventListener('click', function() { data = new Date(); createCalendar(data); }); var daysTd = document.querySelectorAll('.week-day'); var blockCalendar = document.querySelector('#calendar'); function createCalendar(data) { var now = new Date().setHours(0, 0, 0, 0); var year = data.getFullYear(); inputYear.value = year; var month = data.getMonth(); selectMonth.selectedIndex = month; blockCalendar.classList.add('curMonthShow'); var dayTd = document.querySelector('.week-day.curDay'); if (dayTd) dayTd.classList.remove('curDay'); var cls = ['prevMonth', 'curMonth', 'nextMonth'], indexCls = 0; var html = data.reduce(function(value, current, index, source) { var date = current.getDate(); if (date == 1) indexCls++; var className = cls[indexCls]; if (+now == +current && indexCls == 1) { className += ' curDay'; blockCalendar.classList.remove('curMonthShow'); daysTd[index % 7].classList.add('curDay'); } if (current.getDay() == 1) value += '<tr>'; return value + '<td class="' + className + '">' + date; }, ''); document.querySelector('#table tbody').innerHTML = html; } createCalendar(data); var timer; function refresh() { window.clearTimeout(timer); var finish = new Date().setHours(24, 0, 0, 0); finish -= data; timer = window.setTimeout(function() { createCalendar(data); refresh(); }, finish); } refresh(); </script> </body> </html> |
рони, :thanks: :thanks: :thanks:
вот сейчас всё работает на отлично, прежде закрыть вопрос с календарем, остался один вопрос, возможно ли сделать такой же календарь, с такой же панелью, подсветкой дней, обновлением подсветки, короче со всеми функционалом этого календаря, только вертикальный, как тут? |
Блондинка,
вариант без js. <!DOCTYPE html> <html lang="ru"> <head> <meta charset="utf-8"> <title></title> <style> body { width: 760px; } body, select, input { font: 14px serif; } #calendar { width: 330px; height: 375px; display: inline-block; border: 1px solid #a9a9a9; border-radius: 6px/4px; padding: 5px; } #navigation_panel { background: hsl(207, 100%, 92%); min-height: 34px; max-height: 78px; border: 1px solid #a9a9a9; border-radius: 6px/4px; padding: 10px; margin-bottom: 6px; text-align: center; vertical-align: middle; display: -webkit-box; display: -webkit-flex; display: flex; -webkit-box-align: center; -webkit-align-items: center; align-items: center; -webkit-box-pack: center; -webkit-justify-content: center; justify-content: center; -webkit-flex-wrap: wrap; flex-wrap: wrap; } #navigation_panel button, #navigation_panel #calendar_year { background: hsl(207, 100%, 85%); color: hsl(207, 100%, 35%); font: 14px serif; border: 1px solid hsl(207, 100%, 35%); } #calendar_month { background: transparent; color: hsl(207, 100%, 35%); font: 14px serif; border: 1px solid hsl(207, 100%, 35%); } #navigation_panel button { height: 34px; vertical-align: middle; } #background_month { background: hsl(207, 100%, 85%); display: inline-block; } button.minus { width: 28px; text-align: center; border-radius: 12px 0 0 12px / 10px 0 0 10px; margin-right: -1px; } button.plus { width: 28px; text-align: center; border-radius: 0 12px 12px 0 / 0 10px 10px 0; margin-left: -1px; } .month_plus { margin-right: 35px; } #presently { width: 288px; margin-top: 10px; border-radius: 12px/10px;} #calendar_month { width: 89px; display: inline-block; } #calendar_year { width: 54px; display: inline-block; } select { height: 34px; } input { height: 30px; border: 1px solid #a9a9a9; display: inline-block; text-align: center; } #table { font: sans-serif; width: 100%; padding: 2px; } #table, td { border: 1px solid #a9a9a9; border-radius: 6px/4px; } td { margin: 1px; padding: 5px; text-align: center; } .prevMonth { opacity: 0.5; } .curMonth { background: #ff69b4; } .nextMonth { opacity: 0.4; } .nextMonth:nth-child(n + 6) { background: #ffebf5; } .curDay { background: #fffacd; } #presently.hide { display: none; } .week-day { background: #c2d6ff; color: #0069ff; } .week-day.curDay { border: 1px solid #285fcd; background: #6b9cff; color: #e6f5ff; font-weight: bold; } .week-day:nth-child(n + 6) { background: #ffcae3; color: #ff0075; } .week-day.curDay:nth-child(n + 6) { border: 1px solid #ff0075; background: #ff97c8; color: #fff8fc; text-shadow: 1px 1px #ff0075, 1px -1px #ff0075, -1px 1px #ff0075, -1px -1px #ff0075, 1px 0 #ff0075, -1px 0 #ff0075, 0 1px #ff0075, 0 -1px #ff0075; } #table tbody td { background: #def1ff; color: #0091ff; } #table tbody td:nth-child(n + 6) { background: #ffdced; color: #ff0075; } #table tbody td.curMonth.curDay { background: #c2d6ff; border: 1px solid hsl(207, 100%, 35%); font-weight: bold; color: #fff; text-shadow: 1px 1px hsl(207, 100%, 35%), -1px 1px hsl(207, 100%, 35%), 1px -1px hsl(207, 100%, 35%), -1px -1px hsl(207, 100%, 35%), 1px 0 hsl(207, 100%, 35%), 0 1px hsl(207, 100%, 35%), -1px 0 hsl(207, 100%, 35%), 0 -1px hsl(207, 100%, 35%); } #table tbody td.curMonth.curDay:nth-child(n + 6) { background: #ffbadb; border: 1px solid #ff0075; text-shadow: 1px 1px #ff0075, 1px -1px #ff0075, -1px 1px #ff0075, -1px -1px #ff0075, 1px 0 #ff0075, -1px 0 #ff0075, 0 1px #ff0075, 0 -1px #ff0075; } #calendar{ display: flex; flex-direction: column; } .content{ flex: 1; } #table{ height:100%; } </style> </head> <body> <div id="calendar"> <div id="navigation_panel"> <button class="month_minus minus">‹</button> <span id="background_month"><select id="calendar_month"></select></span> <button class="month_plus plus">›</button> <button class="year_minus minus">‹</button> <input type="text" size="4" id="calendar_year" /> <button class="year_plus plus">›</button><br /> <button id="presently">сегодня</button> </div> <div class="content"> <table id="table"> <thead> <tr> <td class="week-day">Пн.</td> <td class="week-day">Вт.</td> <td class="week-day">Ср.</td> <td class="week-day">Чт.</td> <td class="week-day">Пт.</td> <td class="week-day">Сб.</td> <td class="week-day">Вс.</td> </tr> </thead> <tbody></tbody> </table></div> </div> <script> Date.prototype.reduce = function(callback, value) { var year = this.getFullYear(); var month = this.getMonth(); var step = new Date(year, month, 1); var last = new Date(year, month + 1, 0); step.setHours(24 * (0 - ((step.getDay() + 6) % 7))); last.setHours(24 * (6 - ((last.getDay() + 6) % 7))); for (var i = 0; step <= last; i++) { value = callback(value, new Date(+step), i, this); step.setHours(24); } return value; }; var data = new Date(); var selectMonth = document.querySelector('#calendar_month'); var monthNames = [ 'Январь', 'Февраль', 'Март', 'Апрель', 'Май', 'Июнь', 'Июль', 'Август', 'Сентябрь', 'Октябрь', 'Ноябрь', 'Декабрь', ]; monthNames.forEach(function(monthName, i) { selectMonth.options[i] = new Option(monthName, i); }); selectMonth.addEventListener('change', function() { data.setMonth(this.value); createCalendar(data); }); var minusMonth = document.querySelector('.month_minus'); minusMonth.addEventListener('click', function() { data.setMonth(data.getMonth() - 1); createCalendar(data); }); var plusMonth = document.querySelector('.month_plus'); plusMonth.addEventListener('click', function() { data.setMonth(data.getMonth() + 1); createCalendar(data); }); var minusYear = document.querySelector('.year_minus'); minusYear.addEventListener('click', function() { data.setYear(data.getFullYear() - 1); createCalendar(data); }); var plusYear = document.querySelector('.year_plus'); plusYear.addEventListener('click', function() { data.setYear(data.getFullYear() + 1); createCalendar(data); }); var inputYear = document.querySelector('#calendar_year'); inputYear.addEventListener('input', function() { if (/^d{4}$/.test(this.value)) { data.setYear(this.value); createCalendar(data); } }); var currentButton = document.querySelector('#presently'); currentButton.addEventListener('click', function() { data = new Date(); createCalendar(data); }); var daysTd = document.querySelectorAll('.week-day'); function createCalendar(data) { var now = new Date().setHours(0, 0, 0, 0); var year = data.getFullYear(); inputYear.value = year; var month = data.getMonth(); selectMonth.selectedIndex = month; currentButton.classList.remove('hide'); var dayTd = document.querySelector('.week-day.curDay'); if (dayTd) dayTd.classList.remove('curDay'); var cls = ['prevMonth', 'curMonth', 'nextMonth'], indexCls = 0; var html = data.reduce(function(value, current, index, source) { var date = current.getDate(); if (date == 1) indexCls++; var className = cls[indexCls]; if (+now == +current && indexCls == 1) { className += ' curDay'; currentButton.classList.add('hide'); daysTd[index % 7].classList.add('curDay'); } if (current.getDay() == 1) value += '<tr>'; return value + '<td class="' + className + '">' + date; }, ''); document.querySelector('#table tbody').innerHTML = html; } createCalendar(data); var timer; function refresh() { window.clearTimeout(timer); var finish = new Date().setHours(24, 0, 0, 0); finish -= data; timer = window.setTimeout(function() { createCalendar(data); refresh(); }, finish); } refresh(); </script> </body> </html> |
Цитата:
|
рони,
возможно ли заменить сетку таблицы чтобы дни располагались в столбик как тут? |
Блондинка,
можно. |
рони,
можешь помочь решить эту проблему? |
Блондинка,
<!DOCTYPE html> <html lang="ru"> <head> <meta charset="utf-8"> <title></title> <style> body { width: 760px; } body, select, input { font: 14px serif; } #calendar { width: 330px; height: 375px; display: inline-block; border: 1px solid #a9a9a9; border-radius: 6px/4px; padding: 5px; } #navigation_panel { background: hsl(207, 100%, 92%); min-height: 34px; max-height: 78px; border: 1px solid #a9a9a9; border-radius: 6px/4px; padding: 10px; margin-bottom: 6px; text-align: center; vertical-align: middle; display: -webkit-box; display: -webkit-flex; display: flex; -webkit-box-align: center; -webkit-align-items: center; align-items: center; -webkit-box-pack: center; -webkit-justify-content: center; justify-content: center; -webkit-flex-wrap: wrap; flex-wrap: wrap; } #navigation_panel button, #navigation_panel #calendar_year { background: hsl(207, 100%, 85%); color: hsl(207, 100%, 35%); font: 14px serif; border: 1px solid hsl(207, 100%, 35%); } #calendar_month { background: transparent; color: hsl(207, 100%, 35%); font: 14px serif; border: 1px solid hsl(207, 100%, 35%); } #navigation_panel button { height: 34px; vertical-align: middle; } #background_month { background: hsl(207, 100%, 85%); display: inline-block; } button.minus { width: 28px; text-align: center; border-radius: 12px 0 0 12px / 10px 0 0 10px; margin-right: -1px; } button.plus { width: 28px; text-align: center; border-radius: 0 12px 12px 0 / 0 10px 10px 0; margin-left: -1px; } .month_plus { margin-right: 35px; } #presently { width: 288px; margin-top: 10px; border-radius: 12px/10px;} #calendar_month { width: 89px; display: inline-block; } #calendar_year { width: 54px; display: inline-block; } select { height: 34px; } input { height: 30px; border: 1px solid #a9a9a9; display: inline-block; text-align: center; } #table { font: sans-serif; width: 100%; padding: 2px; } #table, td { border: 1px solid #a9a9a9; border-radius: 6px/4px; } td { margin: 1px; padding: 5px; text-align: center; } .prevMonth { opacity: 0.5; } .curMonth { background: #ff69b4; } .nextMonth { opacity: 0.4; } .curDay { background: #fffacd; } #presently.hide { display: none; } .week-day { background: #c2d6ff; color: #0069ff; } .week-day.curDay { border: 1px solid #285fcd; background: #6b9cff; color: #e6f5ff; font-weight: bold; } tr:nth-child(n + 6) .week-day { background: #ffcae3; color: #ff0075; } tr:nth-child(n + 6) .week-day.curDay { border: 1px solid #ff0075; background: #ff97c8; color: #fff8fc; text-shadow: 1px 1px #ff0075, 1px -1px #ff0075, -1px 1px #ff0075, -1px -1px #ff0075, 1px 0 #ff0075, -1px 0 #ff0075, 0 1px #ff0075, 0 -1px #ff0075; } #table tbody td { background: #def1ff; color: #0091ff; } #table tbody tr:nth-child(n + 6) td{ background: #ffdced; color: #ff0075; } #table tbody td.curMonth.curDay { background: #c2d6ff; border: 1px solid hsl(207, 100%, 35%); font-weight: bold; color: #fff; text-shadow: 1px 1px hsl(207, 100%, 35%), -1px 1px hsl(207, 100%, 35%), 1px -1px hsl(207, 100%, 35%), -1px -1px hsl(207, 100%, 35%), 1px 0 hsl(207, 100%, 35%), 0 1px hsl(207, 100%, 35%), -1px 0 hsl(207, 100%, 35%), 0 -1px hsl(207, 100%, 35%); } #table tbody td.curMonth.curDay:nth-child(n + 6) { background: #ffbadb; border: 1px solid #ff0075; text-shadow: 1px 1px #ff0075, 1px -1px #ff0075, -1px 1px #ff0075, -1px -1px #ff0075, 1px 0 #ff0075, -1px 0 #ff0075, 0 1px #ff0075, 0 -1px #ff0075; } #calendar{ display: flex; flex-direction: column; } .content{ flex: 1; } #table{ height:100%; } #table tbody tr td:first-child{ } </style> </head> <body> <div id="calendar"> <div id="navigation_panel"> <button class="month_minus minus">‹</button> <span id="background_month"><select id="calendar_month"></select></span> <button class="month_plus plus">›</button> <button class="year_minus minus">‹</button> <input type="text" size="4" id="calendar_year" /> <button class="year_plus plus">›</button><br /> <button id="presently">сегодня</button> </div> <div class="content"> <table id="table"> <tbody></tbody> </table></div> </div> <script> Date.prototype.reduce = function(callback, value) { var year = this.getFullYear(); var month = this.getMonth(); var step = new Date(year, month, 1); var last = new Date(year, month + 1, 0); step.setHours(24 * (0 - ((step.getDay() + 6) % 7))); last.setHours(24 * (6 - ((last.getDay() + 6) % 7))); for (var i = 0; step <= last; i++) { value = callback(value, new Date(+step), i, this); step.setHours(24); } return value; }; var data = new Date(); var selectMonth = document.querySelector('#calendar_month'); var monthNames = [ 'Январь', 'Февраль', 'Март', 'Апрель', 'Май', 'Июнь', 'Июль', 'Август', 'Сентябрь', 'Октябрь', 'Ноябрь', 'Декабрь', ]; monthNames.forEach(function(monthName, i) { selectMonth.options[i] = new Option(monthName, i); }); selectMonth.addEventListener('change', function() { data.setMonth(this.value); createCalendar(data); }); var minusMonth = document.querySelector('.month_minus'); minusMonth.addEventListener('click', function() { data.setMonth(data.getMonth() - 1); createCalendar(data); }); var plusMonth = document.querySelector('.month_plus'); plusMonth.addEventListener('click', function() { data.setMonth(data.getMonth() + 1); createCalendar(data); }); var minusYear = document.querySelector('.year_minus'); minusYear.addEventListener('click', function() { data.setYear(data.getFullYear() - 1); createCalendar(data); }); var plusYear = document.querySelector('.year_plus'); plusYear.addEventListener('click', function() { data.setYear(data.getFullYear() + 1); createCalendar(data); }); var inputYear = document.querySelector('#calendar_year'); inputYear.addEventListener('input', function() { if (/^d{4}$/.test(this.value)) { data.setYear(this.value); createCalendar(data); } }); var currentButton = document.querySelector('#presently'); currentButton.addEventListener('click', function() { data = new Date(); createCalendar(data); }); function createCalendar(data) { var now = new Date().setHours(0, 0, 0, 0); var year = data.getFullYear(); inputYear.value = year; var month = data.getMonth(); selectMonth.selectedIndex = month; currentButton.classList.remove('hide'); var indexcurDay; var cls = ['prevMonth', 'curMonth', 'nextMonth'], indexCls = 0; var html = data.reduce(function(value, current, index, source) { var date = current.getDate(); if (date == 1) indexCls++; var className = cls[indexCls]; if (+now == +current && indexCls == 1) { className += ' curDay'; currentButton.classList.add('hide'); indexcurDay = index % 7; } value[index % 7] += '<td class="' + className + '">' + date; return value }, ['<tr><td class="week-day">Пн.', '<tr><td class="week-day">Вт.', '<tr><td class="week-day">Ср.', '<tr><td class="week-day">Чт.', '<tr><td class="week-day">Пт.', '<tr><td class="week-day">Сб.', '<tr><td class="week-day">Вс.']); document.querySelector('#table tbody').innerHTML = html.join(''); var daysTd = document.querySelectorAll('.week-day'); if(indexcurDay !== void 0) daysTd[indexcurDay].classList.add('curDay'); } createCalendar(data); var timer; function refresh() { window.clearTimeout(timer); var finish = new Date().setHours(24, 0, 0, 0); finish -= data; timer = window.setTimeout(function() { createCalendar(data); refresh(); }, finish); } refresh(); </script> </body> </html> |
рони,
:thanks: :thanks: :thanks: |
Осталось два момента, как с помощью js изменить высоту таблицы с 413 (без кнопки 'сегодня') на 369 с кнопкой, поскольку
Цитата:
И второй момент, как с помощью js задать ячейкам week-day ширину 58пк (что равно 20%) а оставшуюся ширину равномерно распределить между оставшимися ячейками, в зависимости от количества недель в месяце (4/5/6)? другими словами надо задать ширину ячеек начиная со второй 20% если в строке 5 ячеек, или 16% если в строке 6 ячеек, или 13.3% если в строке 7 ячеек, как это реализовать на js? <!DOCTYPE html> <html lang="ru"> <head> <meta charset="utf-8"> <title></title> <style> @import url('https://fonts.googleapis.com/css?family=Baumans&display=swap'); body { width: 760px; } body, select, input { font: 14px serif; } #calendar { width: 350px; height: 475px; display: inline-block; border: 1px solid #a9a9a9; border-radius: 6px/4px; padding: 5px; } #navigation_panel { background: hsl(207, 100%, 92%); min-height: 34px; max-height: 78px; border: 1px solid #a9a9a9; border-radius: 6px/4px; padding: 10px; margin-bottom: 6px; text-align: center; vertical-align: middle; display: -webkit-box; display: -webkit-flex; display: flex; -webkit-box-align: center; -webkit-align-items: center; align-items: center; -webkit-box-pack: center; -webkit-justify-content: center; justify-content: center; -webkit-flex-wrap: wrap; flex-wrap: wrap; } #navigation_panel button, #navigation_panel #calendar_year { background: hsl(207, 100%, 85%); color: hsl(207, 100%, 35%); font: 14px serif; border: 1px solid hsl(207, 100%, 35%); } #calendar_month { background: transparent; color: hsl(207, 100%, 35%); font: 14px serif; border: 1px solid hsl(207, 100%, 35%); } #navigation_panel button { height: 34px; vertical-align: middle; } #background_month { background: hsl(207, 100%, 85%); display: inline-block; } button.minus { width: 28px; text-align: center; border-radius: 12px 0 0 12px / 10px 0 0 10px; margin-right: -1px; } button.plus { width: 28px; text-align: center; border-radius: 0 12px 12px 0 / 0 10px 10px 0; margin-left: -1px; } .month_plus { margin-right: 35px; } #presently { width: 288px; margin-top: 10px; border-radius: 12px/10px;} #calendar_month { width: px; display: inline-block; } #calendar_year { width: px; display: inline-block; } select { height: 34px; } input { height: 30px; border: 1px solid #a9a9a9; display: inline-block; text-align: center; } #table { font-family: 'Baumans', cursive; width: 100%; height: 413px; padding: 2px; } #table, td { border: 1px solid #a9a9a9; border-radius: 6px/4px; } td { margin: 1px; text-align: center; } .prevMonth { opacity: 0.5; } .curMonth { background: #ff69b4; } .nextMonth { opacity: 0.4; } .curDay { background: #fffacd; } #presently.hide { display: none; } .week-day { background: #c2d6ff; color: #0069ff; font-family: 'Baumans', cursive; } .week-day.curDay { border: 1px solid #285fcd; background: #6b9cff; color: #fff; font-weight: bold; } tr:nth-child(n + 6) .week-day { background: #ffcae3; color: #fff; } tr:nth-child(n + 6) .week-day.curDay { border: 1px solid #ff0075; background: #ff97c8; color: #ffffff; text-shadow: 1px 1px #ff0075, 1px -1px #ff0075, -1px 1px #ff0075, -1px -1px #ff0075, 1px 0 #ff0075, -1px 0 #ff0075, 0 1px #ff0075, 0 -1px #ff0075; } #table tbody td { background: #def1ff; color: #0091ff; } #table tbody tr:nth-child(n + 6) td{ background: #ffdced; color: #ff0075; } #table tbody td.curMonth.curDay { background: #c2d6ff; border: 1px solid hsl(207, 100%, 35%); font-weight: bold; color: #fff; text-shadow: 1px 1px hsl(207, 100%, 35%), -1px 1px hsl(207, 100%, 35%), 1px -1px hsl(207, 100%, 35%), -1px -1px hsl(207, 100%, 35%), 1px 0 hsl(207, 100%, 35%), 0 1px hsl(207, 100%, 35%), -1px 0 hsl(207, 100%, 35%), 0 -1px hsl(207, 100%, 35%); } #table tbody td.curMonth.curDay:nth-child(n + 6) { background: #ffbadb; border: 1px solid #ff0075; color: #fff; text-shadow: 1px 1px #ff0075, 1px -1px #ff0075, -1px 1px #ff0075, -1px -1px #ff0075, 1px 0 #ff0075, -1px 0 #ff0075, 0 1px #ff0075, 0 -1px #ff0075; } </style> </head> <body> <div id="calendar"> <div id="navigation_panel"> <button class="month_minus minus">‹</button> <span id="background_month"><select id="calendar_month"></select></span> <button class="month_plus plus">›</button> <button class="year_minus minus">‹</button> <input type="text" size="4" id="calendar_year" /> <button class="year_plus plus">›</button><br /> <button id="presently">сегодня</button> </div> <table id="table"> <tbody></tbody> </table> </div> <script> Date.prototype.reduce = function(callback, value) { var year = this.getFullYear(); var month = this.getMonth(); var step = new Date(year, month, 1); var last = new Date(year, month + 1, 0); step.setHours(24 * (0 - ((step.getDay() + 6) % 7))); last.setHours(24 * (6 - ((last.getDay() + 6) % 7))); for (var i = 0; step <= last; i++) { value = callback(value, new Date(+step), i, this); step.setHours(24); } return value; }; var data = new Date(); var selectMonth = document.querySelector('#calendar_month'); var monthNames = [ 'Январь', 'Февраль', 'Март', 'Апрель', 'Май', 'Июнь', 'Июль', 'Август', 'Сентябрь', 'Октябрь', 'Ноябрь', 'Декабрь', ]; monthNames.forEach(function(monthName, i) { selectMonth.options[i] = new Option(monthName, i); }); selectMonth.addEventListener('change', function() { data.setMonth(this.value); createCalendar(data); }); var minusMonth = document.querySelector('.month_minus'); minusMonth.addEventListener('click', function() { data.setMonth(data.getMonth() - 1); createCalendar(data); }); var plusMonth = document.querySelector('.month_plus'); plusMonth.addEventListener('click', function() { data.setMonth(data.getMonth() + 1); createCalendar(data); }); var minusYear = document.querySelector('.year_minus'); minusYear.addEventListener('click', function() { data.setYear(data.getFullYear() - 1); createCalendar(data); }); var plusYear = document.querySelector('.year_plus'); plusYear.addEventListener('click', function() { data.setYear(data.getFullYear() + 1); createCalendar(data); }); var inputYear = document.querySelector('#calendar_year'); inputYear.addEventListener('input', function() { if (/^d{4}$/.test(this.value)) { data.setYear(this.value); createCalendar(data); } }); var currentButton = document.querySelector('#presently'); currentButton.addEventListener('click', function() { data = new Date(); createCalendar(data); }); function createCalendar(data) { var now = new Date().setHours(0, 0, 0, 0); var year = data.getFullYear(); inputYear.value = year; var month = data.getMonth(); selectMonth.selectedIndex = month; currentButton.classList.remove('hide'); var indexcurDay; var cls = ['prevMonth', 'curMonth', 'nextMonth'], indexCls = 0; var html = data.reduce(function(value, current, index, source) { var date = current.getDate(); if (date == 1) indexCls++; var className = cls[indexCls]; if (+now == +current && indexCls == 1) { className += ' curDay'; currentButton.classList.add('hide'); indexcurDay = index % 7; } value[index % 7] += '<td class="' + className + '">' + date; return value }, ['<tr><td class="week-day">Пн.', '<tr><td class="week-day">Вт.', '<tr><td class="week-day">Ср.', '<tr><td class="week-day">Чт.', '<tr><td class="week-day">Пт.', '<tr><td class="week-day">Сб.', '<tr><td class="week-day">Вс.']); document.querySelector('#table tbody').innerHTML = html.join(''); var daysTd = document.querySelectorAll('.week-day'); if(indexcurDay !== void 0) daysTd[indexcurDay].classList.add('curDay'); } createCalendar(data); var timer; function refresh() { window.clearTimeout(timer); var finish = new Date().setHours(24, 0, 0, 0); finish -= data; timer = window.setTimeout(function() { createCalendar(data); refresh(); }, finish); } refresh(); </script> </body> </html> |
Цитата:
|
рони,
я конечно смогу разобраться с этим алгоритмом, но для этого мне понадобится дней 7-8, и выровнять ширину ячеек с датами ещё неделя, если бы было всё так просто для меня, я бы сюда не обращалась... |
народ, помогите кто нибудь исправить недочеты что написаны в 21 посте...
|
рони,
почему не срабатывает 40 строка, я ведь заменила цвет на белый, Вс должно же быть текст белый жирный с красной тенью... |
Блондинка,
воскресенье это #table tbody tr:nth-child(7) td.week-day и не используйте background для цвета фона, лучше background-color |
nth-child(7) или nth-child(n + 6), я имела ввиду почему не срабатывало вчера подсветка для сег дня недели, указала белый цвет и красную тень, а было и цвет и тень красные...
|
Цитата:
|
всю ночь трахалась с этим календарем, но результата никакого, народ помогите кто нибудь
<!DOCTYPE html> <html lang="ru"> <head> <meta charset="utf-8"> <title></title> <style> @import url('https://fonts.googleapis.com/css?family=Baumans&display=swap'); body { width: 760px; } body, select, input { font: 14px serif; } #calendar { width: 350px; height: 475px; display: inline-block; border: 1px solid #a9a9a9; border-radius: 6px/4px; padding: 5px; } #navigation_panel { background-color: hsl(207, 100%, 92%); min-height: 34px; max-height: 78px; border: 1px solid #a9a9a9; border-radius: 6px/4px; padding: 10px; margin-bottom: 6px; text-align: center; vertical-align: middle; display: -webkit-box; display: -webkit-flex; display: flex; -webkit-box-align: center; -webkit-align-items: center; align-items: center; -webkit-box-pack: center; -webkit-justify-content: center; justify-content: center; -webkit-flex-wrap: wrap; flex-wrap: wrap; } #navigation_panel button, #navigation_panel #calendar_year { background-color: hsl(207, 100%, 85%); color: hsl(210,100%,50%); font: 14px serif; border: 1px solid hsl(210,100%,50%); } #calendar_month { background-color: transparent; color: hsl(210,100%,50%); font: 14px serif; border: 1px solid hsl(210,100%,50%); } #navigation_panel button { height: 34px; vertical-align: middle; } #background_month { background-color: hsl(207, 100%, 85%); display: inline-block; } button.minus { width: 28px; text-align: center; border-radius: 12px 0 0 12px / 10px 0 0 10px; margin-right: -1px; } button.plus { width: 28px; text-align: center; border-radius: 0 12px 12px 0 / 0 10px 10px 0; margin-left: -1px; } .month_plus { margin-right: 35px; } #presently { width: 288px; margin-top: 10px; border-radius: 12px/10px;} #calendar_month { width: px; display: inline-block; } #calendar_year { width: px; display: inline-block; } select { height: 34px; } input { height: 30px; border: 1px solid #a9a9a9; display: inline-block; text-align: center; } #table { font-family: 'Baumans', cursive; width: 100%; height: 413px; padding: 2px; } #table, td { border: 1px solid #a9a9a9; border-radius: 6px/4px; } td { margin: 1px; text-align: center; } #table tbody td.prevMonth { background-color: hsl(210,100%,95%); color: hsl(210,100%,80%); border: 1px solid hsl(0,0%,80%); } #table tbody tr:nth-child(n + 6) td.prevMonth { background-color: hsl(348,100%,95%); color: hsl(348,100%,80%); border: 1px solid hsl(0,0%,80%); } .curMonth { background-color: ; } #table tbody td.nextMonth { background-color: hsl(210,100%,95%); color: hsl(210,100%,80%); border: 1px solid hsl(0,0%,80%); } #table tbody tr:nth-child(n + 6) td.nextMonth { background-color: hsl(348,100%,95%); color: hsl(348,100%,80%); border: 1px solid hsl(0,0%,80%); } .curDay { background-color: #fffacd; } #presently.hide { display: none; } #table tbody tr td.week-day { background-color: hsl(210,100%,85%); color: hsl(210,100%,50%); border: 1px solid hsl(0,0%,60%); width: 20%; } /* день недели */ #table tbody tr:nth-child(n + 6) td.week-day { background-color: hsl(348,100%,85%); color: hsl(348,100%,50%); border: 1px solid hsl(0,0%,60%); } /* день недели выходной*/ #table tbody tr td.week-day.curDay { border: 1px solid hsl(210,100%,50%); background-color: hsl(210,100%,75%); color: hsl(210,100%,95%); font-weight: bold; text-shadow: 1px 1px hsl(210,100%,50%), -1px 1px hsl(210,100%,50%), 1px -1px hsl(210,100%,50%), -1px -1px hsl(210,100%,50%), 1px 0 hsl(210,100%,50%), 0 1px hsl(210,100%,50%), -1px 0 hsl(210,100%,50%), 0 -1px hsl(210,100%,50%); } /* сегодн день недели */ #table tbody tr:nth-child(n + 6) td.week-day.curDay { border: 1px solid hsl(348,100%,50%); background-color: hsl(348,100%,75%); color: hsl(348,100%,95%); font-weight: bold; text-shadow: 1px 1px hsl(348,100%,50%), 1px -1px hsl(348,100%,50%), -1px 1px hsl(348,100%,50%), -1px -1px hsl(348,100%,50%), 1px 0 hsl(348,100%,50%), -1px 0 hsl(348,100%,50%), 0 1px hsl(348,100%,50%), 0 -1px hsl(348,100%,50%); } /* сегодн день недели выходной */ #table tbody td { background-color: hsl(210,100%,90%); color: hsl(210,100%,50%); } /* будние дни текущего месяца */ #table tbody tr:nth-child(n + 6) td{ background-color: hsl(348,100%,90%); color: hsl(348,100%,50%); } /* выходные дни текущего месяца */ #table tbody tr td.curMonth.curDay { background-color: hsl(210,100%,80%); border: 1px solid hsl(210, 100%, 50%); font-weight: bold; color: hsl(210,100%,100%); text-shadow: 1px 1px hsl(210,100%,50%), -1px 1px hsl(210,100%,50%), 1px -1px hsl(210,100%,50%), -1px -1px hsl(210,100%,50%), 1px 0 hsl(210,100%,50%), 0 1px hsl(210,100%,50%), -1px 0 hsl(210,100%,50%), 0 -1px hsl(210,100%,50%); } #table tbody tr:nth-child(n + 6) td.curMonth.curDay: { background-color: hsl(348,100%,80%); border: 1px solid hsl(348,100%,50%); color: hsl(348,100%,100%); text-shadow: 1px 1px hsl(348,100%,50%), 1px -1px hsl(348,100%,50%), -1px 1px hsl(348,100%,50%), -1px -1px hsl(348,100%,50%), 1px 0 hsl(348,100%,50%), -1px 0 hsl(348,100%,50%), 0 1px hsl(348,100%,50%), 0 -1px hsl(348,100%,50%); } table { table-layout: fixed; width: 100%; } tbody>tr>*:nth-last-child(5)~* td:nth-child(n + 2) { width: 20%; } tbody>tr>*:nth-last-child(6)~* td:nth-child(n + 2) { width: 16%; } tbody>tr>*:nth-last-child(7)~* td:nth-child(n + 2) { width: 13.3%; }</style> </head> <body> <div id="calendar"> <div id="navigation_panel"> <button class="month_minus minus">‹</button> <span id="background_month"><select id="calendar_month"></select></span> <button class="month_plus plus">›</button> <button class="year_minus minus">‹</button> <input type="text" size="4" id="calendar_year" /> <button class="year_plus plus">›</button><br /> <button id="presently">сегодня</button> </div> <table id="table"> <colgroup width="58"> <colgroup span="6" width="2*"> <tbody></tbody> </table> </div> <script> Date.prototype.reduce = function(callback, value) { var year = this.getFullYear(); var month = this.getMonth(); var step = new Date(year, month, 1); var last = new Date(year, month + 1, 0); step.setHours(24 * (0 - ((step.getDay() + 6) % 7))); last.setHours(24 * (6 - ((last.getDay() + 6) % 7))); for (var i = 0; step <= last; i++) { value = callback(value, new Date(+step), i, this); step.setHours(24); } return value; }; var data = new Date(); var selectMonth = document.querySelector('#calendar_month'); var monthNames = [ 'Январь', 'Февраль', 'Март', 'Апрель', 'Май', 'Июнь', 'Июль', 'Август', 'Сентябрь', 'Октябрь', 'Ноябрь', 'Декабрь', ]; monthNames.forEach(function(monthName, i) { selectMonth.options[i] = new Option(monthName, i); }); selectMonth.addEventListener('change', function() { data.setMonth(this.value); createCalendar(data); }); var minusMonth = document.querySelector('.month_minus'); minusMonth.addEventListener('click', function() { data.setMonth(data.getMonth() - 1); createCalendar(data); }); var plusMonth = document.querySelector('.month_plus'); plusMonth.addEventListener('click', function() { data.setMonth(data.getMonth() + 1); createCalendar(data); }); var minusYear = document.querySelector('.year_minus'); minusYear.addEventListener('click', function() { data.setYear(data.getFullYear() - 1); createCalendar(data); }); var plusYear = document.querySelector('.year_plus'); plusYear.addEventListener('click', function() { data.setYear(data.getFullYear() + 1); createCalendar(data); }); var inputYear = document.querySelector('#calendar_year'); inputYear.addEventListener('input', function() { if (/^d{4}$/.test(this.value)) { data.setYear(this.value); createCalendar(data); } }); var currentButton = document.querySelector('#presently'); currentButton.addEventListener('click', function() { data = new Date(); createCalendar(data); }); function createCalendar(data) { var now = new Date().setHours(0, 0, 0, 0); var year = data.getFullYear(); inputYear.value = year; var month = data.getMonth(); selectMonth.selectedIndex = month; currentButton.classList.remove('hide'); var indexcurDay; var cls = ['prevMonth', 'curMonth', 'nextMonth'], indexCls = 0; var html = data.reduce(function(value, current, index, source) { var date = current.getDate(); if (date == 1) indexCls++; var className = cls[indexCls]; if (+now == +current && indexCls == 1) { className += ' curDay'; currentButton.classList.add('hide'); indexcurDay = index % 7; } value[index % 7] += '<td class="' + className + '">' + date; return value }, ['<tr><td class="week-day">Пн.', '<tr><td class="week-day">Вт.', '<tr><td class="week-day">Ср.', '<tr><td class="week-day">Чт.', '<tr><td class="week-day">Пт.', '<tr><td class="week-day">Сб.', '<tr><td class="week-day">Вс.']); document.querySelector('#table tbody').innerHTML = html.join(''); var daysTd = document.querySelectorAll('.week-day'); if(indexcurDay !== void 0) daysTd[indexcurDay].classList.add('curDay'); } createCalendar(data); var timer; function refresh() { window.clearTimeout(timer); var finish = new Date().setHours(24, 0, 0, 0); finish -= data; timer = window.setTimeout(function() { createCalendar(data); refresh(); }, finish); } refresh(); </script> </body> </html> |
Rise,
Причём тут дизайн и графический редактор? Надо изменять высоту таблицы при появлении/исчезновении кнопки 'сегодня' и изменять ширину ячеек с датами в зависимости от количества недель в месяце, равномерно распределить 80% ширины между ячейками с датами, а у ячеек с днем недели всегда 20% от ширины таблицы, как это решить на js? |
Rise,
вот решение 'на бумажке' <!DOCTYPE html> <head lang="ru"> <meta charset="utf-8"> <title></title> <style> td { border: 1px solid #000; height: 100px; margin: 1px; } </style> </head> <body> <div style="border: 1px solid #000; width: 350px; padding: 5px;"><table style="border: 1px solid #00f; width: 100%; padding: 2px;"><tr><td style="width: 62px;"></td><td style="width: 62px;"></td><td style="width: 62px;"></td><td style="width: 62px;"></td><td style="width: 62px;"></td></tr></table></div> <div style="border: 1px solid #000; width: 350px; padding: 5px;"><table style="border: 1px solid #00f; width: 100%; padding: 2px;"><tr><td style="width: 62px;"></td><td style="width: 48px;"></td><td style="width: 48px;"></td><td style="width: 48px;"></td><td style="width: 48px;"></td><td style="width: 48px;"></td></tr></table></div> <div style="border: 1px solid #000; width: 350px; padding: 5px;"><table style="border: 1px solid #00f; width: 100%; padding: 2px;"><tr><td style="width: 62px;"></td><td style="width: 39px;"></td><td style="width: 39px;"></td><td style="width: 39px;"></td><td style="width: 39px;"></td><td style="width: 39px;"></td><td style="width: 39px;"></td></tr></table></div> <br> если в строке (tr) 5 ячеек, то td id=week-day 62px, остальные ячейки 62px<br> если в строке (tr) 5 ячеек, то td id=week-day 62px, остальные ячейки 48px<br> если в строке (tr) 7 ячеек, то td id=week-day 62px, остальные ячейки 39px </body> </html> Надо на js узнать количество ячеек в строке и присвоить нужную ширину ячейкам |
А не проще ли использовать сетку, конечно если браузер ваш ее поддерживает.
|
laimas,
флексбокс? всё равно будут какие-то пиксели прыгать, а тут всё высчитано до пиксели, всё идеально... |
Цитата:
|
Цитата:
|
Цитата:
|
Цитата:
|
Цитата:
|
календарь не таких уж и больших размеров чтобы не вписаться, и в случае необходимости всегда можно посчитать размеры и изменить цифры, так что для меня такой вариант оптималем
|
как на js посчитать количество ячеек в строке и задать ширину? при условии что ширина первой постоянна...
|
Часовой пояс GMT +3, время: 19:48. |