我有一块帆布.我必须缩放它直到按下放大按钮并缩小它直到按下缩小按钮.我设法让它工作,但它适用于延迟.即使我已经释放按钮,它也会一直放大或缩小.这是我到目前为止所尝试的.请帮我.您可以在现场超过
Here查看.
<script language="javascript">
var timeout, clicker = $('#zoomin'),
clicker2 = $('#zoomout');
clicker.mousedown(function() {
timeout = setInterval(function() {
var wd = $("#canvas").width();
var ht = $("#canvas").height();
wd = parseInt(wd) + 250;
ht = parseInt(ht) + 250;
$("#canvas").animate({
width: wd,
height: ht
});
var ctx = canvas.getContext("2d");
ctx.drawImage(backgroundImage, 0, 0, wd - 1, ht - 1);
ctx.drawImage(outlineImage, 0, 0, wd - 1, ht - 1);
redraw();
}, 500);
return false;
});
clicker2.mousedown(function() {
timeout = setInterval(function() {
var wd = $("#canvas").width();
var ht = $("#canvas").height();
wd = parseInt(wd) - 250;
ht = parseInt(ht) - 250;
if (wd < 500) {
return false;
}
$("#canvas").animate({
width: wd,
height: ht
});
var ctx = canvas.getContext("2d");
ctx.drawImage(backgroundImage, 0, 0, wd - 1, ht - 1);
ctx.drawImage(outlineImage, 0, 0, wd - 1, ht - 1);
redraw();
}, 500);
return false;
});
$(document).mouseup(function() {
clearInterval(timeout);
return false;
});
$("#zoomout").mousedown(function() {
var wd = $("#canvas").width();
var ht = $("#canvas").height();
wd = parseInt(wd) - 250;
ht = parseInt(ht) - 250;
if (wd < 500) {
return false;
}
$("#canvas").animate({
width: wd,
height: ht
});
var ctx = canvas.getContext("2d");
ctx.drawImage(backgroundImage, 0, 0, wd - 1, ht - 1);
ctx.drawImage(outlineImage, 0, 0, wd - 1, ht - 1);
redraw();
console.log(wd + ":" + ht);
});
$("#zoomin").click(function() {
var wd = $("#canvas").width();
var ht = $("#canvas").height();
wd = parseInt(wd) + 250;
ht = parseInt(ht) + 250;
$("#canvas").animate({
width: wd,
height: ht
});
var ctx = canvas.getContext("2d");
ctx.drawImage(backgroundImage, 0, 0, wd - 1, ht - 1);
ctx.drawImage(outlineImage, 0, 0, wd - 1, ht - 1);
redraw();
console.log(wd + ":" + ht);
});
$("#zoomout").click(function() {
var wd = $("#canvas").width();
var ht = $("#canvas").height();
wd = parseInt(wd) - 250;
ht = parseInt(ht) - 250;
if (wd < 500) {
return false;
}
$("#canvas").animate({
width: wd,
height: ht
});
var ctx = canvas.getContext("2d");
ctx.drawImage(backgroundImage, 0, 0, wd - 1, ht - 1);
ctx.drawImage(outlineImage, 0, 0, wd - 1, ht - 1);
redraw();
console.log(wd + ":" + ht);
});
</script>
最佳答案 禁用/删除您的点击处理程序.
在鼠标释放时触发click事件.
这意味着,你正在做的是,当用户释放鼠标时,你调用jQuery来进一步缩放画布.
如果删除单击处理程序,则在鼠标释放后,画布不应继续动画.