Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/83.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript jquery中的悬停和onclick功能问题_Javascript_Jquery_Node.js - Fatal编程技术网

Javascript jquery中的悬停和onclick功能问题

Javascript jquery中的悬停和onclick功能问题,javascript,jquery,node.js,Javascript,Jquery,Node.js,我希望同时具有onclick和hover功能,但如果您单击了某个位置,则在我单击其他位置之前,hover不应工作。我已经尝试了很多,但我没有找到任何工作代码。好心帮忙 canvas.addEventListener('mousedown', function(evt) { }, false); canvas.onmousemove = function(evt) { }; 嗯,我不太清楚你需要什么,为什么需要它。但是,在你写的那段短代码中,我看到了“canvas”这个词,并

我希望同时具有onclick和hover功能,但如果您单击了某个位置,则在我单击其他位置之前,hover不应工作。我已经尝试了很多,但我没有找到任何工作代码。好心帮忙

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

 };

嗯,我不太清楚你需要什么,为什么需要它。但是,在你写的那段短代码中,我看到了“canvas”这个词,并想“见鬼,那会很有趣!”。从前面开始,我对canvas元素没有太多经验,所以我意识到可能有更好的方法来编写这段代码

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

var Canvas=function(){
这个.$canvas=$('canvas');
这个.$currPos=$(“#currPos”);
这个.$currClick=$(“#currClick”);
这是。$clickInfo=$(“#clickInfo”);
这个.canvsWidth=150;
这个.cavasHeight=150;
this.ctx=ctx=this.$canvas[0].getContext('2d');
this.rect=this.$canvas[0].getBoundingClientRect();
此值为.squares=[];
这个平方米=50;
这是0.tracker=0;
this.latestHover={};
这个;
这个;
};
Canvas.prototype.\u events=function(){
var self=这个;
此.canvas.on('mousemove',函数(e){
var posX=e.clientX-self.rect.left,
posY=e.clientY-self.rect.top,
newX=数学楼层(posX/self.sqm),
newY=数学楼层(posY/self.sqm);
if($.isEmptyObject(self.latestHover)| |(self.latestHover.x!==newX | | self.latestHover.y!==newY)){
self.latestHover.x=newX;
self.latestHover.y=newY;
自平方映射(函数(k,v){
设obj=自平方[v];
如果(!obj.fixedBackground)对象反向背景颜色();
});
var square=self.findObject(newX,newY)[0];
if(方形){
方形.收根底色('ff0000');
self.$currPos.html(newX+'x'+newY);
self._redraw();
}
}
});
此.$canvas.on('click',function(){
如果(self.tracker==2){
返回自我。_reset();
}
if(!($.isEmptyObject(self.latestHover))){
var x=self.latestHover.x,
y=自最晚悬停时间y;
var square=self.findObject(x,y)[0];
square.setFixedBackground();
self.$currClick.html(x+'x'+y);
self.setTracker();
}
});
};
Canvas.prototype.\u prepare=function(){
对于(变量行=0;行<3;行++){
for(变量col=0;col<3;col++){
this.squares.push(新的正方形(row,col,this.ctx,this.sqm));
}
}
};
Canvas.prototype.\u redraw=function(){
var self=这个;
this.ctx.clearRect(0,0,this.canvasWidth,this.canvasHeight);
这个平方滤波器(函数(k,v){
self.squares[v].draw();
});
};
Canvas.prototype.setTracker=函数(){
这个.tracker++;
如果(this.tracker==2)this.$clickInfo.html('再次单击以重新开始');
};
Canvas.prototype.findObject=函数(x,y){
var self=这个;
返回平方=self.squares.filter(函数(k,v){
var obj=自平方[v];
如果(obj.posX==x&&obj.posY==y)返回obj;
});
};
Canvas.prototype.\u reset=function(){
var self=这个;
此.squares.map(函数(k,v){
设obj=自平方[v];
obj.reverseBackgroundColor();
obj.unsetFixedBackground();
});
这是.$currClick.html(“”);
这是.$clickInfo.html(“”);
这是0.tracker=0;
这个;
};
变量平方=函数(x,y,ctx,sqm){
this.ctx=ctx;
本图中,平方米=平方米;
this.posX=x;
this.posY=y;
this.background='#fff';
该参数为:strokeThickness=1;
this.fixedBackground=false;
这个.draw();
};
Square.prototype.setBackgroundColor=函数(颜色){
返回this.background=color;
};
Square.prototype.reverseBackgroundColor=函数(){
返回this.background='#fff';
};
Square.prototype.setFixedBackground=函数(){
返回this.fixedBackground=true;
};
Square.prototype.unsetFixedBackground=函数(){
返回this.fixedBackground=false;
};
Square.prototype.draw=函数(){
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);
};
Canvas=newcanvas()
画布{
边框:1px实心#ccc;
}

当前职位:
最后一次单击:
请向我们提供您在添加代码之前已经尝试过的内容。请检查这不是jquery或node。您可以在单击时进行两次调用,悬停只是一个事件。因此,点击您可以进行点击调用和悬停调用(假设悬停调用只是需要发生的css或函数调用)。我就是这样理解的。