原生js实现日期与时间选择器效果代码
代码语言:html
所属分类:选择器
代码描述:原生js实现日期与时间选择器效果代码
下面为部分代码预览,完整代码请点击下载或在bfwstudio webide中打开
<!DOCTYPE html> <html lang="en" > <head> <meta charset="UTF-8"> <style> body{ padding: 50px; } html { box-sizing: border-box; font-family: sans-serif; font-size: 1em; } *, *::before, *::after { box-sizing: inherit; } /* *********************************** */ .date-picker { --control-size: 1.7rem; width: min-content; display: grid; grid-template-columns: repeat(3,min-content); grid-auto-rows: min-content; gap: calc(0.2 * var(--control-size)); position: relative; } .date-picker button, .date-picker input { font-size: var(--control-size); } .date-picker > button:focus { outline: 2px dashed blue; outline-offset: 1px; } .date-picker > .back-btn { padding: 0px; position: relative; } .date-picker > .back-btn:before { content: ""; display: block; width: var(--control-size); height: var(--control-size); background: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' version='1.1' xmlns:xlink='http://www.w3.org/1999/xlink' xmlns:svgjs='http://svgjs.com/svgjs' viewBox='0 0 16 16' width='16' height='16'%3E%3Cg transform='matrix(0.6666666666666666,0,0,0.6666666666666666,0,0)'%3E%3Cpath d='M21.016.531A2,2,0,0,0,19,.563L2.929,10.288a2,2,0,0,0,0,3.422L19,23.436a2,2,0,0,0,3.035-1.711V2.274A2,2,0,0,0,21.016.531Z' fill='%23000000' stroke='none' stroke-linecap='round' stroke-linejoin='round' stroke-width='0'%3E%3C/path%3E%3C/g%3E%3C/svg%3E") no-repeat 50% 50% / calc(0.7 * var(--control-size)); } .date-picker > .back-btn:disabled { cursor: not-allowed; } .date-picker > .back-btn:disabled:before { opacity: 0.1; } .date-picker > .forward-btn { padding: 0px; position: relative; } .date-picker > .forward-btn:before { content: ""; display: block; width: var(--control-size); height: var(--control-size); position: relative; background: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' version='1.1' xmlns:xlink='http://www.w3.org/1999/xlink' xmlns:svgjs='http://svgjs.com/svgjs' viewBox='0 0 16 16' width='16' height='16'%3E%3Cg transform='matrix(0.6666666666666666,0,0,0.6666666666666666,0,0)'%3E%3Cpath d='M21.058,10.289,5.014.564A2,2,0,0,0,1.978,2.275v19.45a2,2,0,0,0,3.037,1.711l16.043-9.725a2,2,0,0,0,0-3.422Z' fill='%23000000' stroke='none' stroke-linecap='round' stroke-linejoin='round' stroke-width='0'%3E%3C/path%3E%3C/g%3E%3C/svg%3E") 50% 50% / calc(0.7 * var(--control-size)) no-repeat; } .date-picker > .forward-btn:disabled { cursor: not-allowed; } .date-picker > .forward-btn:disabled:before { opacity: 0.1; } .date-picker > .date-input { position: relative; height: 100%; align-self: center; } .date-picker > .date-input > input[type=text] { cursor: default; display: block; width: calc(10 * var(--control-size)); height: 100%; padding-left: 0.3em; z-index: 2; } .date-picker > .date-input.focused > input[type=text] { outline: 2px dashed blue; outline-offset: 1px; } .date-picker > .date-input::after { content: ""; position: absolute; width: calc(1.5 * var(--control-size)); height: 100%; top: 0px; right: 0px; z-index: 1; background: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' version='1.1' xmlns:xlink='http://www.w3.org/1999/xlink' xmlns:svgjs='http://svgjs.com/svgjs' viewBox='0 0 16 16' width='16' height='16'%3E%3Cg transform='matrix(0.6666666666666666,0,0,0.6666666666666666,0,0)'%3E%3Cpath d='M21.5,3H18.75a.25.25,0,0,1-.25-.25V1a1,1,0,0,0-2,0v4.75a.75.75,0,0,1-.75.75h0a.75.75,0,0,1-.75-.75V3.5a.5.5,0,0,0-.5-.5H8.25A.25.25,0,0,1,8,2.751V1A1,1,0,1,0,6,1v4.75a.75.75,0,0,1-.75.75h0a.75.75,0,0,1-.75-.75V3.5A.5.5,0,0,0,4,3H2.5a2,2,0,0,0-2,2V22a2,2,0,0,0,2,2h19a2,2,0,0,0,2-2V5A2,2,0,0,0,21.5,3ZM21,22H3a.5.5,0,0,1-.5-.5V9.5A.5.5,0,0,1,3,9H21a.5.5,0,0,1,.5.5v12A.5.5,0,0,1,21,22Z' fill='%23000000' stroke='none' stroke-linecap='round' stroke-linejoin='round' stroke-width='0'%3E%3C/path%3E%3C/g%3E%3C/svg%3E") calc(100% - (0.25 * var(--control-size))) 50% / calc(0.8 * var(--control-size)) no-repeat; } .date-picker > .date-input > input[type=date] { position: absolute; display: block; left: 0px; top: 0px; width: 100%; height: 100%; opacity: 0; cursor: pointer; border: none; padding: 0px; margin: 0px; z-index: 3; } .date-picker > .date-input > input[type=date]::-webkit-calendar-picker-indicator { position: absolute; left: -50%; top: 0px; width: 300%; height: 100%; color: transparent; background: transparent; cursor: pointer; z-index: 4; } /* *********************************** */ .time-picker { --control-size: 1.7rem; position: relative; display: inline-block; width: min-content; } .time-picker::after { content: ""; position: absolute; width: calc(1.5 * var(--control-size)); height: 100%; top: 0px; right: 0px; z-index: 1; background: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' version='1.1' xmlns:xlink='http://www.w3.org/1999/xlink' xmlns:svgjs='http://svgjs.com/svgjs' viewBox='0 0 16 16' width='16' height='16'%3E%3Cg transform='matrix(0.6666666666666666,0,0,0.6666666666666666,0,0)'%3E%3Cpath d='M12,0A12,12,0,1,0,24,12,12.014,12.014,0,0,0,12,0Zm0,22A10,10,0,1,1,22,12,10.011,10.011,0,0,1,12,22Z' fill='%23000000' stroke='none' stroke-linecap='round' stroke-linejoin='round' stroke-width='0'%3E%3C/path%3E%3Cpath d='M17.134,15.81,12.5,11.561V6.5a1,1,0,0,0-2,0V12a1,1,0,0,0,.324.738l4.959,4.545a1.01,1.01,0,0,0,1.413-.061A1,1,0,0,0,17.134,15.81Z' fill='%23000000' stroke='none' stroke-linecap='round' stroke-linejoin='round' stroke-width='0'%3E%3C/path%3E%3C/g%3E%3C/svg%3E") calc(100% - (0.25 * var(--control-size))) 50% / calc(0.8 * var(--control-size)) no-repeat; } .time-picker input { font-size: var(--control-size); } .time-picker > input[type=text] { cursor: default; display: block; width: calc(7 * var(--control-size)); height: 100%; padding-left: 0.3em; z-index: 2; } .time-picker.focused > input[type=text] { outline: 2px dashed blue; outline-offset: 1px; } .time-picker > input[type=time] { position: absolute; display: block; left: 0px; top: 0px; width: 100%; height: 100%; opacity: 0; cursor: pointer; border: none; padding: 0px; margin: 0px; z-index: 3; } .time-picker > input[type=time]::-webkit-calendar-picker-indicator { position: absolute; left: -50%; top: 0px; width: 300%; height: 100%; color: transparent; cursor: pointer; z-index: 4; } </style> </head> <body translate="no" > <form> <div class="date-picker"> <button type="button" class="back-btn" tabindex="0" aria-label="Go backward one day"></button> <div class="date-input"><input type="text" readonly aria-hidden="true" tabindex="-1"><input type="date" role="button" tabindex="0" aria-label="choose a date with native calendar control"></div> <button type="button" class="forward-btn" tabindex="0" aria-label="Go forward one day"></button> </div> <br> <div class="time-picker"><input type="text" readonly aria-hidden="true" tabindex="-1"><input type="time" role="button" tabindex="0" aria-label="choose a time with native time control"></div> </form> <script> function DatePicker(element) { var backBtn; var dateInputEl; var displayDateEl; var nativeDateEl; var forwardBtn; backBtn = element.querySelectorAll(".back-btn")[0]; dateInputEl = element.querySelectorAll(".date-input")[0]; displayDateEl = dateInputEl.querySelectorAll("input[type=text]")[0]; nativeDateEl = dateInputEl.querySelectorAll("input[type=date]")[0]; forwardBtn = element.querySelectorAll(".forward-btn")[0]; dateInputEl.addEventListener("focus", onFocusDateControl, true); backBtn.addEventListener("click", onDateBackward, false); displayDateEl.addEventListener("focus", onFocusDateInput, false); displayDateEl.addEventListener("blur", onBlurDateInput, false); nativeDateEl.addEventListener("focus", onFocusDateInput, false); nativeDateEl.addEventListener("blur", onBlurDateInput, false); nativeDateEl.addEventListener("change", onNativeSelectDate, false); forwardBtn.addEventListener("click", onDateForward, false); syncDateElements(); // ************************************** function syncDateElements() { if (element.dataset.value) { let [curYear, curMonth, curDay] = getCurrentDate() || []; if (curYear != null) { nativeDateEl.value = formatDateStr(curYear, curMonth, curDay); displayDateEl.value = formatDisplayDate(nativeDateEl.value); } else { nativeDateEl.value = ""; displayDateEl.value = ""; } } else { element.dataset.value = ""; displayDateEl.value = ""; } if (element.dataset.min) { let [minYear, minMonth, minDay] = getMinDate() || []; if (minYear != null) { nativeDateEl.setAttribute("min", formatDateStr(minYear, minMonth, minDay)); } } if (element.dataset.max) { let [maxYear, maxMonth, maxDay] = getMaxDate() || []; if (maxYear != null) { nativeDateEl.setAttribute("max", formatDateStr(maxYear, maxMonth, maxDay)); } } updateBackwardForwardButtons(); } function updateBackwardForwardButtons() { var [year, month, day] = getCurrentDate() || []; if (year != null) { let minDate = getMinDate(); if (minDate) { backBtn.disabled = // are we at the min date for the control? compareInternalDates( [year, month, day], minDate) === 0; } let maxDate = getMaxDate(); .........完整代码请登录后点击上方下载按钮下载查看
网友评论0