【问题标题】:Hover and onclick functionality issue in jqueryjquery中的悬停和点击功能问题
【发布时间】:2017-09-12 05:16:43
【问题描述】:

我想同时拥有 onclick 和 hover 功能,但是如果您点击了某个地方,那么在我点击其他地方之前,悬停应该不起作用。我已经尝试了很多,但我没有找到任何工作代码。请帮忙

 canvas.addEventListener('mousedown', function(evt) {
    }, false);
canvas.onmousemove = function(evt) {   

 };

【问题讨论】:

  • 请向我们提供您尝试过的东西
  • 我已经添加了代码。请检查
  • 那不是 jquery 或 node。
  • 您可以在点击时拨打两个电话,悬停只是一个事件。因此,在单击时,您可以进行单击调用和悬停调用(假设悬停调用只是需要发生的 css 或函数调用)。我就是这么理解的。

标签: javascript jquery node.js


【解决方案1】:

好吧,我不太确定您需要什么以及为什么需要它。但是,在你写的那段简短的代码中,我看到了“canvas”这个词,并想“这到底是怎么回事,这可能很有趣!”。之前我对 canvas 元素没有太多经验,所以我意识到可能有更好的方法来编写这段代码。

但是,我希望我在下面的示例中所写的内容至少接近您所寻找的内容。否则,发疯,改变和适应你喜欢的方式......当你这样做的时候,试着从中学习一些东西。

var Canvas = function() {
  this.$canvas = $('canvas');
  this.$currPos = $('#currPos');
  this.$currClick = $('#currClick');
  this.$clickInfo = $('#clickInfo');
  
  this.canvsWidth = 150;
  this.cavasHeight = 150;
  this.ctx = ctx = this.$canvas[0].getContext('2d');
  this.rect = this.$canvas[0].getBoundingClientRect();
  this.squares = [];
  this.sqm = 50;
  
  this.tracker = 0;
  this.latestHover = {};
  
  this._events();
  this._prepare();
};

Canvas.prototype._events = function() {
  var self = this;
  
  this.$canvas.on('mousemove', function(e) {
    var posX = e.clientX - self.rect.left,
        posY = e.clientY - self.rect.top,
        newX = Math.floor(posX / self.sqm),
        newY = Math.floor(posY / self.sqm);
    
    if($.isEmptyObject(self.latestHover) || (self.latestHover.x !== newX || self.latestHover.y !== newY)) {
      self.latestHover.x = newX;
      self.latestHover.y = newY;
      
     self.squares.map(function(k, v) {
      let obj = self.squares[v];
      if(!obj.fixedBackground) obj.reverseBackgroundColor();
     });

      var square = self.findObject(newX, newY)[0];
      if(square) {
        square.setBackgroundColor('#ff0000');
        
        self.$currPos.html(newX +'x'+ newY);
        self._redraw();
      }
      
    }
  });
  
  this.$canvas.on('click', function() {
    if(self.tracker === 2) {
      return self._reset();
    }
  
    if(!($.isEmptyObject(self.latestHover))) {
      var x = self.latestHover.x,
          y = self.latestHover.y;
          
      var square = self.findObject(x, y)[0];
          square.setFixedBackground();
          
      self.$currClick.html(x +'x'+ y);
      self.setTracker();
    }
  });
  
};

Canvas.prototype._prepare = function() {
  for(var row = 0; row < 3; row++) {
    for(var col = 0; col < 3; col++) {
      this.squares.push(new Square(row, col, this.ctx, this.sqm));
    }
  }
};

Canvas.prototype._redraw = function() {
  var self = this;
  
  this.ctx.clearRect(0, 0, this.canvasWidth, this.canvasHeight);

  this.squares.filter(function(k, v) {
    self.squares[v].draw();
  });
};

Canvas.prototype.setTracker = function() {
  this.tracker++;
  
  if(this.tracker === 2) this.$clickInfo.html('Click one more time to start over');
};

Canvas.prototype.findObject = function(x, y) {
  var self = this;
  
  return square = self.squares.filter(function(k, v) {
      var obj = self.squares[v];
      if(obj.posX === x && obj.posY === y) return obj;
    });
};

Canvas.prototype._reset = function() {
  var self = this;
  
  this.squares.map(function(k, v) {
    let obj = self.squares[v];
        obj.reverseBackgroundColor();
        obj.unsetFixedBackground();
  });
  
  this.$currClick.html('');
  this.$clickInfo.html('');
  this.tracker = 0;
  this._redraw();
};

var Square = function(x, y, ctx, sqm) {
  this.ctx = ctx;
  this.sqm = sqm;
  this.posX = x;
  this.posY = y;
  this.background = '#fff';
  this.strokeThickness = 1;
  this.fixedBackground = false;

  this.draw();
};

Square.prototype.setBackgroundColor = function(color) {
  return this.background = color;
};

Square.prototype.reverseBackgroundColor = function() {
  return this.background = '#fff';
};

Square.prototype.setFixedBackground = function() {
  return this.fixedBackground = true; 
};

Square.prototype.unsetFixedBackground = function() {
  return this.fixedBackground = false; 
};

Square.prototype.draw = function() {
  this.ctx.fillStyle = this.background;
  this.ctx.fillRect(this.posX * this.sqm, this.posY * this.sqm, this.sqm, this.sqm);
  this.ctx.strokeRect(this.posX * this.sqm, this.posY * this.sqm, this.sqm, this.sqm);
};

window.Canvas = new Canvas();
canvas {
  border: 1px solid #ccc;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<canvas width="150" height="150"></canvas>
<div>
  Current position: <span id="currPos"></span> <br/>
  Last click: <span id="currClick"></span> <span id="clickInfo"></span>
</div>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-06-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多