Click and keydown at same time for draggable jQuery event?

前端 未结 1 1239
说谎
说谎 2021-01-28 00:39

I\'m trying to have a jQuery UI event fire only if it meets the criteria of being clicked while the shift key is in the keydown state ( to mimic being held), and if not disable

相关标签:
1条回答
  • 2021-01-28 01:15

    I see lots of errors with that code. Firstly, you only add the key listener after there's been a click on the document. Second you are adding keydown to the container div, rather than the whole document. Then, you also need to listen to keyup, since releasing the shift key should disable draggability, then you also need to pass disabled: false to the case where shift is down. And your handler is missing the e parameter. Try this:

    $(function(e) {
        var handler = function(e) {
            if (e.shiftKey) {
                $('.container').draggable({
                    disabled: false
                });
            } else {
                $('.container').draggable({
                    disabled: true
                });
            }
        };
        $(document).keydown(handler);
        $(document).keyup(handler);
    });
    
    0 讨论(0)
提交回复
热议问题