-
-
Notifications
You must be signed in to change notification settings - Fork 2.4k
/
demo.vue
59 lines (50 loc) · 1.1 KB
/
demo.vue
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
<script setup lang="ts">
import { ref } from 'vue-demi'
import { onKeyStroke } from '.'
const translateX = ref(0)
const translateY = ref(0)
onKeyStroke('ArrowUp', (e: KeyboardEvent) => {
translateY.value -= 10
e.preventDefault()
})
onKeyStroke('ArrowDown', (e: KeyboardEvent) => {
translateY.value += 10
e.preventDefault()
})
onKeyStroke('ArrowLeft', (e: KeyboardEvent) => {
translateX.value -= 10
e.preventDefault()
})
onKeyStroke('ArrowRight', (e: KeyboardEvent) => {
translateX.value += 10
e.preventDefault()
})
</script>
<template>
<div>
<div class="container">
<div class="ball" :style="{transform: `translate(${translateX}px, ${translateY}px)`}" />
</div>
<div class="text-center mt-4">
Use the arrow keys to control the movement of the ball.
</div>
</div>
</template>
<style scoped>
.container {
display: flex;
justify-content: center;
align-items: center;
width: 400px;
height: 100px;
margin: auto;
overflow: hidden;
border: 1px solid #a1a1a1;
}
.ball {
width: 16px;
height: 16px;
background: #a1a1a1;
border-radius: 50%;
}
</style>