基于 Vue 的 焦点组件,既键盘导航组件(keyboard navigation/spatial navigation)
tag
:必填,所要渲染的标签,如div
clickable
:选填,鼠标是否可点击/悬浮聚焦,默认为true
className
:选中状态下的 class 样式名称,默认为focus
setupKeyBoardEvents
:设置键盘事件
import Focusable, { SpatialNavigationOptions ,NavigationServiceDirection } from "../../dist/"
Vue.use(Focusable, <SpatialNavigationOptions>{
tag: "div",
clickable: true,
setupKeyBoardEvents(el) {
document.addEventListener("keydown", (e: KeyboardEvent) => {
// 查找
const keyCode = e.keyCode ? e.keyCode : e.charCode ? e.charCode : e.which;
if (el) {
switch (keyCode) {
case 38:
el.spatialNavigationAction(NavigationServiceDirection.Up)
break;
case 40:
el.spatialNavigationAction(NavigationServiceDirection.Down)
break;
case 37:
el.spatialNavigationAction(NavigationServiceDirection.Left)
break;
case 39:
el.spatialNavigationAction(NavigationServiceDirection.Right)
break;
case 13:
el.spatialNavigationAction(NavigationServiceDirection.Enter)
break;
}
}
});
}
})
当 left
、right
、up
、down
为AUTOFOCUS
时,跳转到下一个默认:具有data-default
/ default
的焦点元素
属性名 | 说明 |
---|---|
id |
指定组件id :默认随机 |
data-default / default |
是否为默认焦点 |
data-left / left |
指定触发left 事件时所要导航到的id |
data-right / right |
指定触发right 事件时所要导航到的id |
data-up / up |
指定触发up 事件时所要导航到的id |
data-down / down |
指定触发down 事件时所要导航到的id |
事件参数:
{
id: string,
isDefault: boolean,
isFocus: boolean
}
属性名 | 说明 |
---|---|
focus |
触发focus 事件时 |
blur |
触发blur 事件时 |
click |
触发click 事件时 |
left |
触发left 事件时 |
right |
触发right 事件时 |
up |
触发up 事件时 |
down |
触发down 事件时 |
跳转到指定的 id
import { navigationService } from "../../dist/";
let el = navigationService.getFocusElementById("button2");
if (el) el.focus()
来自example
目录下的HOME
<template>
<div class="home">
<img alt="Vue logo" src="../assets/logo.png" />
<!-- <HelloWorld msg="Welcome to Your Vue.js + TypeScript App"/> -->
<div class="df fw yellow">
<Focusable down="left1" class="item blueviolet">{{0}}</Focusable>
<Focusable v-for="i in 5" :key="i" class="item blueviolet" default>{{i}}</Focusable>
</div>
<Focusable id="f1" class="df crimson">
<!-- <template>
<div :style="{'color':scope.isFocus?'red':'#333'}">1111111{{scope}}</div>
</template> -->
<Focusable class="blue">
<Focusable id="left0" right="right2" class="item red">{{0}}</Focusable>
<Focusable :id="'left' + i" right="right2" v-for="i in 5" :key="i" class="item red">{{i}}</Focusable>
</Focusable>
<Focusable class="df dfc red">
<div>
<Focusable class="df fw blueviolet">
<Focusable left="AUTOFOCUS" down="AUTOFOCUS" class="item blue">{{0}}</Focusable>
<div v-for="i in 10" :key="i">
<Focusable :id="'right' + i" v-if="i%2 == 0" class="item blue">{{i}}</Focusable>
<div v-if="i%2 != 0" class="item crimson">{{i}}</div>
</div>
</Focusable>
</div>
<Focusable class="df fw red">
<Focusable v-for="i in 10" :key="i" class="item yellow" default>{{i}}</Focusable>
</Focusable>
</Focusable>
</Focusable>
</div>
</template>