How to make an input field draggable [closed]

。_饼干妹妹 提交于 2019-12-08 07:45:18

问题


I have a dropdown/input field, but I want to put a draggable event into it. When I drag it for the first time, it moves smoothly, but when I start to edit the text inside the input field, I can't seem to drag it around anymore. I've been researching the on the draggable methods but no luck.

Basically I have a <div> wrapped around the dropdown/input field where the draggable event is attached.

<div class="draggable">
    <select>
       <option>
    </select>
    <input>
</div>

It's something like that. Below is the link for the code that has the dropdown/input field, but it still has the draggable event.

http://jsfiddle.net/yUxr4/406/

EDIT: Here is the updated code, but the draggable is not working. I don't know why. http://jsfiddle.net/9SPvQ/1/


回答1:


You need to dispatch event because jQuery UI draggable prevent drag behaviour on input element. Now, to still have input element responsive, you can use following workaround:

DEMO jsFiddle

$(".draggable").draggable({
    start: function( event, ui ) {
       $(this).data('preventBehaviour', true);
    }
});

$(".draggable :input").on('mousedown', function (e) {
    var mdown = document.createEvent("MouseEvents");
    mdown.initMouseEvent("mousedown", true, true, window, 0, e.screenX, e.screenY, e.clientX, e.clientY, true, false, false, true, 0, null);
    $(this).closest('.draggable')[0].dispatchEvent(mdown);
}).on('click', function(e){
    var $draggable = $(this).closest('.draggable');
    if($draggable.data("preventBehaviour")){
        e.preventDefault();
        $draggable.data("preventBehaviour", false)
    }
});


来源:https://stackoverflow.com/questions/22814073/how-to-make-an-input-field-draggable

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!