移动端类似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.mzph.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…