javascript--cursor key controls Div movement,

Source: Internet
Author: User

CSS code:

#div1 { width: 100px; height: 100px; background: #ccc; position: absolute;}

HTML code:

<id= "Div1"></div>

JS Code:

Window.onload =function(){        varOdiv = document.getElementById (' Div1 ')); Document.onkeydown=function(EV) {varEV = EV | |event; Switch(ev.keycode) {//Move left                 Case37: ODiv.style.left= odiv.offsetleft-10 + ' px ';  Break; //Move Up                 Case38: ODiv.style.top= odiv.offsettop-10 + ' px ';  Break; //Move Right                 Case39: ODiv.style.left= Odiv.offsetleft + + ' px ';  Break; //Downward Movement                 Case40: ODiv.style.top= Odiv.offsettop + + ' px ';  Break;    }        }; };

Note: only offsetleft and offsetTop, no offsetright and offsetbottom.

Description: The event.keycode value for 37,38,39,40 corresponds to the pressed direction key divided by the left, top, right, and bottom.

javascript--cursor key controls Div movement,

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.