【问题标题】:angular Drag and Drop not working on mobile角度拖放在移动设备上不起作用
【发布时间】:2017-01-13 19:07:50
【问题描述】:

我正在使用这个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);
                    }
                }
            }]);

【问题讨论】:

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


    【解决方案1】:

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

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


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

    替代方案:

    angular-dragdrop

    可能是最著名的。这里是some examples。请注意,它们不适用于触摸设备。要支持触摸事件,您还应该添加touchpunch.js

    ngDraggable

    简单且适用于移动设备。你可以看到一个启用触摸的例子here

    ng-sortable

    在移动设备上工作。您可以查看启用触摸的示例here

    【讨论】:

    • 已尝试使用angular-dragdrop,但当我在 Google chrome 和 Mozilla Firefox 上模拟移动设备时它不起作用,而 ngDraggable 应使用此链接而不是 ngDraggable 而 ng-sortable 应使用此链接:@ 987654331@。但是,你最好想出用于 Angular 10 左右的。顺便说一句,尽管 CDK-Drag and Drop 适用于移动设备,但 CDK-Drag and Drop 也不符合我的拖放目的。
    【解决方案2】:

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

    使用 polyfill 意味着您正在使用 html 中已经建立的事件(例如 onDragStart 和 onDragEnd)。 然后,您可以按照 html 的方式使用内置的拖放功能。

    这是一篇关于为什么应该使用 polyfill 的好文章(由本答案中引用的 github 存储库的同一作者撰写) https://www.codeproject.com/Articles/1091766/Add-support-for-standard-HTML-Drag-and-Drop-operat

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

    【讨论】:

    • 看起来应该可以了。但是,我只是想知道如何使它与 Angular 10 一起使用,即使它可以与 AngularJS(AKA Angular 1.x)一起使用
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-01-07
    • 2020-07-09
    • 2021-06-02
    • 2020-01-13
    • 1970-01-01
    相关资源
    最近更新 更多