我的页面上有按钮,我有一些jquery代码,当按下向上或向下按钮时,下一个/上一个按钮将被聚焦。有没有一种方法可以从中心而不是顶部和底部滚动?我有一个设置和示例,here单击一个按钮,然后持续按下,它是否只能在div的中心而不是到达顶部或底部时才开始滚动?
let el=$('button');
for(var i = 1; i<=el.length; i++){
el.eq(i-1).attr('tabindex',i);
}
$('button').on('keydown', function(event) {
let currentTabIndex = $(this).attr('tabindex');
let el = $('button');
var callback = function(currentTabIndex){
$('li').removeClass('selected');
$("[tabindex=" + currentTabIndex + "]").focus();
$("[tabindex=" + currentTabIndex + "]").parent().addClass('selected');
}
//Focus on categories if at top and up is pressed
switch (event.which) {
case 38:
currentTabIndex = parseInt(currentTabIndex) - 1;
if (currentTabIndex == 0) {
callback(el.length)
} else {
callback(currentTabIndex)
}
break;
case 40:
currentTabIndex = parseInt(currentTabIndex) + 1;
if (currentTabIndex == el.length+1) {
callback(1)
} else {
callback(currentTabIndex)
}
break;
}
});