原生html+js+jq+less 實現時間區間下拉彈窗選擇器

?

html彈窗

  <div class="popupForm" id="popupForm10"><div class="pop-box"><i class="iconfont icon-quxiao cancel" onclick="toggleForm(10)"></i><div class="title">選擇時間</div><div class="time-box"><div class="time"><!-- <span class="active">2025-7-28</span> --><span>開始時間</span>至<span>結束時間</span></div><div class="time-list flex-sb"><!-- 年 --><div class="year"><div class="li"></div><div class="li"></div><div class="li">2025年</div><div class="li">2024年</div><div class="li">2023年</div><div class="li">2022年</div><div class="li">2021年</div><div class="li">2020年</div><div class="li"></div><div class="li"></div></div><!-- 月 --><div class="month"><div class="li"></div><div class="li"></div><div class="li">1月</div><div class="li">2月</div><div class="li">3月</div><div class="li">4月</div><div class="li">5月</div><div class="li">6月</div><div class="li">7月</div><div class="li">8月</div><div class="li">9月</div><div class="li">10月</div><div class="li">11月</div><div class="li">12月</div><div class="li"></div><div class="li"></div></div><!-- 日 --><div class="day"><div class="li"></div><div class="li"></div><div class="li">1日</div><div class="li">2日</div><div class="li">3日</div><div class="li">4日</div><div class="li">5日</div><div class="li">6日</div><div class="li">7日</div><div class="li">8日</div><div class="li">9日</div><div class="li">10日</div><div class="li">11日</div><div class="li">12日</div><div class="li">13日</div><div class="li">14日</div><div class="li">15日</div><div class="li">16日</div><div class="li">17日</div><div class="li">18日</div><div class="li">19日</div><div class="li">20日</div><div class="li">21日</div><div class="li">22日</div><div class="li">23日</div><div class="li">24日</div><div class="li">25日</div><div class="li">26日</div><div class="li">27日</div><div class="li">28日</div><div class="li">29日</div><div class="li">30日</div><div class="li"></div><div class="li"></div></div></div><div class="bottom flex-cc"><button class="reset" onclick="resetTime()">重置</button><button class="confirm" onclick="confirmTime()">確定</button></div></div></div></div>

?less樣式

.popupForm {display: none;width: 100%;height: 100%;position: fixed;bottom: 0;left: 0;background-color: #57575748;.pop-box {position: absolute;bottom: 0;left: 0;height: auto;width: 100%;border-radius: 0.625rem 0.625rem 0rem 0rem;background: #ffffff;padding: 1.25rem 0;}// 取消.cancel {font-size: 0.75rem;color: #606266;position: absolute;top: 1rem;right: 1.375rem;}.title {font-size: 1rem;color: #3d3d3d;text-align: center;}// 時間選擇器.time-box {padding: 0.625rem 0;.time {font-size: 1rem;color: #606266;text-align: center;span {margin: 0 1.6875rem;padding: 0.1875rem 0.625rem;border-bottom: 0.0625rem solid #d8d8d8;font-size: 0.875rem;color: #909399;}.active {color: #1890ff;}}.time-list {height: 12.5rem;margin: 1.875rem 0;position: relative;box-shadow: inset 0 0.3125rem 0.625rem #d5d5d52e,inset 0 -0.3125rem 0.625rem #d5d5d52e;&::after {content: "";position: absolute;top: 5rem;left: 0;width: 100%;height: 0.0625rem;background-color: #eaeaea;}&::before {content: "";position: absolute;bottom: 5rem;left: 0;width: 100%;height: 0.0625rem;background-color: #eaeaea;}& > div {flex: 1;text-align: center;overflow-y: auto;&::-webkit-scrollbar {width: 0;height: 0;}.li {height: 2.5rem;font-size: 0.875rem;color: #606266;padding: 0.625rem 0;}}}.bottom {gap: 2.5rem;.reset {width: 7.875rem;height: 1.6875rem;border-radius: 2.125rem;border: 0.0625rem solid #c4302c;font-size: 0.875rem;color: #c4302c;}.confirm {width: 7.875rem;height: 1.6875rem;border-radius: 2.1875rem;background: linear-gradient(180deg, #eb140f 0%, #c70504 100%);font-size: 0.875rem;color: #ffffff;}}}
}

