我想拖放一張卡片的圖像在javascript的畫布上,但mouseup事件似乎沒有工作,即使它在main()內部。一旦卡被選中,它會圍繞着鼠標,但當我放開鼠標時它似乎並沒有放開。我也知道,圖像是從不清除屏幕重複。Javascript mouseup和mousedown事件
function main(){
var ctx = cvs.getContext("2d");
var img = new Image();
img.src = "allcards.png";
var imgX = 75;
var imgY = 75;
draw();
function draw(){
ctx.drawImage(img,0,0,97,129,imgX,imgY,100,100);
}
cvs.addEventListener("mouseup", function(ev){
greaterX = false;
lessX = false;
greaterY = false;
lessY = false;
}
);
cvs.addEventListener("mousedown", function(ev){
if(ev.clientX <= (imgX + 97)){
var greaterX = true;
}
if(ev.clientY <= (imgY + 129)){
var greaterY = true;
}
if(ev.clientX >= imgX){
var lessX = true;
}
if(ev.clientY >= imgY){
var lessY = true;
}
if(greaterX == true)
if(greaterY == true)
if(lessX == true)
if(lessY == true)
cvs.addEventListener("mousemove", function(ev){
var offsetX = (ev.clientX - imgX);
var offsetY = (ev.clientY - imgY);
imgX = imgX + offsetX;
imgY = imgY + offsetY;
draw();
});
});
};
你不能使用JQuery?它會處理他爲你粘貼的所有代碼:http://jqueryui.com/demos/draggable/ –
@Lando OP正在使用Canvas,屏幕上放置的圖像僅使用代碼創建。 jQuery的DOM選擇器將[沒有幫助](http://jsfiddle.net/SeRcf/)。 – Polyov