Prevent drag event to interfere with input elements in Firefox using HTML5 drag/drop

前端 未结 5 1263
猫巷女王i
猫巷女王i 2020-12-18 22:56

It seems that an input element loses a lot of functionality when put into an element with draggable=\"true\". This only seems to occur in firefox.

See my jsfiddle: h

相关标签:
5条回答
  • 2020-12-18 23:31

    I used the onMouseEnter and onMouseLeave functions on the textarea to set the div draggable only when the mouse is outside the textarea.

    I did this because I needed the focus to stay in the edit fields while dragging and dragging itself does not trigger a focus event.

    0 讨论(0)
  • 2020-12-18 23:34

    See Firefox defect.

    As an alternative, setting the draggable="false" on input focus event and replacing back to draggable="true" on input blur event works.

    See jsfiddle for an example without any framework.

    HTML:

    <div draggable="true" id="draggableDiv">
        <textarea onfocus="onFocus();" onblur="onBlur();">Inside draggable (FIXED)</textarea>
    </div>
    

    JS:

    onFocus= function(e) {
        document.getElementById("draggableDiv").setAttribute("draggable", "false");
    }
    onBlur= function(e) {
        document.getElementById("draggableDiv").setAttribute("draggable", "true");
    }
    
    0 讨论(0)
  • 2020-12-18 23:41

    If you are like me and come across this issue, and are using Sortable.js, you can use the filter option to specify elements that won't trigger dragging, and thus allow the input to operate normally.

    JQuery:

    $('#my-sortable').sortable({
        filter: ".my-text-input",   // Or whatever class you specify
        preventOnFilter: false      // Allow the input to operate normally
    });
    

    You can also find this information from the list of Sortable.js options found here: https://github.com/SortableJS/sortablejs

    0 讨论(0)
  • 2020-12-18 23:43

    I have also found using onmouseenter and onmouseleave to toggle the draggable attribute works better because it places the cursor in the input box where you actually click. When using onfocus/onblur, the cursor always goes to the start or end of the text even if you click in the middle.

    0 讨论(0)
  • 2020-12-18 23:46

    As far as I know this is a known bug in FF. A quick (and "dirty" workaround) would be to remove the draggable attribute on text input focus event, add it again on text input blur event, and disable text selection on #drag div to enable dragging once you clicked outside the focused input (clicking on #div directly).

    Updated fiddle here.

    Sample code:

    JS:

    $('#message')
        .on('focus', function(e) {
            $(this).closest('#drag').attr("draggable", false);
        })
        .on('blur', function(e) {
            $(this).closest('#drag').attr("draggable", true);
        });
    

    CSS:

    .disable-selection {
        /* event if these are not necessary, let's just add them */
        -webkit-user-select: none;
        -ms-user-select: none;
        user-select: none;
    
        /* this will add drag availability once you clicked the 
           #drag div while you're focusing #message div */
        -moz-user-select: none;
    }
    

    Hope it could help you.

    0 讨论(0)
提交回复
热议问题