js整體邏輯?

$(() => {// 當前年份const thisYear = new Date().getFullYear();const ITEM_HEIGHT = 40; // 每個選項高度// 滾動類型:0=開始時間,1=結束時間let type = 0;let startTime = ""; //開始時間let endTime = ""; //結束時間// 存儲滾動下標const scrollValues = {startYear: 0,startMonth: 1,startDay: 1,endYear: 0,endMonth: 1,endDay: 1,};// 緩存 DOM 元素const $year = $(".time-list .year");const $month = $(".time-list .month");const $day = $(".time-list .day");const $timeSpans = $(".time-box .time span");// ========== 生成年份列表(100年)==========function renderYearList() {let html = '<div class="li"></div><div class="li"></div>';for (let i = 0; i < 100; i++) {html += `<div class="li">${thisYear - i}年</div>`;}html += '<div class="li"></div><div class="li"></div>';$year.html(html);}// ========== 生成月份列表(1~12月)==========function renderMonthList() {let html = '<div class="li"></div><div class="li"></div>';for (let i = 1; i <= 12; i++) {html += `<div class="li">${i}月</div>`;}html += '<div class="li"></div><div class="li"></div>';$month.html(html);}// ========== 獲取某年某月的天數(month: 1~12)==========function getDaysInMonth(year, month) {return new Date(year, month, 0).getDate();}// ========== 更新日列表(根據當前年月動態生成)==========function updateDayList() {const yearOffset =type === 0 ? scrollValues.startYear : scrollValues.endYear;const month = type === 0 ? scrollValues.startMonth : scrollValues.endMonth;const year = thisYear - yearOffset;const days = getDaysInMonth(year, month);const dayKey = type === 0 ? "startDay" : "endDay";const currentDay = scrollValues[dayKey];// 如果當前日超出該月最大天數,修正為最后一天if (currentDay > days) {scrollValues[dayKey] = days;}// 生成日 HTMLlet html = '<div class="li"></div><div class="li"></div>';for (let i = 1; i <= days; i++) {html += `<div class="li">${i}日</div>`;}html += '<div class="li"></div><div class="li"></div>';$day.html(html);// 滾動到當前日$day.scrollTop((scrollValues[dayKey] - 1) * ITEM_HEIGHT);}// ========== 格式化為 YYYY-MM-DD 字符串 ==========function formatTime(yearOffset, month, day) {const y = thisYear - yearOffset;const m = month < 10 ? `0${month}` : month;const d = day < 10 ? `0${day}` : day;return `${y}-${m}-${d}`;}// ========== 更新顯示文本 ==========function updateDisplay() {const isStart = type === 0;const prefix = isStart ? "start" : "end";const year = scrollValues[prefix + "Year"];const month = scrollValues[prefix + "Month"];const day = scrollValues[prefix + "Day"];const formatted = formatTime(year, month, day);isStart ? (startTime = formatted) : (endTime = formatted); // 更新時間$timeSpans.eq(type).addClass("active").text(formatted);}// ========== 初始化滾動事件 ==========function initWheel() {[$year, $month, $day].forEach(($container) => {let isAnimating = false;$container.on("wheel", function (e) {e.preventDefault();if (isAnimating) return;const delta = e.originalEvent.deltaY > 0 ? 1 : -1;const current = $container.scrollTop();const maxScroll = this.scrollHeight - this.clientHeight;const nextScroll = Math.max(0,Math.min(maxScroll, current + delta * ITEM_HEIGHT));isAnimating = true;$container.animate({ scrollTop: nextScroll }, 200, () => {isAnimating = false;});// 計算滾動索引const index = Math.round(nextScroll / ITEM_HEIGHT);// 更新對應值if ($container.is($year)) {scrollValues[`${type === 0 ? "start" : "end"}Year`] = index;} else if ($container.is($month)) {scrollValues[`${type === 0 ? "start" : "end"}Month`] = index + 1;} else if ($container.is($day)) {scrollValues[`${type === 0 ? "start" : "end"}Day`] = index + 1;}updateDisplay(); // 更新顯示updateDayList(); // 重新生成日(月或年變化時)});});}// ========== 切換時間類型(點擊“開始/結束”)==========$timeSpans.parent().on("click", "span", function () {type = $(this).index();const values = [scrollValues[`${type === 0 ? "start" : "end"}Year`],scrollValues[`${type === 0 ? "start" : "end"}Month`] - 1,scrollValues[`${type === 0 ? "start" : "end"}Day`] - 1,];$year.scrollTop(values[0] * ITEM_HEIGHT);$month.scrollTop(values[1] * ITEM_HEIGHT);$day.scrollTop(values[2] * ITEM_HEIGHT);});// ========== 初始化 ==========$(function () {renderYearList();renderMonthList();updateDayList(); // 初始生成日initWheel();// 默認顯示當前日期(可選)// $timeSpans.eq(0).text(formatTime(0, 1, 1));// $timeSpans.eq(1).text(formatTime(0, 1, 1));});// ========== 確認時間 ==========window.confirmTime = function () {// 判斷開始結束時間是否正確console.log(startTime, endTime);if (startTime && endTime && startTime > endTime) {// message.error("結束時間不能小于開始時間");} else if (!startTime && endTime) {// message.error("請選擇開始時間");} else if (startTime && !endTime) {// message.error("請選擇結束時間");} else {// 關閉彈窗 請求接口toggleForm(10);}};// ========== 重置時間 ==========window.resetTime = function () {$(".time-box .time").html("<span>開始時間</span>至<span>結束時間</span>");Object.assign(scrollValues, {startYear: 0,startMonth: 1,startDay: 1,endYear: 0,endMonth: 1,endDay: 1,});$(".time-box .time span").eq(0).click(); // 觸發切換并滾動};
});

