有时,我们需要在网页上使用软键盘。今天,就给大家带来一个基于jQuery的数字键盘插件,除了jQuery,不需要依赖任何文件资源。纯数字键盘,有退格,有清除,不支持输入小数(需要的可以自己改一下,主要是多个小数点就有13个键,不好排列了,呵呵)。支持鼠标拖动和触摸拖动,可关闭。

  在线演示

  1.页面代码 

<ul>
  <li><input type="text" placeholder="手机号码后四位" id="numkeyboard1" class="numkeyboard" /></li>
  <li><input type="text" placeholder="开箱密码" id="numkeyboard2" class="numkeyboard"/></li>
  <button type="submit" class="numkeyboard" value="querun">确&nbsp;&nbsp;认</button>
</ul>

  很简单吧,下面看看调用

$(".numkeyboard").numkeyboard({
keyboardRadix:1000,//键盘大小基数
mainbackground:'#C8BFE7', //主背景色
menubackground:'#4A81B0', //头背景色
exitbackground:'#4376A0', //关闭按钮背景色
buttonbackground:'#ff730e', //键盘背景色
clickeve:true//是否绑定元素click事件
});

  效果截图:

   

  参数说明:keyboardRadix:设置键盘大小,默认1000。当值为1000时,实际大小为600X500。keyboardRadix最小为300;clickeve:设置是否绑定元素的click事件,如果为true,则绑定click事件,即单击click时也可以达到onfocus的效果。默认只绑定元素的onfocus事件。

  为了使用简单,所以css样式控制纯由js完成,美观上就不能强求了。如果要想漂亮,建议用css控制样式,js操作样式太蛋疼了。这是我以前看到过的一个纯CSS3键盘,大家可以看看,很漂亮的,有需要的可以参照一下。源代码查看

