简体   繁体   English

greensock可拖动/触发点击和拖动

[英]greensock draggable / triggering click and drag

I have an issue which is related to greensock animation but also may be a more general js issue in that I need to transfer a mousedown event to a second draggable instance after killing the first draggable instance. 我有一个与greensock动画有关的问题,但也可能是一个更一般的js问题,因为在杀死第一个可拖动实例之后,我需要将mousedown事件转移到第二个可拖动实例。

the codepen hopefully will illustrate what i am trying to do.. code is underneath. Codepen希望可以说明我正在尝试做的事情。代码在下面。

Codepen URL: http://codepen.io/anon/pen/ypdGn Codepen网址: http ://codepen.io/anon/pen/ypdGn

var detachdraggable;
var rotatedraggable;

function makeRotateDraggable() {


    // create rotate draggable for the cards..
    rotatedraggable = Draggable.create(".dragclone", {
        type:"rotation", 
        throwProps:true, 
        dragResistance : 0,
        edgeResistance : 1,
        bounds:{minRotation:-60, maxRotation:60},
        onDragStart : function() {
            var $el = $(this.target);
            var cardStartAngle = $el.data('startangle');
        },
        onDrag: function() {

            currentCardAngle = this.rotation;

            var $el = $(this.target);
            var cardStartAngle = $el.data('startangle');

            cardDirection = ( currentCardAngle > cardStartAngle ) ? "up":"down";
            cardTravelDegrees =  Math.abs(currentCardAngle - cardStartAngle);   

            this.vars.type = "x";       
        },
        onDragEnd: function() {

        },
        onClick: function() {
          return false;
        }

    });

    $('.dragclone').mousedown(function() { 
        $('.dragclone').css('z-index','10');
        rotatedraggable[0].enable();        
    });

    $('.dragclone').mouseout(function() { 
        detachdraggable[0].disable();
        $('.dragclone').trigger('mousedown');
    });

    $('.dragclone').trigger('mouseout');
}



// first setup the x,y draggable..
detachdraggable = Draggable.create('.dragclone', {
      type: "x,y",
      edgeResistance:1,
      throwProps:true,
      onPress:function() {
        startX = this.x;
        startY = this.y;    
      },
      onDrag:function() {  

        var xChange = this.x - startX,
            yChange = this.y - startY,
            ratio = Math.abs(xChange / yChange),
            direction = [];

        // NB:: you can adjust these ratio thresholds to make things 
        // more or less sensitive to diagonal movement. 
        if (ratio > 0.25) { 
          direction.push((xChange < 0) ? "left" : "right");
        }
        if (ratio < 4) {
          direction.push((yChange < 0) ? "up" : "down");
        }

        if(direction[0] == "left") {
            // TweenMax.to( $('.cloned'), .0001, {right:-xChange + 135});           
        }

        // moving up so lets switch cards !!
        if(direction[0] == "up") {

              if(Math.abs(yChange) > 20) {    
                   makeRotateDraggable();
              }

        }

      },
      onDragEnd:function() {
        // driftDragCardBack();
          // if(!cardPopping) { driftClonedCardBack(); }
      },
      onThrowComplete: function() {
      }
    });

I am battling to switch between 2 draggables setup on same element, am wondering if this is possible at all. 我正在努力在同一元素的2个可拖动对象设置之间切换,想知道这是否完全可能。 basically the codepen has an example of what I want to do, but it isnt seamless. 基本上,codepen给出了我要执行的操作的示例,但是它不是无缝的。 draggable is setup for element of type x,y, what i want to do is when the drag direction is up kill the type x,y draggable and switch to a type: rotation draggable so that the card moves around on an axis. draggable是为x,y类型的元素设置的,我要做的是在向上拖动方向时杀死类型为x,y的可拖动对象并切换为以下类型:可拖动旋转,以使卡在轴上移动。 you can see mine does that, but only if you release and then click again - is there any way to make this seamless, so it just switches mid-drag ? 您可以看到我的功能,但前提是您先松开然后再次单击-有没有任何方法可以使它无缝连接,所以仅在中间拖动时切换?

thanks, Justin 谢谢,贾斯汀

See http://codepen.io/anon/pen/klanu http://codepen.io/anon/pen/klanu

I've never used greensock but just had a look at their document: 我从没使用过greensock,只是看了看他们的文档:

https://greensock.com/docs/#/HTML5/Drag/Draggable/startDrag/ https://greensock.com/docs/#/HTML5/Drag/Draggable/startDrag/

First of all you had a couple of issues within your code. 首先,您的代码中有几个问题。 You don't need to create rotatedraggable inside a function, just create it, it's not enabled anyways. 您无需在函数内部创建rotatedraggable ,只需创建它,无论如何都不会启用它。 I also moved 我也感动了

$('.dragclone').mousedown(function() { 
    $('.dragclone').css('z-index','10');
    detachdraggable[0].enable();
});

outside the function. 功能之外。

In your 2nd draggable, you were calling createRotation to create the rotation but like I said you can create it at the start. 在第二个可拖动对象中,您调用了createRotation来创建旋转,但是就像我说的那样,您可以在开始时创建它。 When the div is moved to the top, I just disabled the current drag and enabled the 1st one and called dragStart on it. 当div移动到顶部时,我只是禁用了当前拖动并启用了第一个拖动,并在其上调用了dragStart e is what's passed to drag of the 2nd. e是传递给第二个拖动的东西。

if(Math.abs(yChange) > 20) {    
    detachdraggable[0].disable();
    rotatedraggable[0].enable();        
    rotatedraggable[0].startDrag(e);        
}

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

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