我有一個畫布。我必須縮放它,直到按下放大按鈕並縮小它,直到按下縮小按鈕。我已經設法讓它工作,但它延遲工作。並且即使在我釋放按鈕時它也會保持放大或縮小。這是我到目前爲止所嘗試的。請幫幫我。您可以通過Here在現場查看。Jquery放大和縮小的工作,而不是光滑的跳動
<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>
它適用於我在Chrome上正常工作!你的瀏覽器是什麼? –
你有錯誤的地方清理。 – Jai
我使用firefox .. –