Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/85.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript jquery-拖动时按下shift按钮时在x轴上移动bux_Javascript_Jquery_Drag And Drop - Fatal编程技术网

Javascript jquery-拖动时按下shift按钮时在x轴上移动bux

Javascript jquery-拖动时按下shift按钮时在x轴上移动bux,javascript,jquery,drag-and-drop,Javascript,Jquery,Drag And Drop,我为这段代码创建了小提琴 问题是,当按下shift键时,我不知道如何使div在x轴上移动 请看这里: JS: $(document).ready(function () { $('div').mousedown(function () { var dragElement = $(this); $(document).mousemove(function (e) { var elementWidth = dragEleme

我为这段代码创建了小提琴 问题是,当按下shift键时,我不知道如何使div在x轴上移动

请看这里:

JS

    $(document).ready(function () {

    $('div').mousedown(function () {
        var dragElement = $(this);

        $(document).mousemove(function (e) {
            var elementWidth = dragElement.width() / 2;
            var elementHeight = dragElement.height() / 2;

            var x = e.pageX - elementHeight;
            var y = e.pageY - elementWidth;
            dragElement.css({top: y + 'px', left: x + 'px', backgroundColor: 'red'});

            $(document).on('keydown',function(e){
                if(e.which == 16){

                }

            });

        });
        $('div').mouseup(function () {
            $(document).unbind('mousemove');
            dragElement.css({backgroundColor:'gray'})
        })
    });

});
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
    div {
        position: absolute;
        width: 140px;
        height: 140px;
        background-color: gray;
        top: 0;
        left: 0;
    }

    div:hover, div:active {
        cursor: move;
    }
HTML

    $(document).ready(function () {

    $('div').mousedown(function () {
        var dragElement = $(this);

        $(document).mousemove(function (e) {
            var elementWidth = dragElement.width() / 2;
            var elementHeight = dragElement.height() / 2;

            var x = e.pageX - elementHeight;
            var y = e.pageY - elementWidth;
            dragElement.css({top: y + 'px', left: x + 'px', backgroundColor: 'red'});

            $(document).on('keydown',function(e){
                if(e.which == 16){

                }

            });

        });
        $('div').mouseup(function () {
            $(document).unbind('mousemove');
            dragElement.css({backgroundColor:'gray'})
        })
    });

});
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
    div {
        position: absolute;
        width: 140px;
        height: 140px;
        background-color: gray;
        top: 0;
        left: 0;
    }

    div:hover, div:active {
        cursor: move;
    }

所以你的意思是“补挂”功能是正确的,但你想让它只在按下shift键时移动?是吗?@AnujaAgarwal是的。我只是想让我的元素在用户按下shift键并移动鼠标时在x轴上移动,但如果没有,拖放应该正常进行