<!DOCTYPE HTML><HTMLLang= "en"><Head> <MetaCharSet= "UTF-8"> <title>JavaScript Accelerated Motion</title> </Head><Body><Buttonstyle= "width:80px; height:40px; line-height:40px; text-align:center; Background:linear-gradient (to the left, #999, #d96972 , #cc1233, #d96972); position:fixed; top:200px; left:200px;border:0 ">Accelerated</Button> <Scripttype= "Text/javascript"> varbtn=document.getElementsByTagName ('Button')[0]; //Create a Div varDiv=Document.createelement ('Div'); Document.body.appendChild (DIV); Div.style.width='100px'; Div.style.height='100px'; Div.style.backgroundColor='Red'; Div.style.position='Absolute'; Div.style.left='0'; Div.style.top='0'; var Speed= 5; Btn.onclick=function() { speed++; } console.log ('Speed'+Speed ); //The onkeydown event occurs when the user presses a keyboard key. Document.onkeydown= function(e) {//Console.log (e)//print E to know the origin of the following numbers Switch(E.which) {//Upward Case -: Div.style.top=parseint (div.style.top)- Speed+ 'px'; Break;//to prevent the code from automatically running down a case //downward Case +: Div.style.top=parseint (div.style.top)+ Speed+ 'px'; Break; //left Case Panax Notoginseng: Div.style.left=parseint (div.style.left)- Speed+ 'px'; Break; //Right Case the: Div.style.left=parseint (div.style.left)+ Speed+ 'px'; Break; } } </Script> </Body></HTML>
JavaScript accelerates motion by pressing the top/right/bottom/left arrows on the keyboard