Skip to content

Commit

Permalink
优化keydown
Browse files Browse the repository at this point in the history
  • Loading branch information
llan committed Jan 23, 2017
1 parent 1ddcba2 commit 25364c4
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 2 deletions.
9 changes: 8 additions & 1 deletion second-step/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,14 @@ class Block {
left = parseInt(activeModel.style.left) ? parseInt(activeModel.style.left) : 0,
top = parseInt(activeModel.style.top) ? parseInt(activeModel.style.top) : 0;
const key = e.keyCode;
const {canMoveRight, canMoveLeft, canMoveTop, canMoveDown} = this.canMove();
let canMoveRight,
canMoveLeft,
canMoveTop,
canMoveDown;
switch (key) {
//left
case 37:
canMoveLeft = this.canMove().canMoveLeft;
if (canMoveLeft) {
activeModel.style.left = `${left - 20}px`;
} else {
Expand All @@ -62,6 +66,7 @@ class Block {
break;
//up
case 38:
canMoveTop = this.canMove().canMoveTop;
if (canMoveTop) {
activeModel.style.top = `${top - 20}px`;
} else {
Expand All @@ -70,6 +75,7 @@ class Block {
break;
//right
case 39:
canMoveRight = this.canMove().canMoveRight;
if (canMoveRight) {
activeModel.style.left = `${left + 20}px`;
} else {
Expand All @@ -78,6 +84,7 @@ class Block {
break;
//down
case 40:
canMoveDown = this.canMove().canMoveDown;
if (canMoveDown) {
activeModel.style.top = `${top + 20}px`;
} else {
Expand Down
9 changes: 8 additions & 1 deletion third-step/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,10 +88,14 @@ class Block {
document.onkeydown = (e)=> {
let activeModel = document.querySelectorAll('.activityModel');
const key = e.keyCode;
const {canMoveRight, canMoveLeft, canMoveTop, canMoveDown} = this.canMove();
let canMoveRight,
canMoveLeft,
canMoveTop,
canMoveDown;
switch (key) {
//left
case 37:
canMoveLeft = this.canMove().canMoveLeft;
if (canMoveLeft) {
for (let v of activeModel) {
v.style.left = `${parseInt(v.style.left) - 20}px`;
Expand All @@ -102,6 +106,7 @@ class Block {
break;
//up
case 38:
canMoveTop = this.canMove().canMoveTop;
if (canMoveTop) {
for (let v of activeModel) {
v.style.top = `${parseInt(v.style.top) - 20}px`;
Expand All @@ -112,6 +117,7 @@ class Block {
break;
//right
case 39:
canMoveRight = this.canMove().canMoveRight;
if (canMoveRight) {
for (let v of activeModel) {
v.style.left = `${parseInt(v.style.left) + 20}px`;
Expand All @@ -122,6 +128,7 @@ class Block {
break;
//down
case 40:
canMoveDown = this.canMove().canMoveDown;
if (canMoveDown) {
for (let v of activeModel) {
v.style.top = `${parseInt(v.style.top) + 20}px`;
Expand Down

0 comments on commit 25364c4

Please sign in to comment.