[Reprinted] a smooth keyboard control (with inertia)

Source: Internet
Author: User

 

1 < Body >
2 < Div ID = " Man " Style = " Position: absolute; width: 50px; Height: 40px; top: 200px; left: 200px; Background: red " > </ Div >
3 < Button onclick = " Alert (keycache. length) " > Ask </ Button >
4 </ Body >
5 < Script >
6 VaR Keycache = [];
7 VaR Iscache =   False ;
8 VaR Cachenum =   0 ;
9
10 Document. onkeydown = Insertkey;
11 Document. onkeyup =   Function () {If(!Iscache&&Event. keycode> =37&&Event. keycode<=40) Keycache. Length= 0;}
12
13 Function Insertkey () {
14 VaR KC = Event. keycode;
15 If (KC > = 37   && KC <= 40 ) {
16 If (KC ! = Keycache [keycache. Length - 1 ]) {
17 Keycache. Length =   0 ;
18 Keycache. Push (KC, KC) // Ensures smooth multi-injection of 6
19 Iscache =   True ;
20 }
21 Keycache. Push (KC );
22 }
23 }
24
25 Window. setinterval ( " Writecache () " , 50 );
26
27 Function Writecache () {
28 If (Keycache. Length = 0 ) Return ;
29
30 VaR KC = Keycache. Shift ();
31
32 Switch (KC) {
33 Case   37 : Man. style. Left = Parseint (man. style. Left) -   5 ; Break ;
34 Case   38 : Man. style. Top = Parseint (man. style. Top) -   5 ; Break ;
35 Case   39 : Man. style. Left = Parseint (man. style. Left) +   5 ; Break ;
36 Case   40 : Man. style. Top = Parseint (man. style. Top) +   5 ; Break ;
37 }
38
39 If (Iscache) cachenum ++ ;
40 If (Cachenum =   5 ) {
41Iscache= False;
42Cachenum= 0;
43}
44 }
45 </ Script >

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.