<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no">
<title>移动端触屏滑动日期控件</title>
<style>
* {
margin: 0;
padding: 0;
box-sizing: border-box;
-webkit-tap-highlight-color: transparent;
}
body {
font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
background: #f5f5f5;
color: #333;
padding: 20px;
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
min-height: 100vh;
}
.date-picker-container {
width: 100%;
max-width: 400px;
background: #fff;
border-radius: 12px;
box-shadow: 0 5px 15px rgba(0, 0, 0, 0.1);
overflow: hidden;
margin-bottom: 30px;
}
.date-picker-header {
background: linear-gradient(135deg, #6a11cb 0%, #2575fc 100%);
color: white;
padding: 20px;
text-align: center;
}
.date-picker-header .month-year {
font-size: 18px;
font-weight: 500;
margin-bottom: 5px;
}
.date-picker-header .selected-date {
font-size: 28px;
font-weight: bold;
}
.date-picker-body {
padding: 15px;
}
.weekdays {
display: flex;
justify-content: space-around;
margin-bottom: 10px;
font-size: 14px;
color: #999;
}
.weekday {
width: 14.28%;
text-align: center;
}
.days-container {
position: relative;
height: 250px;
overflow: hidden;
}
.days {
display: flex;
flex-wrap: wrap;
transition: transform 0.3s ease;
}
.day {
width: 14.28%;
height: 50px;
display: flex;
align-items: center;
justify-content: center;
font-size: 16px;
cursor: pointer;
position: relative;
transition: all 0.2s;
}
.day:not(.disabled):active {
transform: scale(0.9);
}
.day.today {
color: #2575fc;
font-weight: bold;
}
.day.selected {
background: #2575fc;
color: white;
border-radius: 50%;
}
.day.disabled {
color: #ddd;
pointer-events: none;
}
.footer {
margin-top: 30px;
text-align: center;
color: #666;
font-size: 14px;
}
.footer a {
color: #2575fc;
text-decoration: none;
transition: color 0.3s;
}
.footer a:hover {
color: #6a11cb;
text-decoration: underline;
}
</style>
</head>
<body>
<div class="date-picker-container">
<div class="date-picker-header">
<div class="month-year" id="month-year">2023年11月</div>
<div class="selected-date" id="selected-date">15日</div>
</div>
<div class="date-picker-body">
<div class="weekdays">
<div class="weekday">日</div>
<div class="weekday">一</div>
<div class="weekday">二</div>
<div class="weekday">三</div>
<div class="weekday">四</div>
<div class="weekday">五</div>
<div class="weekday">六</div>
</div>
<div class="days-container">
<div class="days" id="days"></div>
</div>
</div>
</div>
<div class="footer">
</div>
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery-touch-events/2.0.3/jquery.mobile-events.min.js"></script>
<script>
$(document).ready(function() {
let currentDate = new Date();
let selectedDate = new Date();
let touchStartX = 0;
let touchEndX = 0;
// 初始化日期选择器
function initDatePicker() {
updateMonthYear();
renderCalendar();
}
// 更新月份年份显示
function updateMonthYear() {
const options = { year: 'numeric', month: 'long' };
$('#month-year').text(currentDate.toLocaleDateString('zh-CN', options));
$('#selected-date').text(selectedDate.getDate() + '日');
}
// 渲染日历
function renderCalendar() {
const daysContainer = $('#days');
daysContainer.empty();
const year = currentDate.getFullYear();
const month = currentDate.getMonth();
const firstDay = new Date(year, month, 1);
const lastDay = new Date(year, month + 1, 0);
const daysInMonth = lastDay.getDate();
const startingDay = firstDay.getDay();
// 上个月的最后几天
const prevMonthLastDay = new Date(year, month, 0).getDate();
for (let i = 0; i < startingDay; i++) {
daysContainer.append(`<div class="day disabled">${prevMonthLastDay - startingDay + i + 1}</div>`);
}
// 当前月的日期
const today = new Date();
for (let i = 1; i <= daysInMonth; i++) {
const dayElement = $(`<div class="day">${i}</div>`);
const date = new Date(year, month, i);
// 标记今天
if (date.toDateString() === today.toDateString()) {
dayElement.addClass('today');
}
// 标记已选日期
if (date.toDateString() === selectedDate.toDateString()) {
dayElement.addClass('selected');
}
// 点击选择日期
dayElement.on('tap click', function() {
selectedDate = new Date(year, month, i);
updateMonthYear();
renderCalendar();
});
daysContainer.append(dayElement);
}
// 下个月的前几天
const daysToShow = 42 - (startingDay + daysInMonth);
for (let i = 1; i <= daysToShow; i++) {
daysContainer.append(`<div class="day disabled">${i}</div>`);
}
}
// 滑动切换月份
$('.days-container').on('touchstart', function(e) {
touchStartX = e.originalEvent.touches[0].clientX;
});
$('.days-container').on('touchend', function(e) {
touchEndX = e.originalEvent.changedTouches[0].clientX;
handleSwipe();
});
function handleSwipe() {
const threshold = 50;
const difference = touchStartX - touchEndX;
if (difference > threshold) {
// 向左滑动 - 下个月
currentDate.setMonth(currentDate.getMonth() + 1);
initDatePicker();
} else if (difference < -threshold) {
// 向右滑动 - 上个月
currentDate.setMonth(currentDate.getMonth() - 1);
initDatePicker();
}
}
// 初始化
initDatePicker();
});
</script>
</body>
</html>
1567

被折叠的 条评论
为什么被折叠?