2.js代码

 /*
* numkeyboard 1.0
* Copyright (c) 2014 BowenLuo http://www.luobo.com/
* Date: 2014-06-08
* Example:$(".numkeyboard").numkeyboard();
*/
(function($){
$.fn.numkeyboard = function(options){
var defaults = {
keyboardRadix:1000,//键盘大小基数
mainbackground:'#C8BFE7', //主背景色
menubackground:'#4A81B0', //头背景色
exitbackground:'#4376A0', //关闭按钮背景色
buttonbackground:'#ff730e', //键盘按钮背景色
clickeve:false//是否绑定元素click事件
}
var options = $.extend(defaults, options);
var keyboardRadix = options.keyboardRadix;
if(keyboardRadix<300){
keyboardRadix=300;
}
var numkeyboardcount = 0;
var activeinputele;
this.each(function(){
numkeyboardcount++;
//添加唯一的数字键盘
if(numkeyboardcount<2){
keyBoardId = randomOnlyId();
$("body").append("<div id='"+keyBoardId+"' class='auth_keybord'>"
+"<div id='auth_keybord_exit' class='auth_keybord_exit'>关闭</div>"
+"<div id='auth_keybord_menu' class='auth_keybord_menu'></div>"
+"<ul class='number_list' id='number_list'>"
+"<li><button type='button'>7</button></li>"
+"<li><button type='button'>8</button></li>"
+"<li><button type='button'>9</button></li>"
+"<li><button type='button'>4</button></li>"
+"<li><button type='button'>5</button></li>"
+"<li><button type='button'>6</button></li>"
+"<li><button type='button'>1</button></li>"
+"<li><button type='button'>2</button></li>"
+"<li><button type='button'>3</button></li>"
+"<li><button type='button'>0</button></li>"
+"<li><button type='submit'>清除</button></li>"
+"<li><button type='submit'>退格</button></li>"
+"</ul></div>");
}
//元素选择器
var inputele = $(this);
var keyboard = $("#"+keyBoardId+"");
var keyboard_exit = keyboard.children('div:first');
var keyboard_menu = keyboard.children('div:eq(1)');
var keyboard_buttonul = keyboard.find('ul:first');
var keyboard_buttonli = keyboard_buttonul.children('li');
var keyboard_button = keyboard_buttonli.children('button');
//元素css样式控制
keyboard.css({"position":"absolute","z-index":"10","display":"none","background":options.mainbackground,overflow:"hidden",
"width":keyboardRadix*0.6,"height":keyboardRadix*0.5,"border-radius":keyboardRadix*0.01});
keyboard_exit.css({"position":"absolute","z-index":"1","right":"0","background":options.exitbackground,"cursor":"pointer","text-align":"center",
"width":keyboardRadix*0.16,"height":keyboardRadix*0.081,"border-top-right-radius":keyboardRadix*0.01,"line-height":keyboardRadix*0.081+"px",
"font-family":"'微软雅黑',arial","font-size":keyboardRadix*0.036+"px","corlor":"#000","letter-spacing":keyboardRadix*0.005});
keyboard_menu.css({position:"relative",background:options.menubackground,cursor:"move",margin:"auto",
width:keyboardRadix*0.6,height:keyboardRadix*0.081,"border-top-left-radius":keyboardRadix*0.01,"border-top-right-radius":keyboardRadix*0.01});
keyboard_buttonul.css({position:"relative",margin:"auto","margin-top":keyboardRadix*0.03+"px",width:keyboardRadix*0.54,height:keyboardRadix*0.37});
keyboard_buttonli.css({position:"relative",margin:"auto",overflow:"hidden","float":"left",width:keyboardRadix*0.18,height:keyboardRadix*0.09});
var buttonborder = String(keyboardRadix*0.001+"px solid "+options.buttonbackground);
keyboard_button.css({"position":"relative","float":"left", padding: "0","cursor":"pointer","background":options.buttonbackground,"text-align":"center",
"width":keyboardRadix*0.16,"height":keyboardRadix*0.08,"border-radius":keyboardRadix*0.004,border:buttonborder,
"line-height":keyboardRadix*0.08+"px",margin:"0 0 0 "+keyboardRadix*0.01+"px",
"font-family":"'微软雅黑',arial","font-size":keyboardRadix*0.032+"px","color":"#fff"});
keyboard_button.mousedown(function(event){
$(this).css({background:"#666",top:"2px"});
event.preventDefault();
}).mouseup(function(){
$(this).css({background:options.buttonbackground,top:"0"});
}); keyboard_exit.click(function(){
exit(options.clickeve);
});
inputele.focus(function(event){
activeinputele = $(this);
var p = GetScreenPosition(this);
if(keyboard.css("display")=="none"){
keyboard.css({"display":"block","left":p.x,"top":p.y+$(this).height()*1.2});
mouseDrag();
touchDrag();
}}); if(options.clickeve){
inputele.click(function(){
activeinputele = $(this);
var p = GetScreenPosition(this);
if(keyboard.css("display")=="none"){
keyboard.css({"display":"block","left":p.x,"top":p.y+$(this).height()*1.2});
mouseDrag();
touchDrag();
}});
}
if(numkeyboardcount<2){
for(var i=0;i<keyboard_button.length;i++){
numbuttonclick(i);
}
}
function randomOnlyId(){
var tempRandom = String(Date.now());
var tempRandomLength = tempRandom.length;
tempRandom = tempRandom.substring(tempRandomLength-5,tempRandomLength-1);
var randomId = "auth_keybord"+tempRandom;
if($("#randomId").length>0){
randomOnlyId()
}else{
return randomId;
}
} function getElem(id) {
return document.getElementById(id);
} function numbuttonclick(buttonnum){
keyboard_buttonli.children('button:eq('+buttonnum+')').click(function(e){ var buttontext = keyboard_buttonli.children('button:eq('+buttonnum+')').text();
if(buttontext/1){
clickkey(buttontext/1);
}else{
if(buttontext=="0"){
clickkey(0);
}
if(buttontext=="退格"){
backspace();
}
if(buttontext=="清除"){
keyclear();
}
}
})
} function keyclear(){
activeinputele.val("");
}
function backspace(){
var inputtext = activeinputele.val();
if(inputtext.length>0){
inputtext = inputtext.substring(0,inputtext.length-1);
activeinputele.val(inputtext);
}
}
function clickkey(num){
var inputtext = activeinputele.val();
inputtext = inputtext+num;
activeinputele.val(inputtext);
} function exit(){
keyboard.css({"display":"none"});
} function GetScreenPosition(object) {
var position = {};
position.x = object.offsetLeft;
position.y = object.offsetTop;
while (object.offsetParent) {
position.x = position.x + object.offsetParent.offsetLeft;
position.y = position.y + object.offsetParent.offsetTop;
if (object == document.getElementsByTagName("body")[0]) {
break;
}
else{
object = object.offsetParent;
}
}
return position;
} function mouseDrag() {
var moveEle = keyboard;
var eventEle = keyboard_menu;
var stx = etx = curX = sty = ety = curY = 0;
var MAction = false;
var eleLeft = +moveEle.css("left").split('px')[0];
var eleTop = +moveEle.css("top").split('px')[0];
eventEle.mousedown(function(event){
MAction = true;
stx = event.pageX;
sty = event.pageY;
eleLeft = +moveEle.css("left").split('px')[0];
eleTop = +moveEle.css("top").split('px')[0];
event.preventDefault();
}).mousemove(function(event){
if(MAction){
var curX = event.pageX-stx;
var curY = event.pageY-sty;
moveEle.css({"left":eleLeft+curX,"top":eleTop+curY});
event.preventDefault();
}});
$("body").mouseup(function(event){
stx = etx = curX = sty = ety = curY = 0;
MAction = false; });
} function touchDrag() {
var moveEle = keyboard;
var eventEle = keyboard_menu;
var stx = sty = etx = ety = curX = curY = 0;
var TAction = false;
var eleLeft = +moveEle.css("left").split('px')[0];
var eleTop = +moveEle.css("top").split('px')[0]; eventEle.on("touchstart", function(event) { //touchstart
var event = event.originalEvent;
TAction = true;
curX = curY = 0;
// 元素当前位置
eleLeft = +moveEle.css("left").split('px')[0];
eleTop = +moveEle.css("top").split('px')[0];
// 手指位置
stx = event.touches[0].pageX;
sty = event.touches[0].pageY;
});
eventEle.on("touchmove", function(event) {
if(TAction){
var event = event.originalEvent;
event.preventDefault();
curX = event.touches[0].pageX - stx;
curY = event.touches[0].pageY - sty;
//alert(eleLeft+"-"+gundongX);
moveEle.css({"left":eleLeft+curX,"top":eleTop+curY});
} });
eventEle.on("touchend", function(event) {
stx = etx = curX = sty = ety = curY = 0;
MAction = false; }); function getT3d(elem, ename) {
var elem = elem[0];
var str1 = elem.style.webkitTransform;
if (str1 == "") return "0";
str1 = str1.replace("translate3d(", "");
str1 = str1.replace(")", "");
var carr = str1.split(","); if (ename == "x") return carr[0];
else if (ename == "y") return carr[1];
else if (ename == "z") return carr[2];
else return "";
}
}
});
};
})(jQuery);

 今天先写这么多,有时间接着写实现过程。 

