些網(wǎng)站要實(shí)現(xiàn)一些彈層效果,經(jīng)常會(huì)有鼠標(biāo)跟著移動(dòng)彈層也隨之移動(dòng)的特效,下面來說說這種效果的實(shí)現(xiàn)方法!
實(shí)現(xiàn)的效果:
下面來看看代碼:
html:
css:
js:
HTML 的 tabindex 屬性開發(fā)過程中一般不會(huì)使用到,最近開發(fā)中有個(gè)需求兼顧富交互,便總結(jié)了一下。本篇文章同時(shí)收錄在我的【前端知識(shí)點(diǎn)】中,Github鏈接請(qǐng)點(diǎn)擊閱讀原文直達(dá),歡迎 Star
兼容性:Safari不支持!
在我們?nèi)粘J褂镁W(wǎng)頁的過程中,可以通過鍵盤控制一些元素的聚焦,從而達(dá)到便捷訪問的目的
element 分為 focusable 和 非focusable ,如果使用了tabindex就可以改變相關(guān)的行為
在HTML中有6個(gè)元素默認(rèn)支持聚焦:
以上的元素默認(rèn)都可以使用 Tab 鍵,以及 JS focus() 方法聚焦
document.querySelector("a").focus();
使用 tab鍵 進(jìn)行聚焦元素時(shí),聚焦的順序等于元素在代碼中的出現(xiàn)先后順序,當(dāng)我們進(jìn)行富交互優(yōu)化時(shí),就需要用到 tabindex 這個(gè)屬性來幫助我們進(jìn)行更好用戶體驗(yàn)的優(yōu)化了
①元素是否能聚焦:通過鍵盤這類輸入設(shè)備,或者通過 JS focus() 方法
②元素什么時(shí)候能聚焦:在用戶通過鍵盤與頁面交互時(shí)
通俗來說:就是當(dāng)用戶使用鍵盤時(shí),tabindex用來定位html元素,即使用tab鍵時(shí)焦點(diǎn)的順序。
tabindex理論上可以使用在幾乎所有元素上
tabindex 有三個(gè)值:0,-N(通常是-1),N(正值)
tabindex 決定聚焦順序
// HTML
<button type="button" tabindex="1">tabindex === 1</button>
<button type="button" tabindex="999">tabindex === 999</button>
<button type="button" tabindex="0">tabindex === 0</button>
// HTML
<button type="button" tabindex="0">tabindex === 0</button>
<button type="button" tabindex="1">tabindex === 1</button>
<button type="button" tabindex="999">tabindex === 999</button>
<button type="button" tabindex="0">tabindex === 0</button>
tabindex 決定是否聚焦
// HTML
<button type="button">未設(shè)置tabindex</button>
<button type="button" tabindex="-1">tabindex === -1</button>
<button type="button" tabindex="0">tabindex === 0</button>
<button type="button" tabindex="1">tabindex === 1</button>
tabindex 與JS編程聚焦
// HTML
<button type="button" @click="clickBtn()">點(diǎn)擊讓DIV聚焦</button>
<div id="FocusDiv" ref="FocusDiv" tabindex="-1">這是一個(gè)div</div>
// JS
clickBtn: function() {
document.getElementById('FocusDiv').focus();
}
針對(duì)自定義標(biāo)簽進(jìn)行富交互優(yōu)化
針對(duì)特定節(jié)點(diǎn)禁止聚焦操作
復(fù)雜列表控制聚焦順序
現(xiàn)效果:
<script src="https://lf6-cdn-tos.bytescm.com/obj/cdn-static-resource/tt_player/tt.player.js?v=20160723"></script>
實(shí)現(xiàn)代碼:
*請(qǐng)認(rèn)真填寫需求信息,我們會(huì)在24小時(shí)內(nèi)與您取得聯(lián)系。