javascript - 具有键盘导航行为的 Jquery 自定义画廊

标签 javascript jquery html

我正在尝试创建一个带有键盘导航的图片库。 除了一些小东西之外,大部分都可以正常工作。画廊应该有 3 个状态。顶部、中间和底部状态。请参阅包含的图片:

enter image description here

在某些情况下,当您在图库中上下“滚动”速度太快时,它会变得“困惑”。

错误状态:

enter image description here

我的猜测是键盘事件是在动画完成之前注册的。 非常感谢任何有关此问题的帮助。

fiddle : http://jsfiddle.net/sb3nqtnr/1/

var y_pos = 1,
x_pos = 1,
item_position = 1;
var gallery_li = $('.gallery > ul > li');
var gallery_ul = $('.gallery');
var total_items = gallery_li.length;
var num_cols = 3;
var num_rows = Math.ceil(total_items / num_cols);
var li_height = gallery_li.height();
var li_margin = gallery_li.css("margin-top").replace("px", "");
var move_margin = 0;
var li_offset = li_height + (1.5 * li_margin);

$('.gallery > ul > li').each(function(i) {
    $(this).attr('id', 'property_' + (i + 1));
    $(this).text('TEST ' + (i + 1))
});

selectItem(1);

function selectItem(pos) {
    var item_position = pos;
    gallery_li.removeClass('active');
    var item = $('.gallery > ul > li:nth-child(' + item_position + ')');
    item.addClass('active');
}

//calculate number of li per row
function calculateLIsInRow() {
    var lisInRow = 0;
    $('.gallery > ul > li').each(function() {
        if ($(this).prev().length > 0) {
            if ($(this).position().top != $(this).prev().position().top) return false;
            lisInRow++;
        } else {
            lisInRow++;
        }
    });
    return lisInRow;
}

$("html").on("keydown", function(event) {
    keyCode = event.which;

    var current_position = y_pos + x_pos;
    var gallery_ul = $('.gallery > ul');
    var gallery_ul_mt = gallery_ul.css("margin-top").replace("px", "");


    if (keyCode == 37) {
        //move left
        if (x_pos > 1) {
            x_pos--;
        }

    } else if (keyCode == 39) {
        //move right
        if (x_pos < num_cols && item_position < total_items) {
            x_pos++;
        }

    } else if (keyCode == 38) {
        //move up
        if (y_pos > 1) {
            y_pos--;

            if (y_pos > 1) {
                move_margin += li_offset;
                gallery_ul.stop(true, true).finish().animate({
                    marginTop: move_margin
                }, 500);
            }

        }

    } else if (keyCode == 40) {
        //move down
        if (y_pos < num_rows && (item_position + num_cols <= total_items)) {
            y_pos++;

            if (y_pos > 2 && (y_pos + 1 <= num_rows)) {
                move_margin -= li_offset;
                gallery_ul.stop(true, true).finish().animate({
                    marginTop: move_margin
                }, 500);
            }
        }
    }


    item_position = (x_pos * y_pos) + ((num_cols - x_pos) * (y_pos - 1));
    selectItem(item_position);
});

最佳答案

我自己解决了这个问题。

问题在于添加和减去顶部偏移量。

move_margin -= li_offset;

必须将其更改为根据所选行/y 位置计算位置:

move_margin = -(y_pos-2) * li_offset;

<强> Updated fiddle

关于javascript - 具有键盘导航行为的 Jquery 自定义画廊,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27509293/

相关文章:

html - 为什么当我的 div 在 body 外面时没有出现滚动条?

javascript - 继承在 react Mocha 测试中不起作用

javascript - 定位所有选择字段,而不仅仅是首先使用 JS/jQuery

click() 闭包的 Jquery 循环

javascript - 单击时打开 Div,再次单击时保持打开状态

javascript - 使用 jQuery 动态 "add another product"表单

javascript - 我的 .find 函数没有调用我的数据库

javascript - Eslint: --fix 不根据 .eslintrc 规则解决警告

php - 将 HTML 标记存储在 PHP 字符串中

javascript - 出现以下错误 Cannot read property 'value' of undefined at HTMLInputElement.onclick