打開關閉彈窗?

function toggleForm(type) {// console.log(type);$(`#popupForm${type}`).fadeToggle(200);
}

?

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/bicheng/91406.shtml
繁體地址,請注明出處:http://hk.pswp.cn/bicheng/91406.shtml
英文地址,請注明出處:http://en.pswp.cn/bicheng/91406.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

基于邏輯回歸、隨機森林、梯度提升樹、XGBoost的廣告點擊預測模型的研究實現

文章目錄有需要本項目的代碼或文檔以及全部資源&#xff0c;或者部署調試可以私信博主一、項目背景與目標二、數據概覽與預處理2.1 數據導入與初步分析2.2 缺失值與重復值處理2.3 目標變量分布三、探索性數據分析&#xff08;EDA&#xff09;3.1 數值變量分布3.2 類別變量分布3…

Docker學習相關視頻筆記(三)

參考視頻地址&#xff1a;40分鐘的Docker實戰攻略&#xff0c;一期視頻精通Docker。感謝作者的辛苦付出。 本文是Docker學習相關視頻筆記&#xff08;一&#xff09;與Docker學習相關視頻筆記&#xff08;二&#xff09;的后續 4、Docker命令 4.8 Docker 網絡 4.8.1 橋接模式…

RK3568筆記九十五:基于FFmpeg和Qt實現簡易視頻播放器

