1

我想知道是否有人知道如何在按下鼠标时一遍又一遍地重复功能,我不知道如何使其工作。我知道在原型中你可以参加类似的活动

$('id').observe("click",function(event){}) 
$('id').observe("leave",function(event){})
$('id').observe("change",function(event){})
//etc...

但类似于$('id').observe("whilemousepress",function(event){}):P
//我知道 Javascript 中没有任何事件,但我想效仿。

4

4 回答 4

5

我无法对原型细节发表评论,但您可以通过使用setInterval()开始mousedown和停止使用.clearInterval()on创建间隔来做到这一点mouseup

于 2010-04-22T17:48:47.447 回答
1

鼠标按下时有一个事件,mousedown鼠标抬起时有一个事件mouseup。只需在按下鼠标时启动您的事件,并在释放按钮时停止它们。

$('id').observe("mousedown",function(event){
    // Do whatever you want
})

$('id').observe("mouseup",function(event){
    // Stop the events starts when the mouse when down
})
于 2010-04-22T18:04:00.513 回答
0

好的...我想两者都是正确的我所做的是:

        $('right').observe('mousedown',function(event){ intervalRigth = setInterval(this.name + ".dosomething()",50); }.bind(this));
        $('left').observe('mousedown',function(event){ intervalLeft = setInterval(this.name + ".dosomething()",50); }.bind(this));


        $('right').observe('mouseup',function(event){ clearInterval(intervalRigth ); }.bind(this));
        $('left').observe('mouseup',function(event){ clearInterval(intervalLeft ); }.bind(this));            

//所以我猜这两个答案的混合都是正确的谢谢=)

于 2010-04-22T18:32:39.980 回答
0

var MouseUtils = (function() {
  'use strict';
  // VARIABLES
  var _isScrolling = false;
  var _buttonsArray = [false, false, false, false, false, false, false, false, false]; // 0 left, 2 right, 1 middle, other.. extra buttons, gaming mouses
  var _mousePressed = false;

  //EVENT LISTENERS
  var _init = function(w, d) {
    w.onscroll = function() {
      _isScrolling = true;
    };
    d.onmousedown = function(e) {
      _buttonsArray[e.button] = true;
      _mousePressed = true;
    };
    d.onmouseup = function(e) {
      _buttonsArray[e.button] = false;
      _mousePressed = false;
    };
    d.oncontextmenu = function() { // this is mandatory for clicks down|ups works well
      return false;
    };
    return this;
  };

  // TIMERS
  var _scrollInterval = setInterval(function() {
    if(_isScrolling) {
      _isScrolling = false;
    }
  }, 500);

  // EXPOSED
  var _isLeftPressed = function() {
    return _buttonsArray[0];
  };
  var _isRightPressed = function() {
    return _buttonsArray[2];
  };
  var _isMiddlePressed = function() {
    return _buttonsArray[1];
  };
  var _isScrolling = function() {
    return _isScrolling;
  };

  var _clearScrollInterval = function() {
    clearInterval(_scrollInterval);
  };

  return {
    init: _init,
    isLeftPressed: _isLeftPressed,
    isRightPressed: _isRightPressed,
    isMiddlePressed: _isMiddlePressed,
    isScrolling: _isScrolling,
    clearScrollInterval: _clearScrollInterval
  };
})();

MouseUtils.init(window, document);

while(MouseUtils.isLeftPressed) {
   // DO SOMETHING
}

于 2015-09-17T16:58:01.697 回答