Tutorial for HTML5 drag&drop - sortable list

前端 未结 4 1428
攒了一身酷
攒了一身酷 2021-01-30 21:37

Do anyone know a really good tutorial for HTML5 drag&drop? Im making a toDo-list and I want to be able to reorder/sort it with this API. I\'ve been googling for it like a ma

相关标签:
4条回答
  • 2021-01-30 22:01

    If you're going with the solution by adamf (Mar 10 '15 at 11:16), and want to use it on table rows, replace the dragenter function to the following:

    function dragenter(e) {
        var target = e.target;
        while (target.parentNode.tagName != 'TBODY') {
            target = target.parentNode;
        }
    
        if (isbefore(source, target)) {
            target.parentNode.insertBefore(source, target);
        }
        else {
            target.parentNode.insertBefore(source, target.nextSibling);
        }
    }
    

    This way the target will only apply for TR elements, and not any of their child elements.

    The same thing would apply for ul > li strunctures, if the li elements have children.

    If there are img child elements, add a draggable="false" attribute to each.

    0 讨论(0)
  • 2021-01-30 22:18

    I've tried to keep this sample as simple as possible.

    If you create a HTML list:

    <ul>
      <li draggable="true" ondragover="dragOver(event)" ondragstart="dragStart(event)">Apples</li>
      <li draggable="true" ondragover="dragOver(event)" ondragstart="dragStart(event)">Oranges</li>
      <li draggable="true" ondragover="dragOver(event)" ondragstart="dragStart(event)">Bananas</li>
      <li draggable="true" ondragover="dragOver(event)" ondragstart="dragStart(event)">Strawberries</li>
    </ul>
    

    ...and the following javascript:

    var _el;
    
    function dragOver(e) {
      if (isBefore(_el, e.target))
        e.target.parentNode.insertBefore(_el, e.target);
      else
        e.target.parentNode.insertBefore(_el, e.target.nextSibling);
    }
    
    function dragStart(e) {
      e.dataTransfer.effectAllowed = "move";
      e.dataTransfer.setData("text/plain", null); // Thanks to bqlou for their comment.
      _el = e.target;
    }
    
    function isBefore(el1, el2) {
      if (el2.parentNode === el1.parentNode)
        for (var cur = el1.previousSibling; cur && cur.nodeType !== 9; cur = cur.previousSibling)
          if (cur === el2)
            return true;
      return false;
    }
    

    ... you should get a sortable list.

    You can try the code on https://codepen.io/crouchingtigerhiddenadam/pen/qKXgap

    Please be aware of the following bug in FireFox: https://developer.mozilla.org/en-US/docs/Web/Events/dragenter

    Hope this helps.

    0 讨论(0)
  • 2021-01-30 22:22

    For a beginning to end tutorial, check this out: http://taximeeting.tumblr.com/post/26539340142/lightweight-jquery-plugin-for-html5-sortable-lists.

    It's based on html5sortable: http://farhadi.ir/projects/html5sortable/. Another great tutorial on HTML5's drag and drop can be found here: http://www.html5rocks.com/en/tutorials/dnd/basics/.

    0 讨论(0)
  • 2021-01-30 22:23

    If you are looking to do this with table rows you need to make a slight change:

    https://jsfiddle.net/cmpenney/6rx6u2kf/

    <table>
        <tr draggable="true" ondragenter="dragenter(event)" ondragstart="dragstart(event)">
            <td style="border: 1px solid black">Apples</td>
            <td style="border: 1px solid black">A-Column2</td>
        </tr>
        <tr draggable="true" ondragenter="dragenter(event)" ondragstart="dragstart(event)">
            <td style="border: 1px solid black">Oranges</td>
            <td style="border: 1px solid black">O-Column2</td>
        </tr>
        <tr draggable="true" ondragenter="dragenter(event)" ondragstart="dragstart(event)">
            <td style="border: 1px solid black">Bananas</td>
            <td style="border: 1px solid black">B-Column2</td>
        </tr>
        <tr draggable="true" ondragenter="dragenter(event)" ondragstart="dragstart(event)">
            <td style="border: 1px solid black">Strawberries</td>
            <td style="border: 1px solid black">S-Column2</td>
        </tr>
    </table>
    
    
    
    var source;
    
    function isbefore(a, b) {
        if (a.parentNode == b.parentNode) {
            for (var cur = a; cur; cur = cur.previousSibling) {
                if (cur === b) {
                    return true;
                }
            }
        }
        return false;
    }
    
    function dragenter(e) {
        var targetelem = e.target;
        if (targetelem.nodeName == "TD") {
           targetelem = targetelem.parentNode;   
        }  
    
        if (isbefore(source, targetelem)) {
            targetelem.parentNode.insertBefore(source, targetelem);
        } else {
            targetelem.parentNode.insertBefore(source, targetelem.nextSibling);
        }
    }
    
    function dragstart(e) {
        source = e.target;
        e.dataTransfer.effectAllowed = 'move';
    }
    
    0 讨论(0)
提交回复
热议问题