移動端類似IOS的滾動年月控件(需要jQuery和iScroll)

http://www.cnblogs.com/ccblogs/p/5260949.html

一. 效果圖

?

二. 功能介紹

  支持滾動和點擊選擇年月。(目前只支持設置年月的最大最小值,不支持整體的最大最小值)

?

三. 代碼

  1. 在你的html中添加如下代碼:

    直接加載<body>里面,這里是插件渲染html的地方。

<div id="datePlugin"></div>

  2. 在你的html中添加輸入框:

    可以是直接的一個輸入框,也可以是input-group樣式的。

    我這里使用的時input-group,html是由JS加載的。

'<div class="item item-buydate input-group">' +
'<span class="input-group-span no-border-right" id="buydate-span">申購成交時間</span>' +
'<input class="txt-input txt-buydate no-border-left text-right" type="text" placeholder="請選擇申購日期" readonly>' +
'</div>';

  3. 調用方法:

$('.item-buydate').date({ title: '申購成交時間' });

  4. JS源代碼:

復制代碼
(function($) {$.fn.date = function(options) {var that = $(this);var docType = $(this).is('input');var nowdate = new Date();var indexY = 1,indexM = 1;var initY = parseInt((nowdate.getYear() + '').substr(1, 2));var initM = parseInt(nowdate.getMonth() + '') + 1;var yearScroll = null,monthScroll = null;$.fn.date.defaultOptions = {title: '請選擇年月',beginyear: 2000, //日期--年--份開始endyear: nowdate.getFullYear(), //日期--年--份結束beginmonth: 1, //日期--月--份結束endmonth: 12, //日期--月--份結束curdate: false, //打開日期是否定位到當前日期mode: null, //操作模式(滑動模式)event: "click", //打開日期插件默認方式為點擊后后彈出日期isShowByDefault: false,isSetFinancialDefaultDateValue: false}var opts = $.extend(true, {}, $.fn.date.defaultOptions, options);if (opts.isSetFinancialDefaultDateValue) {if (opts.beginyear < opts.endyear) {initY = ((opts.endyear - 1) + '').substr(2, 2);} else if (opts.beginyear = opts.endyear) {initY = (opts.endyear + '').substr(2, 2);}}if (opts.isShowByDefault) {showDatePicker()}that.bind(opts.event, showDatePicker);function showDatePicker() {createUL();init_iScrll();extendOptions();that.blur();refreshDate();bindButton();}function refreshDate() {yearScroll.refresh();monthScroll.refresh();resetInitDete();yearScroll.scrollTo(0, initY * 40, 100, true);monthScroll.scrollTo(0, initM * 40 - 40, 100, true);}function resetIndex() {indexY = 1;indexM = 1;}function resetInitDete() {if (opts.curdate) {return false;} else if (that.val() === '') {if (that.children('input').val() === '') {return false;}initY = parseInt(that.children('input').val().substr(2, 2));initM = parseInt(that.children('input').val().substr(5, 2));} else {initY = parseInt(that.val().substr(2, 2));initM = parseInt(that.val().substr(5, 2));}}function bindButton() {resetIndex();$("#yearwrapper ul li").unbind('click').click(function() {if ($(this).hasClass("placeholder")) {return false;}var target = $(this).prev('li');yearScroll.scrollToElement(target[0]);indexY = $(this).attr('data-params');$("#dateconfirm").removeClass("disabled");});$("#monthwrapper ul li").unbind('click').click(function() {if ($(this).hasClass("placeholder")) {return false;}var target = $(this).prev('li');monthScroll.scrollToElement(target[0]);indexM = $(this).attr('data-params');$("#dateconfirm").removeClass("disabled");});$("#dateshadow").unbind('click').click(function() {$("#datePage").hide();$("#dateshadow").hide();});$("#dateconfirm").unbind('click').click(function() {if ($(this).hasClass('disabled')) {return false;}if (indexY !== undefined && indexY !== '') {indexY = parseInt(parseFloat(indexY).toFixed(0));}if (indexM !== undefined && indexM !== '') {indexM = parseInt(parseFloat(indexM).toFixed(0));}var datestr = $("#yearwrapper ul li:eq(" + indexY + ")").html().substr(0, $("#yearwrapper ul li:eq(" + indexY + ")").html().length - 1) + "-" +$("#monthwrapper ul li:eq(" + indexM + ")").html().substr(0, $("#monthwrapper ul li:eq(" + indexM + ")").html().length - 1);if (docType) {that.val(datestr);that.trigger('input');} else {that.children('input').val(datestr);that.children('input').trigger('input');}$("#datePage").hide();$("#dateshadow").hide();});$("#datecancle").click(function() {$("#datePage").hide();$("#dateshadow").hide();});}function extendOptions() {$("#datePage").show();$("#dateshadow").show();}//日期滑動function init_iScrll() {var oldIndexY = parseInt(indexY.toFixed(0));var oldIndexM = parseInt(indexM.toFixed(0));var strY = $("#yearwrapper ul li:eq(" + oldIndexY + ")").html().substr(0, $("#yearwrapper ul li:eq(" + oldIndexY + ")").html().length - 1);var strM = $("#monthwrapper ul li:eq(" + oldIndexM + ")").html().substr(0, $("#monthwrapper ul li:eq(" + oldIndexM + ")").html().length - 1);yearScroll = new iScroll("yearwrapper", {snap: "li",vScrollbar: false,onScrollMove: function() {$("#dateconfirm").addClass("disabled");},onScrollEnd: function() {indexY = (this.y / 40) * (-1) + 1;$("#dateconfirm").removeClass("disabled");}});monthScroll = new iScroll("monthwrapper", {snap: "li",vScrollbar: false,onScrollMove: function() {$("#dateconfirm").addClass("disabled");},onScrollEnd: function() {indexM = (this.y / 40) * (-1) + 1;$("#dateconfirm").removeClass("disabled");}});}function createUL() {CreateDateUI();$("#yearwrapper ul").html(createYEAR_UL());$("#monthwrapper ul").html(createMONTH_UL());}function CreateDateUI() {var str = '<div id="dateshadow"></div>' +'<div id="datePage" class="page">' +'<section>' +'<div id="datetitle">' + opts.title + '</div>' +'<div id="datemark"></div>' +'<div id="datescroll">' +'<div id="yearwrapper">' +'<ul></ul>' +'</div>' +'<div id="monthwrapper">' +'<ul></ul>' +'</div>' +'</div>' +'</section>' +'<footer id="dateFooter">' +'<div id="setcancle">' +'<ul>' +'<li id="dateconfirm">確定</li>' +'<li id="datecancle">取消</li>' +'</ul>' +'</div>' +'</footer>' +'</div>'$("#datePlugin").html(str);}function createYEAR_UL() {var str = '<li class="placeholder">&nbsp;</li>';for (var i = opts.beginyear; i <= opts.endyear; i++) {str += '<li data-params="' + (i - opts.beginyear + 1) + '">' + i + '年</li>';}return str + '<li class="placeholder">&nbsp;</li>';}function createMONTH_UL() {var str = '<li class="placeholder">&nbsp;</li>';for (var i = opts.beginmonth; i <= opts.endmonth; i++) {if (i < 10) {j = "0" + i;} else {j = i;}str += '<li data-params="' + (i - opts.beginmonth + 1) + '">' + j + '月</li>';}return str + '<li class="placeholder">&nbsp;</li>';}}
})(jQuery);
復制代碼

?  5. CSS樣式:

復制代碼
body,
div,
ul,
li,
h1 {padding: 0;margin: 0;font-family: Microsoft YaHei, Arail, sans-serif;
}ul,
li {list-style: none;list-style-type: none;
}#dateshadow {display: none;position: absolute;width: 100%;height: 100%;top: 0;left: 0;background: #000000;opacity: 0.5;
}#datePage {border-radius: 5px;position: absolute;top: 20%;margin: 0 auto;vertical-align: middle;width: 80%;height: 238px;background: #FFFFFF;z-index: 9999999;
}#datetitle {text-align: center;color: #666666;padding: 20px 10px 12px;line-height: 18px;font-size: 18px;
}#datescroll {background: #F8F8F8;margin: 10px 18px;border: 1px solid #dddddd;border-radius: 3px;height: 120px;text-align: center;line-height: 40px;
}.page {display: none;position: absolute;top: 0;left: 0;bottom: 0;right: 0;width: 100%;height: 100%;overflow: hidden;
}#datescroll div {float: left;margin-top: 15px;
}#yearwrapper {position: absolute;margin-left: 16%;left: 0;top: 45px;bottom: 60px;width: 32%;
}#monthwrapper {position: absolute;margin-left: 28%;left: 26%;top: 45px;bottom: 60px;width: 32%;
}#yearwrapper ul li,
#monthwrapper ul li {color: #333333;font-size: 14px;
}#setcancle ul {text-align: center;line-height: 30px;
}#setcancle ul li {border-radius: 3px;float: left;width: 32%;height: 30px;list-style-type: none;font-size: 14px;
}#dateconfirm {position: absolute;background: #8e6dd1;right: 12%;color: #FFFFFF;
}#dateconfirm.disabled {background: #dbdddd!important;
}#datecancle {position: absolute;background: #dbdddd;left: 12%;color: #FFFFFF;
}#datemark {left: 10%;width: 80%;height: 30px;position: absolute;top: 104px;background: #eeeeee;
}#datescroll_datetime {display: none;background: #F8F8F8;width: 94%;margin: 10px 3%;margin-top: 10px;border: 1px solid #E0E0E0;border-radius: 4px;height: 120px;text-align: center;line-height: 40px;
}#yearwrapper ul,
#monthwrapper ul {width: 100%;
}#dateFooter {width: 100%;background: #fff;height: 44px;bottom: 0px;position: absolute;
}
復制代碼

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

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

