The native JS realizes the keyboard control Div movement and solves the pause question _javascript skill

Source: Internet
Author: User

First of all, why is the pause?

effect: Use the keyboard to control a DIV move

When you press one of the arrow keys, the div pauses before starting to move continuously.

Reason: the system to distinguish whether the user is continuous input, the first to the second between there is a pause time
Note: Understand the reason to solve the problem

Effect Show

1. Simple control, but there is a pause

2. Simple control, solve the pause

Solving method

method: first open a timer, so that Div has been in (in 4 directions) ready to move the state

The initial 4-direction values are false,div and remain stationary.

When you press a direction key, the value in this direction changes to True,div will start moving in this direction.

When you release the arrow key, the value in this direction changes to false, and the div stops moving in that direction.

basic move, but there's a nice bit of code

 

control movement, solve the problem of a pause  

 

Summarize

The above is the entire content of this article, I hope the content of this article for everyone's work or learning can bring certain help, if you have questions you can message exchange.

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.