[英]Focus on list item on down arrow press
我創建一個自定義的自動提示框,我需要繼續前進li
項上箭頭向下按。
所以我將tabindex屬性添加到li它正在獲得焦點。 但問題是它以一些隨機高度向上滾動div,它從div中選擇了li。
向下箭頭鍵后:
按下一些向下箭頭鍵后:
之后它會離開屏幕,而鼠標按下則表現完美。
在這里我做了一個Demo JSFiddle首先點擊item1
然后按向下箭頭它表現相同。
闡述我的評論
將容器的scrollTop
設置index of focused li
* li height
index of focused li
。
在keydown時return false
以防止溢出父級的正常瀏覽器滾動。
$('div.container').on('focus', 'li', function() {
var $this = $(this);
$this.addClass('active').siblings().removeClass();
$this.closest('div.container').scrollTop($this.index() * $this.outerHeight());
}).on('keydown', 'li', function(e) {
var $this = $(this);
if (e.keyCode === 40) {
$this.next().focus();
return false;
} else if (e.keyCode === 38) {
$this.prev().focus();
return false;
}
}).find('li').first().focus();
jsfiddle http://jsfiddle.net/38zR3/42/
你嘗試過使用錨而不是tabindex嗎? 例如
<li><a href="#"></li>
根據我的經驗,一些瀏覽器無法正確處理對tabindex的關注
我有一個這樣的問題,並通過將包含div的CSS樣式overflow
設置為none
或hidden
來解決它,具體取決於您的偏好。
添加.scrollTop()
以確保它居中或按您希望的方式放置。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.