若該文為原創文章,轉載請注明原文出處。 一、開發環境 1、硬件:正點原子ATK-DLRK3568 2、QT: 5.14.2 3、系統: buildroot 二、實現功能 使用ffmpeg音視頻庫軟解碼實現視頻播放器 支持打開多種本地視頻文件(如mp4,mov,avi等) 視頻播放支持實時開始,暫停,繼續播放 采…

【LLM】Kimi-K2模型架構(MuonClip 優化器等)

note Kimi K2 的預訓練階段使用 MuonClip 優化器實現萬億參數模型的穩定高效訓練&#xff0c;在人類高質量數據成為瓶頸的背景下&#xff0c;有效提高 Token 利用效率。MuonClip Optimizer優化器&#xff0c;解決隨著scaling up時的不穩定性。Kimi-K2 與 DeepSeek-R1 架構對比…

Vue基礎(25)_組件與Vue的內置關系(原型鏈)

了解組件與Vue的內置關系前&#xff0c;我們需要回顧js原型鏈基礎知識&#xff1a;1、構造函數構造函數是一種特殊的方法&#xff0c;用于創建和初始化一個新的對象。它們是使用 new 關鍵字和函數調用來創建對象的。構造函數實際上只是一個普通的函數&#xff0c;通常以大寫字母…

kafka中生產者的數據分發策略

在 Kafka 中&#xff0c;生產者的數據分發策略決定了消息如何分配到主題的不同分區。在 Python 中&#xff0c;我們通常使用 kafka-python 庫來操作 Kafka&#xff0c;下面詳細講解其數據分發策略及實現代碼。一、Kafka 生產者數據分發核心概念分區&#xff08;Partition&#…

【動態規劃算法】斐波那契數列模型

一. (1137.)第N個泰波那契數(力扣)1.1動態規劃的算法流程 對于初學者來講學術上的概念晦澀難懂,將用通俗易懂的方式帶來感性的理解. 1.狀態表示dp表(一維或二維數組)里面的值所表示的含義 從哪獲取? 1.題目要求,如本題 2.題目沒有明確說明的情況下做題經驗的累積 3.分析問題的…

Odoo 18 PWA 全面掌握:從架構、實現到高級定制

本文旨在對 Odoo 18 中的漸進式網絡應用&#xff08;Progressive Web App, PWA&#xff09;技術進行一次全面而深入的剖析。本文的目標讀者為 Odoo 技術顧問、高級開發人員及解決方案架構師&#xff0c;旨在提供一份權威的技術參考&#xff0c;以指導 PWA 相關的實施項目與戰略…

Binary Classifier Optimization for Large Language Model Alignment

2025.acl-long.93.pdfhttps://aclanthology.org/2025.acl-long.93.pdf 1. 概述 在生產環境中部署大型語言模型(LLMs)時,對齊LLMs一直是一個關鍵因素,因為預訓練的LLMs容易產生不良輸出。Ouyang等人(2022)引入了基于人類反饋的強化學習(RLHF),該方法涉及基于單個提示的…

在CentOS上以源碼編譯的方式安裝PostgreSQL

下載目錄&#xff1a;PostgreSQL: File Browser&#xff0c;我使用的PostgreSQLv17.5。Linux系統&#xff1a;CentOS Linux release 7.9.2009 (Core) 安裝依賴包和工具鏈&#xff08;必須且重要&#xff01;&#xff09; yum groupinstall "Development Tools" -y yu…

