简体   繁体   English

使用overflow-y时,拖动项将被隐藏

[英]Dragging item gets hidden when overflow-y is used

I created a Fiddle to demonstrate the problem (also can be run inside this question, below). 我创建了一个小提琴来演示这个问题(也可以在下面这个问题中运行)。

I have a sidebar of playing card images that I want to drag into a main area. 我有一个侧边栏的扑克牌图片,我想拖到一个主要区域。 The sidebar holds a lot of cards so I want it to be scrollable. 侧边栏包含很多卡片,因此我希望它可以滚动。 However, when I give it a scroll feature, then when I drag a card, it gets hidden when I drag it out of the sidebar. 但是,当我给它一个滚动功能时,当我拖动一张卡片时,当我将它拖出侧边栏时它会被隐藏起来。

 var app = angular.module('myApp', ['ngDraggable']); app.controller('ctrl', function ($scope) { }); 
 #gallery-container { overflow-y: scroll; } .card { width: 100%; } 
 <link href="https://netdna.bootstrapcdn.com/bootstrap/3.0.0/css/bootstrap.min.css" rel="stylesheet"/> <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.1/angular.min.js"></script> <script src="https://rawgit.com/fatlinesofcode/ngDraggable/master/ngDraggable.js"></script> <div ng-app="myApp"> <div ng-controller="ctrl"> <div class="row"> <div class="col-xs-3"> <div id="gallery-container"> <div class="row"> <div class="col-sm-12"> <img ng-drag="true" ng-drag-data="hi" ng-drag-success="onDragComplete($data,$event)" ng-center-anchor="true" class="card" src="http://www.download32.com/images/screen/vector_playing_cards-467278.png" alt=""> </div> </div> <div class="row"> <div class="col-sm-12"> <img ng-drag="true" ng-drag-data="hi" ng-drag-success="onDragComplete($data,$event)" ng-center-anchor="true" class="card" src="http://www.download32.com/images/screen/vector_playing_cards-467278.png" alt=""> </div> </div> <div class="row"> <div class="col-sm-12"> <img ng-drag="true" ng-drag-data="hi" ng-drag-success="onDragComplete($data,$event)" ng-center-anchor="true" class="card" src="http://www.download32.com/images/screen/vector_playing_cards-467278.png" alt=""> </div> </div> <div class="row"> <div class="col-sm-12"> <img ng-drag="true" ng-drag-data="hi" ng-drag-success="onDragComplete($data,$event)" ng-center-anchor="true" class="card" src="http://www.download32.com/images/screen/vector_playing_cards-467278.png" alt=""> </div> </div> <div class="row"> <div class="col-sm-12"> <img ng-drag="true" ng-drag-data="hi" ng-drag-success="onDragComplete($data,$event)" ng-center-anchor="true" class="card" src="http://www.download32.com/images/screen/vector_playing_cards-467278.png" alt=""> </div> </div> <div class="row"> <div class="col-sm-12"> <img ng-drag="true" ng-drag-data="hi" ng-drag-success="onDragComplete($data,$event)" ng-center-anchor="true" class="card" src="http://www.download32.com/images/screen/vector_playing_cards-467278.png" alt=""> </div> </div> <div class="row"> <div class="col-sm-12"> <img ng-drag="true" ng-drag-data="hi" ng-drag-success="onDragComplete($data,$event)" ng-center-anchor="true" class="card" src="http://www.download32.com/images/screen/vector_playing_cards-467278.png" alt=""> </div> </div> </div> </div> <div class="col-xs-9"> <h2> Drop Area </h2> </div> </div> </div> </div> 

When I comment out overflow, like 当我评论溢出时,就像

/*overflow-y: scroll;*/

in the CSS, it now works. 在CSS中,它现在有效。

How can I both have a scrolling sidebar and drag items out of it? 我怎么能有一个滚动的侧边栏并拖出项目?

To drag objects between zones with scrolled or hidden overflows, you need a to create clone div between the drag div and the drop div using the ng-drag-clone 要在具有滚动或隐藏溢出的区域之间拖动对象,您需要使用ng-drag-clone在drag div和drop div之间创建clone div

        <div ng-drag-clone="">
            <img ng-src="{{clonedData .link}}" width="100px">
        </div>

check my Fiddle 检查我的小提琴

I've tried to update your code, but quickly to demonstrate the point: 我试图更新你的代码,但很快就证明了这一点:


I noticed you did not create any way for the drop zone to render or store the data being passed. 我注意到你没有创建任何方法让drop区渲染或存储传递的数据。

I created a cards array and a cardsDrop array to store the data. 我创建了一个卡片阵列和一个cardsDrop数组来存储数据。

I also implemented onDropComplete function to push the card object into cardsDrop 我还实现了onDropComplete函数将卡对象推送到cardsDrop

$scope.onDropComplete = function (data, evt) {
    var index = $scope.cards.indexOf(data);
    if (index == -1) $scope.cardsDrop.push(data);
}

and a onDragComplete function to remove a card from the original deck (for some applications, this is optional... sometimes you want a list to drag from that does not remove options): 和一个onDragComplete函数从原始套牌中删除一张卡(对于某些应用程序,这是可选的......有时你想要一个列表来拖动它不会删除选项):

$scope.onDragComplete = function (data, evt) {
    console.log("133", "$scope", "onDragSuccess1", "", evt);
    var index = $scope.cards.indexOf(data);
    if (index > -1) {
        $scope.cards.splice(index, 1);
    }

and I used ng-repeat to render each deck in the drag zone 我使用ng-repeat来渲染拖动区域中的每个牌组

                    <div class="row" ng-repeat="card in cards">
                        <img src="http://www.download32.com/images/screen/vector_playing_cards-467278.png" alt="" vertical-scroll="false" ng-drag="true" ng-drag-data="card" ng-drag-success="onDragComplete($data,$event)" ng-center-anchor="true" width="100px">
                    </div>

and drop zones: 和放置区:

        <div style="min-height: 300px;" class="col-xs-5" ng-drop="true" ng-scroll="false" ng-drag-move="true" ng-drop-success="onDropComplete($data,$event)">
            <draggableClone></draggableClone>
             <h2> Drop Area </h2>

            <div ng-repeat="card in cardsDrop" ng-drag-success="onDragComplete($data,$event)" class="card">
                <img src="http://www.download32.com/images/screen/vector_playing_cards-467278.png" class="card" alt="" width="100px">
            </div>
        </div>

In addition, I have added some styling to the ng-drag and ng-drop. 另外,我在ng-drag和ng-drop中添加了一些样式。

The ngDraggable library you're using does not support adding the element to a parent element (like document.body) once you start dragging. 您正在使用的ngDraggable库不支持在开始拖动后将元素添加到父元素(如document.body)。 You need that, otherwise the element can never leave the sidebar and keep on being visible. 需要它,否则元素永远不会离开侧边栏并继续可见。 That's how CSS works. 这就是CSS的工作原理。

What you could do is use another library that supports adding the draggable element to another element, like jQuery UI: 你可以做的是使用另一个支持将draggable元素添加到另一个元素的库,比如jQuery UI:

app.directive('draggable', function() {
    return function($scope, $element, $attrs) {

        $element.draggable({
            appendTo: 'body',
            stop: function(event, ui) {
                // Handle new position
            }
        });

    };
});

There are probably AngularJS wrappers for Jquery UI out there that do this for you in a more declarative style, or other ngDraggable alternatives that support this. 可能有Jquery UI的AngularJS包装器以更具声明性的方式为您执行此操作,或者支持此操作的其他ngDraggable替代方案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM