原生js点击input弹出时间选择器

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>原生js点击input弹出时间选择器</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            display: flex;
            justify-content: center;
            align-items: center;
            height: 100vh;
            margin: 0;
            background-color: #f5f5f5;
        }
        .container {
            background-color: white;
            padding: 30px;
            border-radius: 8px;
            box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
            text-align: center;
        }
        h1 {
            color: #333;
            margin-bottom: 20px;
        }
        .date-input {
            padding: 10px 15px;
            font-size: 16px;
            border: 1px solid #ddd;
            border-radius: 4px;
            width: 200px;
            text-align: center;
            cursor: pointer;
        }
        .date-input:focus {
            outline: none;
            border-color: #4CAF50;
        }
        .date-picker {
            position: absolute;
            background-color: white;
            border: 1px solid #ddd;
            border-radius: 4px;
            padding: 10px;
            box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
            z-index: 100;
            display: none;
        }
        .date-picker-header {
            display: flex;
            justify-content: space-between;
            align-items: center;
            margin-bottom: 10px;
        }
        .date-picker-title {
            font-weight: bold;
        }
        .date-picker-nav {
            display: flex;
            gap: 10px;
        }
        .date-picker-nav button {
            background: none;
            border: none;
            cursor: pointer;
            font-size: 16px;
        }
        .date-picker-weekdays {
            display: grid;
            grid-template-columns: repeat(7, 1fr);
            margin-bottom: 5px;
        }
        .date-picker-weekday {
            text-align: center;
            font-weight: bold;
            font-size: 12px;
            color: #666;
        }
        .date-picker-days {
            display: grid;
            grid-template-columns: repeat(7, 1fr);
            gap: 5px;
        }
        .date-picker-day {
            padding: 5px;
            text-align: center;
            cursor: pointer;
            border-radius: 3px;
        }
        .date-picker-day:hover {
            background-color: #f0f0f0;
        }
        .date-picker-day.selected {
            background-color: #4CAF50;
            color: white;
        }
        .date-picker-day.other-month {
            color: #ccc;
        }
        .date-picker-day.today {
            font-weight: bold;
            color: #4CAF50;
        }
   
    </style>
