简体   繁体   English

在鼠标事件之间传递数据

[英]Passing data between mouse events

How can one pass events between mousedown and mouseup specifically? 如何在mousedown和mouseup之间传递事件? In my mousedown event I create a circle and add it to the DOM, in my mouseup I want to animate the circle. 在我的mousedown事件中,我创建了一个圆并将其添加到DOM,在mouseup中,我想为该圆设置动画。 I was hoping to do this by referencing the node without attaching an ID, if possible. 我希望通过引用该节点而不附加ID(如果可能)来实现此目的。

Here's a snippet of my code: 这是我的代码片段:

// Mouse down
hitArea.addEventListener('mousedown', function(e) {
  var el = new createCircle();

  mask.appendChild(el);

  TweenMax.set(el, {
    transformOrigin: '50% 50%',
    x: e.clientX - hitDimensions.left, 
    y: e.clientY - hitDimensions.top
  });
});

// Mouse up
hitArea.addEventListener('mouseup', function(e) {

  // Reference to el needed
  animateCircle(el, e);

});

You can view my in progress code here: http://codepen.io/getreworked/pen/VjzyLL 您可以在此处查看我的进度代码: http : //codepen.io/getreworked/pen/VjzyLL

You can just use closures: 您可以只使用闭包:

var el;
hitArea.addEventListener('mousedown', function(e) {
  el = new createCircle();

  mask.appendChild(el);

  TweenMax.set(el, {
    transformOrigin: '50% 50%',
    x: e.clientX - hitDimensions.left, 
    y: e.clientY - hitDimensions.top
  });
});

// Mouse up
hitArea.addEventListener('mouseup', function(e) {

  if(el)
     animateCircle(el, e);

});

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

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