相關文章

css 橫向滾動隱藏滾動條_使用CSS滾動時如何隱藏滾動條?

css 橫向滾動隱藏滾動條Introduction: 介紹&#xff1a; It is always nice to have a responsive website or web page, to create such websites or web pages we have to make use of our developing skills to a great extent to bring about the functionality as well a…

DrawerLayout側滑詳解

前面我們說了自定義側滑菜單&#xff0c;也說了SlidingMenu的使用&#xff0c;這一節我們再來說下DrawerLayout。DrawerLayout是Support Library包中實現了側滑菜單效果的控件&#xff0c;可以說DrawerLayout是因為第三方控件如SlidingMenu等出現之后&#xff0c;google借鑒而出…

華為筆記本計算機在哪,新一代華為MateBook 的機會在哪里?

2017年5月23日 在德國柏林&#xff0c;華為正式面向全球消費者發布了MateBook系列新品——13英寸靈動商務筆記本MateBook X、12英寸時尚二合一筆記本MateBook E及15.6英寸商務影音筆記本MateBook D。今天筆記本市場似乎是一個比較穩定的市場&#xff0c;筆記本的銷量增長不快&a…

python 字符串 變量_檢查變量是否為字符串的Python程序

python 字符串 變量Python | 檢查變量是否為字符串 (Python | Check if a variable is a string) To check whether a defined variable is a string type or not, we can use two functions which are Python library functions, 要檢查定義的變量是否為字符串類型&#xff0c…

