numkeyboard.js 11 KB

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