numkeyboard.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  1. /*
  2. * numkeyboard 1.0
  3. * Copyright (c) 2014 BowenLuo http://www.luobo.com/
  4. * Date: 2014-06-08
  5. * Example:$(".numkeyboard").numkeyboard();
  6. */
  7. (function($) {
  8. $.fn.numkeyboard = function(options) {
  9. var defaults = {
  10. keyboardRadix : 1000,// 图片停留时间
  11. animatetime : 300, // 图片滑动所需时间
  12. mainbackground : '#C8BFE7', // 轮播方向
  13. menubackground : '#4A81B0', // 轮播方向
  14. exitbackground : '#4376A0', // 轮播方向
  15. buttonbackground : '#ff730e', // 轮播方向
  16. clickeve : false
  17. // 是否绑定元素click事件
  18. }
  19. var options = $.extend(defaults, options);
  20. var keyboardRadix = options.keyboardRadix;
  21. if (keyboardRadix < 300) {
  22. keyboardRadix = 300;
  23. }
  24. var numkeyboardcount = 0;
  25. var activeinputele;
  26. this.each(function() {
  27. numkeyboardcount++;
  28. // 添加唯一的数字键盘
  29. if (numkeyboardcount < 2) {
  30. keyBoardId = randomOnlyId();
  31. // $("body").append(
  32. // "<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>"
  33. // + "<li><button type='submit'>确认</button></li>" + "</ul></div>");
  34. $("body").append(
  35. "<div id='" + keyBoardId + "' class='auth_keybord'>" + "<div id='auth_keybord_exit' class='auth_keybord_exit'>关闭</div>" +
  36. "<div id='auth_keybord_menu' class='auth_keybord_menu'></div>" + "<ul class='number_list' id='number_list'>" +
  37. "<li><button type='button'>7</button></li>" + "<li><button type='button'>8</button></li>" + "<li><button type='button'>9</button></li>" +
  38. "<li><button type='button'>4</button></li>" + "<li><button type='button'>5</button></li>" + "<li><button type='button'>6</button></li>" +
  39. "<li><button type='button'>1</button></li>" + "<li><button type='button'>2</button></li>" + "<li><button type='button'>3</button></li>" +
  40. "<li><button type='button'>0</button></li>" + "<li><button type='button'>.</button></li>"
  41. + "<li><button type='submit'>确认</button></li>" + "</ul></div>");
  42. }
  43. // 元素选择器
  44. var inputele = $(this);
  45. var keyboard = $("#" + keyBoardId + "");
  46. var keyboard_exit = keyboard.children('div:first');
  47. var keyboard_menu = keyboard.children('div:eq(1)');
  48. var keyboard_buttonul = keyboard.find('ul:first');
  49. var keyboard_buttonli = keyboard_buttonul.children('li');
  50. var keyboard_button = keyboard_buttonli.children('button');
  51. // 元素css样式控制
  52. keyboard.css({
  53. "position" : "absolute",
  54. "z-index" : "10",
  55. "display" : "none",
  56. "background" : options.mainbackground,
  57. overflow : "hidden",
  58. "width" : keyboardRadix * 0.58,
  59. "height" : keyboardRadix * 0.45,
  60. "border-radius" : keyboardRadix * 0.01
  61. });
  62. keyboard_exit.css({
  63. "position" : "absolute",
  64. "z-index" : "1",
  65. "right" : "0",
  66. "background" : options.exitbackground,
  67. "cursor" : "pointer",
  68. "text-align" : "center",
  69. "width" : keyboardRadix * 0.16,
  70. "height" : keyboardRadix * 0.081,
  71. "border-top-right-radius" : keyboardRadix * 0.01,
  72. "line-height" : keyboardRadix * 0.081 + "px",
  73. "font-family" : "'微软雅黑',arial",
  74. "font-size" : keyboardRadix * 0.036 + "px",
  75. "corlor" : "#000",
  76. "letter-spacing" : keyboardRadix * 0.005
  77. });
  78. keyboard_menu.css({
  79. position : "relative",
  80. background : options.menubackground,
  81. cursor : "move",
  82. margin : "auto",
  83. width : keyboardRadix * 0.6,
  84. height : keyboardRadix * 0.081,
  85. "border-top-left-radius" : keyboardRadix * 0.01,
  86. "border-top-right-radius" : keyboardRadix * 0.01
  87. });
  88. keyboard_buttonul.css({
  89. position : "relative",
  90. margin : "auto",
  91. "margin-top" : keyboardRadix * 0.02 + "px",
  92. width : keyboardRadix * 0.54,
  93. height : keyboardRadix * 0.20
  94. });
  95. keyboard_buttonli.css({
  96. position : "relative",
  97. margin : "auto",
  98. overflow : "hidden",
  99. "float" : "left",
  100. width : keyboardRadix * 0.15,
  101. height : keyboardRadix * 0.085
  102. });
  103. var buttonborder = String(keyboardRadix * 0.001 + "px solid " + options.buttonbackground);
  104. keyboard_button.css({
  105. "position" : "relative",
  106. "float" : "left",
  107. padding : "0",
  108. "cursor" : "pointer",
  109. "background" : options.buttonbackground,
  110. "text-align" : "center",
  111. "width" : keyboardRadix * 0.14,
  112. "height" : keyboardRadix * 0.08,
  113. "border-radius" : keyboardRadix * 0.004,
  114. border : buttonborder,
  115. "line-height" : keyboardRadix * 0.08 + "px",
  116. margin : "0 0 0 " + keyboardRadix * 0.01 + "px",
  117. "font-family" : "'微软雅黑',arial",
  118. "font-size" : keyboardRadix * 0.032 + "px",
  119. "color" : "#fff"
  120. });
  121. keyboard_button.mousedown(function() {
  122. $(this).css({
  123. background : "#666",
  124. top : "2px"
  125. });
  126. }).mouseup(function() {
  127. $(this).css({
  128. background : options.buttonbackground,
  129. top : "0"
  130. });
  131. });
  132. keyboard_exit.click(function() {
  133. exit(options.clickeve);
  134. });
  135. inputele.focus(function(event) {
  136. activeinputele = $(this);
  137. var p = GetScreenPosition(this);
  138. // var left = document.body.clientWidth - ($(this).width() +
  139. // 520);
  140. // var top = document.body.clientHeight - ($(this).height() +
  141. // 500);
  142. var left = p.x - ($(this).width() - 200);//170
  143. var top = p.y < 500 ? 360 : (360+p.y - $(this).height() * 10); //40
  144. if (keyboard.css("display") == "none") {
  145. keyboard.css({
  146. "display" : "block",
  147. "left" : left,
  148. "top" : top
  149. });
  150. getElem('score_cur').value = '';
  151. mouseDrag();
  152. touchDrag();
  153. }
  154. });
  155. if (options.clickeve) {
  156. inputele.click(function() {
  157. activeinputele = $(this);
  158. var p = GetScreenPosition(this);
  159. if (keyboard.css("display") == "none") {
  160. keyboard.css({
  161. "display" : "block",
  162. "left" : p.x,
  163. "top" : p.y + $(this).height() * 1.2
  164. });
  165. getElem('score_cur').value = '';
  166. mouseDrag();
  167. touchDrag();
  168. }
  169. });
  170. }
  171. if (numkeyboardcount < 2) {
  172. for (var i = 0; i < keyboard_button.length; i++) {
  173. numbuttonclick(i);
  174. }
  175. }
  176. function randomOnlyId() {
  177. var tempRandom = String(Date.now());
  178. var tempRandomLength = tempRandom.length;
  179. tempRandom = tempRandom.substring(tempRandomLength - 5, tempRandomLength - 1);
  180. var randomId = "auth_keybord" + tempRandom;
  181. if ($("#randomId").length > 0) {
  182. randomOnlyId()
  183. } else {
  184. return randomId;
  185. }
  186. }
  187. function getElem(id) {
  188. return document.getElementById(id);
  189. }
  190. function numbuttonclick(buttonnum) {
  191. keyboard_buttonli.children('button:eq(' + buttonnum + ')').click(function(e) {
  192. var buttontext = keyboard_buttonli.children('button:eq(' + buttonnum + ')').text();
  193. if (buttontext / 1 || buttontext == '.') {
  194. // clickkey(buttontext / 1);
  195. clickkey(buttontext);
  196. } else {
  197. if (buttontext == "0") {
  198. clickkey(0);
  199. }
  200. if (buttontext == "确认") {
  201. //backspace();
  202. exit(true)
  203. }
  204. if (buttontext == "清除") {
  205. keyclear();
  206. }
  207. }
  208. })
  209. }
  210. function keyclear() {
  211. activeinputele.val("");
  212. }
  213. function backspace() {
  214. var inputtext = activeinputele.val();
  215. if (inputtext.length > 0) {
  216. inputtext = inputtext.substring(0, inputtext.length - 1);
  217. activeinputele.val(inputtext);
  218. }
  219. }
  220. function clickkey(num) {
  221. var inputtext = activeinputele.val();
  222. inputtext = inputtext + num;
  223. if (activeinputele.attr('max')) {
  224. if (parseInt(inputtext) < parseInt(activeinputele.attr('max'))) {
  225. activeinputele.val(inputtext);
  226. }
  227. } else
  228. activeinputele.val(inputtext);
  229. }
  230. function exit(flag) {
  231. keyboard.css({
  232. "display" : "none"
  233. });
  234. console.log(keyboard+'**');
  235. //调用angularjs中的方法
  236. if(flag) {
  237. var score = getElem('score_cur').value;
  238. var appElement = document.querySelector('[ng-controller=LoginCtrl]');
  239. var $scope = angular.element(appElement).scope();
  240. if(score > 100) {
  241. alert('打的分值不能超过100!');
  242. return;
  243. }
  244. if(score.length > 4) {
  245. alert('不能输入超过3位的数字!');
  246. return;
  247. }
  248. if((score !=='' && score !== undefined) || score === 0 ) {
  249. $scope.saveJudgeScore();
  250. }
  251. }
  252. }
  253. function GetScreenPosition(object) {
  254. var position = {};
  255. position.x = object.offsetLeft;
  256. position.y = object.offsetTop;
  257. while (object.offsetParent) {
  258. position.x = position.x + object.offsetParent.offsetLeft;
  259. position.y = position.y + object.offsetParent.offsetTop;
  260. if (object == document.getElementsByTagName("body")[0]) {
  261. break;
  262. } else {
  263. object = object.offsetParent;
  264. }
  265. }
  266. return position;
  267. }
  268. function mouseDrag() {
  269. var moveEle = keyboard;
  270. var eventEle = keyboard_menu;
  271. var stx = etx = curX = sty = ety = curY = 0;
  272. var MAction = false;
  273. var eleLeft = +moveEle.css("left").split('px')[0];
  274. var eleTop = +moveEle.css("top").split('px')[0];
  275. eventEle.mousedown(function(event) {
  276. MAction = true;
  277. stx = event.pageX;
  278. sty = event.pageY;
  279. eleLeft = +moveEle.css("left").split('px')[0];
  280. eleTop = +moveEle.css("top").split('px')[0];
  281. event.preventDefault();
  282. }).mousemove(function(event) {
  283. if (MAction) {
  284. var curX = event.pageX - stx;
  285. var curY = event.pageY - sty;
  286. moveEle.css({
  287. "left" : eleLeft + curX,
  288. "top" : eleTop + curY
  289. });
  290. event.preventDefault();
  291. }
  292. });
  293. $("body").mouseup(function(event) {
  294. stx = etx = curX = sty = ety = curY = 0;
  295. MAction = false;
  296. });
  297. }
  298. function touchDrag() {
  299. var moveEle = keyboard;
  300. var eventEle = keyboard_menu;
  301. var stx = sty = etx = ety = curX = curY = 0;
  302. var TAction = false;
  303. var eleLeft = +moveEle.css("left").split('px')[0];
  304. var eleTop = +moveEle.css("top").split('px')[0];
  305. eventEle.on("touchstart", function(event) { // touchstart
  306. var event = event.originalEvent;
  307. TAction = true;
  308. curX = curY = 0;
  309. // 元素当前位置
  310. eleLeft = +moveEle.css("left").split('px')[0];
  311. eleTop = +moveEle.css("top").split('px')[0];
  312. // 手指位置
  313. stx = event.touches[0].pageX;
  314. sty = event.touches[0].pageY;
  315. });
  316. eventEle.on("touchmove", function(event) {
  317. if (TAction) {
  318. var event = event.originalEvent;
  319. event.preventDefault();
  320. curX = event.touches[0].pageX - stx;
  321. curY = event.touches[0].pageY - sty;
  322. // alert(eleLeft+"-"+gundongX);
  323. moveEle.css({
  324. "left" : eleLeft + curX,
  325. "top" : eleTop + curY
  326. });
  327. }
  328. });
  329. eventEle.on("touchend", function(event) {
  330. stx = etx = curX = sty = ety = curY = 0;
  331. MAction = false;
  332. });
  333. function getT3d(elem, ename) {
  334. var elem = elem[0];
  335. var str1 = elem.style.webkitTransform;
  336. if (str1 == "")
  337. return "0";
  338. str1 = str1.replace("translate3d(", "");
  339. str1 = str1.replace(")", "");
  340. var carr = str1.split(",");
  341. if (ename == "x")
  342. return carr[0];
  343. else if (ename == "y")
  344. return carr[1];
  345. else if (ename == "z")
  346. return carr[2];
  347. else
  348. return "";
  349. }
  350. }
  351. });
  352. };
  353. })(jQuery);