mysql shell

mysql 查詢10分鐘以內的數據:select *from t_agent where int_last_login>CURRENT_TIMESTAMP - INTERVAL 10 MINUTE; mysql關聯多表進行update更新操作UPDATE TrackINNER JOIN MVON Track.trkidMV.mvidSET Track.is_showMV.is_showWHERE trkid<6等同于UPDATE Track,MVSET…

kaggle計算機視覺比賽技巧,9. 計算機視覺 - 9.12. 實戰Kaggle比賽:圖像分類(CIFAR-10) - 《動手學深度學習》 - 書棧網 · BookStack...

9.12. 實戰Kaggle比賽&#xff1a;圖像分類(CIFAR-10)到目前為止&#xff0c;我們一直在用Gluon的data包直接獲取NDArray格式的圖像數據集。然而&#xff0c;實際中的圖像數據集往往是以圖像文件的形式存在的。在本節中&#xff0c;我們將從原始的圖像文件開始&#xff0c;一步…

qthread中獲取當前優先級_Linux中強大的top命令

top命令算是最直觀、好用的查看服務器負載的命令了。它實時動態刷新顯示服務器狀態信息&#xff0c;且可以通過交互式命令自定義顯示內容&#xff0c;非常強大。在終端中輸入top&#xff0c;回車后會顯示如下內容&#xff1a;top - 21:48:39 up 8:57, 2 users, load average: 0…

JavaScript中帶示例的String repeat()方法

JavaScript | 字符串repeat()方法 (JavaScript | String repeat() Method) The String.repeat() method in JavaScript is used to generate a string by repeating the calling string n number of times. n can be any integer from o to any possible number in JavaScript.…

Python生成驗證碼

#!/usr/bin/env python #coding:utf8 import random #方法1&#xff1a; str_codezxcvbnmasdfghjklqwertyuiopZXCVBNMASDFGHJKLQWERTYUIOP0123456789new_codefor i in range(4):   new_coderandom.choice(str_code)print new_code #方法2&#xff1a; new_code[]def str_code…

