网页设计实用技术:JS软键盘实现(源码)

2008-02-23 08:08:08来源:互联网 阅读 ()

新老客户大回馈,云服务器低至5折

鉴于安全性的考虑,不少网站在登录输入密码时都采用了软键盘,避免一些键盘记录工具和木马对击键的捕捉。项目中也有这个需求,就分享给大家了,贴个效果图上来。。有兴趣的朋友可以收藏。。..如果觉得功能有点庞大,只需要数字小键盘的朋友,可参考代码精简。
效果还原:Default.aspx,softkeyboard.js,softkey.CSS三个文件
js代码:

softkeyboard.js

1window.onload=
2function()
3{
4password1=null;
5initCalc();
6}
7var password1;
8var CapsLockValue=0;
9var checkSoftKey;
10function setVariables() {
11tablewidth=630;
12tableheight=20;
13if (navigator.appName == "Netscape") {
14horz=".left";
15vert=".top";
16docStyle="document.";
17styleDoc="";
18innerW="window.innerWidth";
19innerH="window.innerHeight";
20offsetX="window.pageXOffset";
21offsetY="window.pageYOffset";
22}
23else {
24horz=".pixelLeft";
25vert=".pixelTop";
26docStyle="";
27styleDoc=".style";
28innerW="document.body.clientWidth";
29innerH="document.body.clientHeight";
30offsetX="document.body.scrollLeft";
31offsetY="document.body.scrollTop";
32}
33}
34function checkLocation() {
35if (checkSoftKey) {
36objectXY="softkeyboard";
37var availableX=eval(innerW);
38var availableY=eval(innerH);
39var currentX=eval(offsetX);
40var currentY=eval(offsetY);
41x=availableX-tablewidth currentX;
42y=currentY;
43evalMove();
44}
45setTimeout("checkLocation()",0);
46}
47function evalMove() {
48eval(docStyle objectXY styleDoc vert "=" y);
49}
50self.onError=null;
51currentX = currentY = 0;
52whichIt = null;
53lastScrollX = 0; lastScrollY = 0;
54NS = (document.layers) ? 1 : 0;
55IE = (document.all) ? 1: 0;
56function heartBeat() {
57if(IE) { diffY = document.body.scrollTop; diffX = document.body.scrollLeft; }
58if(NS) { diffY = self.pageYOffset; diffX = self.pageXOffset; }
59if(diffY != lastScrollY) {
60percent = .1 * (diffY - lastScrollY);
61if(percent > 0) percent = Math.ceil(percent);
62else percent = Math.floor(percent);
63if(IE) document.all.softkeyboard.style.pixelTop = percent;
64if(NS) document.softkeyboard.top = percent;
65lastScrollY = lastScrollY percent;}
66if(diffX != lastScrollX) {
67percent = .1 * (diffX - lastScrollX);
68if(percent > 0) percent = Math.ceil(percent);
69else percent = Math.floor(percent);
70if(IE) document.all.softkeyboard.style.pixelLeft = percent;
71if(NS) document.softkeyboard.left = percent;
72lastScrollX = lastScrollX percent; } }
73function checkFocus(x,y) {
74stalkerx = document.softkeyboard.pageX;
75stalkery = document.softkeyboard.pageY;
76stalkerwidth = document.softkeyboard.clip.width;
77stalkerheight = document.softkeyboard.clip.height;
78if( (x > stalkerx && x < (stalkerx stalkerwidth)) && (y > stalkery && y < (stalkery stalkerheight))) return true;
79else return false;}
80function grabIt(e) {
81checkSoftKey = false;
82if(IE) {
83whichIt = event.srcElement;
84while (whichIt.id!=null&&whichIt.id.indexOf("softkeyboard") == -1) {
85whichIt = whichIt.parentElement;
86if (whichIt == null) { return true; } }
87if(whichIt.style!=null){
88whichIt.style.pixelLeft = whichIt.offsetLeft;
89whichIt.style.pixelTop = whichIt.offsetTop;
90}
91currentX = (event.clientX document.body.scrollLeft);
92currentY = (event.clientY document.body.scrollTop);
93} else {
94window.captureEvents(Event.MOUSEMOVE);
95if(checkFocus (e.pageX,e.pageY)) {
96whichIt = document.softkeyboard;
97StalkerTouchedX = e.pageX-document.softkeyboard.pageX;
98StalkerTouchedY = e.pageY-document.softkeyboard.pageY;} }
99return true; }
100function moveIt(e) {
101if (whichIt == null) { return false; }
102if(IE) {
103if(whichIt.style!=null){
104newX = (event.clientX document.body.scrollLeft);
105newY = (event.clientY document.body.scrollTop);
106distanceX = (newX - currentX); distanceY = (newY - currentY);
107currentX = newX; currentY = newY;
108whichIt.style.pixelLeft = distanceX;
109whichIt.style.pixelTop = distanceY;
110if(whichIt.style.pixelTop < document.body.scrollTop) whichIt.style.pixelTop = document.body.scrollTop;
111if(whichIt.style.pixelLeft < document.body.scrollLeft) whichIt.style.pixelLeft = document.body.scrollLeft;
112if(whichIt.style.pixelLeft > document.body.offsetWidth - document.body.scrollLeft - whichIt.style.pixelWidth - 20) whichIt.style.pixelLeft = document.body.offsetWidth - whichIt.style.pixelWidth - 20;
113if(whichIt.style.pixelTop > document.body.offsetHeight document.body.scrollTop - whichIt.style.pixelHeight - 5) whichIt.style.pixelTop = document.body.offsetHeight document.body.scrollTop - whichIt.style.pixelHeight - 5;
114event.returnValue = false;
115}
116} else {
117whichIt.moveTo(e.pageX-StalkerTouchedX,e.pageY-StalkerTouchedY);

标签:

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有

上一篇:网页知识:在Javascript中使用正则表达式

下一篇:网页:超级实用的几种表单校验效果收集