【问题标题】:JQuery Trigger in JavascriptJavascript 中的 JQuery 触发器
【发布时间】:2011-12-15 02:11:34
【问题描述】:

我想问一下,javascript中的触发器相当于什么?

例如

$(this).trigger('move', 
  {
     x: event.pageX, 
     y: event.pageY
  }
);

【问题讨论】:

    标签: javascript jquery events triggers


    【解决方案1】:

    jQuery 的animate 应该能够完成你上面的工作:

    $(this).animate({ "left": event.pageX, "top": event.pageY }, 1000);
    

    或者瞬间移动它

    $(this).animate({ "left": event.pageX, "top": event.pageY }, 0);
    

    只要确保你移动的任何东西都设置了position:absolute

    【讨论】:

    • @AJNaidas - 我知道,但看起来您正在尝试将元素移动到新位置。上面的代码应该做到这一点。
    • $('#camera').bind('move-viewport', function(evt,movedMouse) - 我想用javascript调用那个函数。我会把它转换成cameraVar .addEventListener('move-viewport', function(evt,movedMouse) 之后。
    • @AJNaidas:你为什么不能使用trigger
    【解决方案2】:

    这里的想法是在绑定时保存指向回调函数的指针,然后在触发时简单地调用该回调函数。

    一个非常简单的实现是:

    ({
    
      // save the function in an internal _callbacks object
      bind: function(name, callback) {
        this._callbacks || (this._callbacks = {});
        this._callbacks[name] || (this._callbacks[name] = []);
        this._callbacks[name].push(callback);
        return this;
      },
    
      // when triggering, look up the function and call it with the current context
      trigger: function(ev) {
        var callback, i;
        for (i = 0; _i < this._callbacks[ev]; i++) {
          callback = this._callbacks[ev][i];
          callback.apply(this, args); // this is the 'trigger' magic
        }
        return true;
      }
    
    });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-11-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-06-25
      • 1970-01-01
      • 2012-02-13
      相关资源
      最近更新 更多