角度拖放不适用于移动设备

时间:2016-09-06 10:43:40

标签: javascript jquery angularjs angularjs-directive drag-and-drop

我正在使用此lvlDragDrop plugin。它不适用于移动平台。 在github上,他们添加了这个issue。但我还是没有运气。

Github Demo

HTML

<div ng-controller="ddController" style="margin-top:50px;">
            <div class="row">
                <div class="col-md-1 col-md-offset-1">
                    <p>Click and drag a color onto the grid to the right</p>
                    <div class="peg green" x-lvl-draggable="true" data-color="green">Green</div>
                    <div class="peg red" x-lvl-draggable="true" data-color="red">Red</div>
                    <div class="peg blue" x-lvl-draggable="true" data-color="blue">Blue</div>
                    <div class="peg black" x-lvl-draggable="true" data-color="black">Black</div>
                    <div class="peg grey" x-lvl-draggable="true" data-color="grey">Grey</div>
                </div>

                <div class="col-md-10">
                    <div ng-repeat="r in [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]">
                        <span class="slot circle" ng-repeat="c in [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]" x-lvl-drop-target="true" x-on-drop="dropped(dragEl, dropEl)"></span>
                    </div>
                </div>
            </div>
        </div>

JS

angular.module('ddApp', ['lvl.directives.dragdrop']) // register the directive with your app module
            .controller('ddController', ['$scope' , function($scope){
                $scope.dropped = function(dragEl, dropEl) { // function referenced by the drop target
                    //this is application logic, for the demo we just want to color the grid squares
                    //the directive provides a native dom object, wrap with jqlite
                    var drop = angular.element(dropEl);
                    var drag = angular.element(dragEl);

                    //clear the previously applied color, if it exists
                    var bgClass = drop.attr('data-color');
                    if (bgClass) {
                        drop.removeClass(bgClass);
                    }

                    //add the dragged color
                    bgClass = drag.attr("data-color");
                    drop.addClass(bgClass);
                    drop.attr('data-color', bgClass);

                    //if element has been dragged from the grid, clear dragged color
                    if (drag.attr("x-lvl-drop-target")) {
                        drag.removeClass(bgClass);
                    }
                }
            }]);

2 个答案:

答案 0 :(得分:8)

该指令不支持触摸屏。正如GitHub所述:

  

移动设备的最大问题是重写处理程序   各种触摸事件。它应该是可行的,但我没有   尝试。

尝试调整它会浪费你的时间。还有其他支持触摸事件的拖放指令。你可以看看:

<强>备选方案:

angular-dragdrop

可能最有名。这是some examples。请注意,他们不使用触摸设备。要支持触摸事件,您还应添加touchpunch.js

ngDraggable

简单并适用于移动设备。您可以看到启用触摸的示例here

ng-sortable

适用于移动设备。您可以看到启用触摸的示例here

答案 1 :(得分:0)

我会在你的应用程序中添加一个“拖放”polyfill。像这个: https://github.com/Bernardo-Castilho/dragdroptouch

使用polyfill意味着您正在利用html中已建立的事件(例如onDragStart和onDragEnd)。 然后你就可以使用内置的拖放功能来实现html的意思。

这是一篇很好的文章,说明为什么要使用polyfill(由本答案中引用的github repo的同一作者编写) https://www.codeproject.com/Articles/1091766/Add-support-for-standard-HTML-Drag-and-Drop-operat

使用npm安装它:npm install drag-drop-touch-polyfill 并在您的应用程序中包含该脚本。

相关问题