基于jQuery的数字键盘插件的更多相关文章

  1. 基于jQuery的软键盘

    基于jQuery的软键盘   前些天写了一个基于基于jQuery的数字键盘,今天给大家带来一个基于jQuery的全字母键盘插件(支持全字母大小写切换,数字输入,退格清除,关闭功能,可调整大小和键盘位置 ...

  2. jQuery 虚拟数字键盘代码

    先上效果:    js直接应用:  $('input').mynumkb(); 就出来效果 HTML: <input maxlength="4" type="tex ...

  3. 10 个基于 jQuery 的 Web 交互插件推荐

    英文原文:10 jQuery for Web Interaction Plugins “用户交互”在现代的 Web 设计中占据了很大比例,这是互联网产品不可或缺的关键,对 Web 设计师也提出了更高的 ...

  4. 源码来袭!!!基于jquery的ajax分页插件(demo+源码)

    前几天打开自己的博客园主页,无意间发现自己的园龄竟然有4年之久了.可是看自己的博客列表却是空空如也,其实之前也有写过,但是一直没发布(然而好像并没有什么卵用).刚开始学习编程时就接触到博客园,且在博客 ...

  5. 出位的template.js 基于jquery的模板渲染插件

    找了好几款基于jquery的模板渲染插件,无一感觉很难用(教程较少.绑定不统一),也可能我智商问题,比如jquery template.js .jtemplate.js. 然后在github上找到这一 ...

  6. 基于jquery的城市选择插件

    城市选择插件的难度不是很大,主要是对dom节点的操作.而我写的这个插件相对功能比较简答,没有加入省市联动. 上代码好了,参照代码的注释应该比较好理解. /* *基于jquery的城市选择插件 *aut ...

  7. 基于jQuery全屏相册插件zoomVisualizer

    基于jQuery全屏相册插件zoomVisualizer.这是一款基于jquery ui实现的相册插件,支持隐藏显示相册缩略图,支持左右箭头切换图片,支持放大缩及缩小图片.效果图如下: 在线预览    ...

  8. 一款基于jQuery的QQ表情插件

    我们在QQ聊天或者发表评论.微博时,会有一个允许加入表情的功能,点击表情按钮,会弹出一系列表情小图片,选中某个表情图片即可发表的丰富的含表情的内容.今天和大家分享一款基于jQuery的QQ表情插件,您 ...

  9. 基于jQuery开发的手风琴插件 jquery.accordion.js

     1.插件代码 少说多做,基于jQuery的手风琴插件jquery.accordion.js的代码:  /* * 手风琴插件说明: * 1.treeTrunk对应树干 * 2.treeLeaf对应树叶 ...

随机推荐

  1. x == (x = y) 不等于 (x = y) == x ?

    简评:不瞒你说,我现在数数都是从 0 开始数的,整数是 1024. 有这么一个 Java 程序: class Quirky { public static void main(String[] arg ...

  2. 几种封装javaBean的方法

    开发框架时,经常需要使用java对象(javaBean)的属性来封装程序的数据,封装javaBean的方法有很多,比如反射,内省,以及使用工具类.下面从反射开始介绍. 1.javaBean介绍: 简介 ...

  3. 架构师养成记--29.redis开篇

    主要有从下几点讲解 NOSQL(Redis) 简介.redis安装与部署 Redis基础事件类型详解 Redis高级命令 Redis与java的使用 Redis集群搭建 Redis集群与spring的 ...

  4. i2c_smbs 函数

    i2c_smbus系列函数有: s32 i2c_smbus_read_byte(const struct i2c_client *client); s32 i2c_smbus_write_byte(c ...

  5. IDEA 笔记汇总

    Intellij IDEA 像eclipse那样给maven添加依赖 Intellij idea maven 引用无法搜索远程仓库的解决方案 Intellij IDEA 封装Jar包(提示错误: 找不 ...

  6. 防止过拟合:L1/L2正则化

    正则化方法:防止过拟合,提高泛化能力 在训练数据不够多时,或者overtraining时,常常会导致overfitting(过拟合).其直观的表现如下图所示,随着训练过程的进行,模型复杂度增加,在tr ...

  7. 剑指offer——面试题30:包含min函数的栈

    #include"iostream" #include"stdio.h" using namespace std; ; ; template<typena ...

  8. mono for android生成APK出现错误fatal error in gc 解决方案

    laxknight 标签: mono for android,fatal error in gc mono for android生成APK出现这个错误fatal error in gc collec ...

  9. 使用NHibernate(4)--拦截器和事件

    如果想在一个事务的开始.执行中.完成后等过程中执行一些自己的逻辑(比如记录日志.查看sql),拦截器(Interceptors)和事件(Event)就可以发挥作用了.两者所能完成的功能差不多. 1,拦 ...

  10. Winform控件的问题汇总

    2014-01-19号 用户控件中的子控件(Btn控件),想要暴露到用户控件之外,以供其它其他控件使用的解决方法 1.在用户控件中定义一个委托和这个委托的事件. public delegate voi ...