</head>
<body>
    <div class="container">
        <h1>Date Picker Demo</h1>
        <input type="text" class="date-input" id="dateInput" readonly placeholder="Select a date">
        
        <div class="date-picker" id="datePicker">
            <div class="date-picker-header">
                <div class="date-picker-title" id="datePickerTitle">June 2023</div>
                <div class="date-picker-nav">
                    <button id="prevMonth">&lt;</button>
                    <button id="nextMonth">&gt;</button>
                </div>
            </div>
            <div class="date-picker-weekdays">
                <div class="date-picker-weekday">Sun</div>
                <div class="date-picker-weekday">Mon</div>
                <div class="date-picker-weekday">Tue</div>
                <div class="date-picker-weekday">Wed</div>
                <div class="date-picker-weekday">Thu</div>
                <div class="date-picker-weekday">Fri</div>
                <div class="date-picker-weekday">Sat</div>
            </div>
            <div class="date-picker-days" id="datePickerDays"></div>
        </div>
    </div>

    <script>
        document.addEventListener('DOMContentLoaded', function() {
            const dateInput = document.getElementById('dateInput');
            const datePicker = document.getElementById('datePicker');
            const datePickerTitle = document.getElementById('datePickerTitle');
            const datePickerDays = document.getElementById('datePickerDays');
            const prevMonthBtn = document.getElementById('prevMonth');
            const nextMonthBtn = document.getElementById('nextMonth');
            
            let currentDate = new Date();
            let selectedDate = null;
            
            // Initialize the date picker
            renderCalendar(currentDate);
            
            // Show date picker when input is clicked
            dateInput.addEventListener('click', function(e) {
                e.stopPropagation();
                positionDatePicker();
                datePicker.style.display = 'block';
            });
            
            // Hide date picker when clicking outside
            document.addEventListener('click', function() {
                datePicker.style.display = 'none';
            });
            
            // Prevent date picker from closing when clicking inside it
            datePicker.addEventListener('click', function(e) {
                e.stopPropagation();
            });
            
            // Navigation buttons
            prevMonthBtn.addEventListener('click', function() {
                currentDate.setMonth(currentDate.getMonth() - 1);
                renderCalendar(currentDate);
            });
            
            nextMonthBtn.addEventListener('click', function() {
                currentDate.setMonth(currentDate.getMonth() + 1);
                renderCalendar(currentDate);
            });
            
            // Position the date picker below the input
            function positionDatePicker() {
                const inputRect = dateInput.getBoundingClientRect();
                datePicker.style.top = (inputRect.bottom + window.scrollY + 5) + 'px';
                datePicker.style.left = (inputRect.left + window.scrollX) + 'px';
            }
            
            // Render the calendar for the given month
            function renderCalendar(date) {
                const year = date.getFullYear();
                const month = date.getMonth();
                
                // Update the title
                datePickerTitle.textContent = new Intl.DateTimeFormat('en-US', { 
                    month: 'long', 
                    year: 'numeric' 
                }).format(date);
                
                // Clear previous days
                datePickerDays.innerHTML = '';
                
                // Get first day of month and last day of month
                const firstDay = new Date(year, month, 1);
                const lastDay = new Date(year, month + 1, 0);
                
                // Get days from previous month to show
                const prevMonthDays = firstDay.getDay();
                
                // Get days from next month to show
                const nextMonthDays = 6 - lastDay.getDay();
                
                // Get total days to show (prev + current + next)
                const totalDays = prevMonthDays + lastDay.getDate() + nextMonthDays;
                
                // Create day elements
                for (let i = 0; i < totalDays; i++) {
                    const dayElement = document.createElement('div');
                    dayElement.classList.add('date-picker-day');
                    
                    let day, isCurrentMonth, isToday, isSelected;
                    
                    if (i < prevMonthDays) {
                        // Previous month
                        const prevDate = new Date(year, month, 0);
                        prevDate.setDate(prevDate.getDate() - (prevMonthDays - i - 1));
                        day = prevDate.getDate();
                        isCurrentMonth = false;
                        dayElement.classList.add('other-month');
                    } else if (i >= prevMonthDays && i < prevMonthDays + lastDay.getDate()) {
                        // Current month
                        day = i - prevMonthDays + 1;
                        isCurrentMonth = true;
                        
                        // Check if today
                        const today = new Date();
                        isToday = day === today.getDate() && 
                                  month === today.getMonth() && 
                                  year === today.getFullYear();
                                  
                        if (isToday) {
                            dayElement.classList.add('today');
                        }
                        
                        // Check if selected
                        if (selectedDate) {
                            isSelected = day === selectedDate.getDate() && 
                                         month === selectedDate.getMonth() && 
                                         year === selectedDate.getFullYear();
                                         
                            if (isSelected) {
                                dayElement.classList.add('selected');
                            }
                        }
                    } else {
                        // Next month
                        const nextDate = new Date(year, month + 1, 1);
                        nextDate.setDate(i - prevMonthDays - lastDay.getDate() + 1);
                        day = nextDate.getDate();
                        isCurrentMonth = false;
                        dayElement.classList.add('other-month');
                    }
                    
                    dayElement.textContent = day;
                    
                    if (isCurrentMonth) {
                        dayElement.addEventListener('click', function() {
                            // Update selected date
                            selectedDate = new Date(year, month, day);
                            
                            // Update input value
                            dateInput.value = new Intl.DateTimeFormat('en-US', {
                                year: 'numeric',
                                month: '2-digit',
                                day: '2-digit'
                            }).format(selectedDate);
                            
                            // Re-render calendar to show selection
                            renderCalendar(currentDate);
                            
                            // Hide the picker
                            datePicker.style.display = 'none';
                        });
                    }
                    
                    datePickerDays.appendChild(dayElement);
                }
            }
            
            // Handle window resize
            window.addEventListener('resize', function() {
                if (datePicker.style.display === 'block') {
                    positionDatePicker();
                }
            });
        });
    </script>
</body>
</html>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值