Baumer工業相機堡盟工業相機如何通過YoloV8深度學習模型實現沙灘小人檢測識別(C#代碼UI界面版)

Baumer工業相機堡盟工業相機如何通過YoloV8深度學習模型實現沙灘小人檢測識別&#xff08;C#代碼UI界面版&#xff09;工業相機使用YoloV8模型實現沙灘小人檢測識別工業相機通過YoloV8模型實現沙灘小人檢測識別的技術背景在相機SDK中獲取圖像轉換圖像的代碼分析工業相機圖像轉換…

Ubuntu服務器安裝與運維手冊——操作純享版

本手冊匯總了從硬件預配置、Ubuntu 安裝、網絡與服務配置&#xff0c;到 Windows/macOS 訪問共享、MySQL 初始化的完整流程&#xff0c;便于今后運維參考。 目錄 環境與硬件概覽BIOS/UEFI 設置制作與啟動安裝介質Ubuntu 24.04 LTS 安裝流程靜態 IP 配置&#xff08;netplan&am…

【Nginx】Nginx進階指南:解鎖代理與負載均衡的多樣玩法

在Web服務的世界里&#xff0c;Nginx就像是一位多面手&#xff0c;它不僅能作為高性能的Web服務器&#xff0c;還能輕松勝任代理服務器、負載均衡器等多種角色。今天&#xff0c;我們就來深入探索Nginx的幾個常見應用場景&#xff0c;通過實際案例和關鍵配置解析&#xff0c;帶…

原創-銳能微82xx系列電能計量芯片軟件驅動開發與精度校準流程完全指南

引言 電能計量芯片的軟件驅動開發是整個計量系統的核心&#xff0c;它直接決定了計量精度、系統穩定性和功能完整性。銳能微82xx系列電能計量芯片憑借其強大的數字信號處理能力和豐富的功能特性&#xff0c;為開發者提供了靈活的軟件開發平臺。本文將詳細介紹82xx系列芯片的軟…

如何使用 Apache Ignite 作為 Spring 框架的緩存(Spring Cache)后端

這份文檔是關于 如何使用 Apache Ignite 作為 Spring 框架的緩存&#xff08;Spring Cache&#xff09;后端&#xff0c;實現方法級別的緩存功能。 這和前面我們講的 Spring Data Ignite 是兩個不同的概念。我們先明確區別&#xff0c;再深入理解。&#x1f501; 一、核心區別…

Android 超大圖片、長圖分割加載

在Android開發中&#xff0c;處理大圖片的加載是一個常見且重要的問題&#xff0c;尤其是在需要顯示高分辨率圖片時。大圖片如果不正確處理&#xff0c;可能會導致內存溢出或應用性能下降。下面是一些常用的策略和技術來優化大圖片的加載&#xff1a;1. 使用圖片壓縮庫a. Glide…

Linux:理解操作系統

文章目錄數據流動操作系統數據流動 軟件運行&#xff0c;必須先加載到內存&#xff0c;本質要把磁盤上的文件 加載到內存。 我們寫的算法是處理存儲器里面的數據&#xff0c;數據就是文件&#xff0c;我們自己寫的可執行文件。 圖中QQ就是軟件&#xff0c;加載內存后進行下一步…

【每日一錯】PostgreSQL的WAL默認段大小

文章目錄題目擴展學習WAL工作原理流程圖題目 擴展學習 WAL&#xff08;Write Ahead Log&#xff09;預寫日志&#xff1a; WAL是PostgreSQL先寫日志、后寫數據的機制&#xff0c;用來防止數據丟失、提升數據恢復能力。 流程&#xff1a; 事務先寫日志文件&#xff08;WAL&…

Visual Studio Code 使用指南 (2025年版)

Visual Studio Code (VS Code) 是一款由微軟開發的免費、開源、跨平臺的現代化輕量級代碼編輯器&#xff0c;憑借其強大的核心功能、豐富的擴展生態系統以及高度可定制性&#xff0c;已成為全球數百萬開發者的首選工具。本指南旨在幫助您快速上手 VS Code&#xff0c;掌握其核心…

【Java】JVM虛擬機(java內存模型、GC垃圾回收)

一、Java內存模型&#xff08;JMM&#xff09;JMM&#xff08;Java Memory Model&#xff0c;Java 內存模型&#xff09;是 Java 虛擬機規范中定義的一種抽象概念&#xff0c;用于規范 Java 程序中多線程對共享內存的訪問規則&#xff0c;解決可見性、原子性和有序性問題&#…