代码之家  ›  专栏  ›  技术社区  ›  ncubica

当鼠标按下事件时。原型js或javascript

  •  1
  • ncubica  · 技术社区  · 14 年前

    我想知道,如果有人知道如何使一个功能重复一遍,而鼠标是按下,我不知道如何使它工作。我知道在原型里你可以

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

    但是有点像 $('id').observe("whilemousepress",function(event){}) :p//我知道javascript中没有任何事件,但我想模拟。

    谢谢。。。

    4 回复  |  直到 8 年前
        1
  •  5
  •   gnarf    14 年前

    我不能对原型细节进行评论,但是您可以通过使用 setInterval() 开始于 mousedown 停止使用 .clearInterval() mouseup .

        2
  •  1
  •   John Conde    14 年前

    当鼠标按下时有一个事件 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
    })
    
        3
  •  0
  •   ncubica    14 年前

    好啊。。。我想两者都是对的我所做的是:

            $('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));            
    

    //所以我想两个答案都是正确的谢谢=)

        4
  •  0
  •   navid SilverHorn    8 年前

    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
    }
    推荐文章