snmp 獲得硬件信息_計算機網絡基礎課程—簡單網絡管理協議(SNMP)

簡單網絡管理協議(Simple Network Management Protocol)?除了提供網絡層服務的協議和使用那些服務的應用程序&#xff0c;因特網還需要運行一些讓管理員進行設備管理、調試問題、控制路由、監測機器狀態的軟件。這種行為稱為網絡管理。??隨著網絡技術的飛速發展&#xff0c;…

僵尸毀滅工程 服務器已停止運行,《僵尸毀滅工程》steam is not enabled錯誤解決方法...

Steam 上面的 Project Zomboid 因為帶有 VAC 所以建服開服需要 Steam服務器認證&#xff0c;這也是出現 steam is not enabled 錯誤主要原因&#xff0c;也是無法和普通零售正版所建的服務器聯機的罪魁禍首。分兩種情況(下面 Project Zomboid 均簡稱PZ)&#xff1a;1、steam版P…

spring boot 1.4默認使用 hibernate validator

spring boot 1.4默認使用 hibernate validator 5.2.4 Final實現校驗功能。hibernate validator 5.2.4 Final是JSR 349 Bean Validation 1.1的具體實現。 How to disable Hibernate validation in a Spring Boot project As [M. Deinum] mentioned in a comment on my original …

python mpi開銷_GitHub - hustpython/MPIK-Means

并行計算的K-Means聚類算法實現一&#xff0c;實驗介紹聚類是擁有相同屬性的對象或記錄的集合&#xff0c;屬于無監督學習&#xff0c;K-Means聚類算法是其中較為簡單的聚類算法之一&#xff0c;具有易理解&#xff0c;運算深度塊的特點.1.1 實驗內容通過本次課程我們將使用C語…

服務器修改開機啟動項,啟動項設置_服務器開機啟動項

最近很多觀眾老爺在苦覓關于啟動項設置的解答&#xff0c;今天欽編為大家綜合5條解答來給大家解開疑惑&#xff01; 有98%玩家認為啟動項設置_服務器開機啟動項值得一讀&#xff01;啟動項設置1.如何在bios設置硬盤為第一啟動項詳細步驟根據BIOS分類的不同操作不同&#xff1a;…

字符串查找字符出現次數_查找字符串作為子序列出現的次數

字符串查找字符出現次數Description: 描述&#xff1a; Its a popular interview question based of dynamic programming which has been already featured in Accolite, Amazon. 這是一個流行的基于動態編程的面試問題&#xff0c;已經在亞馬遜的Accolite中得到了體現。 Pr…

Ubuntu 忘記密碼的處理方法

Ubuntu系統啟動時選擇recovery mode&#xff0c;也就是恢復模式。接著選擇Drop to root shell prompt ,也就是獲取root權限。輸入命令查看用戶名 cat /etc/shadow &#xff0c;$號前面的是用戶名輸入命令&#xff1a;passwd "用戶名" 回車就可以輸入新密碼了轉載于:…

服務器mdl文件轉換,Simulink Project 中 MDL 到 SLX 模型文件格式的轉換

打開彈體示例項目并將 MDL 文件另存為 SLX運行以下命令以創建并打開“sldemo_slproject_airframe”示例的工作副本。Simulink.ModelManagement.Project.projectDemo(airframe, svn);rebuild_s_functions(no_progress_dialog);Creating sandbox for project.Created example fil…

vue 修改div寬度_Vue 組件通信方式及其應用場景總結(1.5W字)

前言相信實際項目中用過vue的同學&#xff0c;一定對vue中父子組件之間的通信并不陌生&#xff0c;vue中采用良好的數據通訊方式&#xff0c;避免組件通信帶來的困擾。今天筆者和大家一起分享vue父子組件之間的通信方式&#xff0c;優缺點&#xff0c;及其實際工作中的應用場景…

Java System類identityHashCode()方法及示例

系統類identityHashCode()方法 (System class identityHashCode() method) identityHashCode() method is available in java.lang package. identityHashCode()方法在java.lang包中可用。 identityHashCode() method is used to return the hashcode of the given object – B…

Linux中SysRq的使用(魔術鍵)

轉&#xff1a;http://www.chinaunix.net/old_jh/4/902287.html 魔術鍵&#xff1a;Linux Magic System Request Key Hacks 當Linux 系統不能正常響應用戶請求時, 可以使用SysRq小工具控制Linux. 一 SysRq的啟用與關閉 要想啟用SysRq, 需要在配置內核時設置Magic SysRq key (CO…