2 回答

TA貢獻1796條經驗 獲得超4個贊
object.style.width 屬性得到一個由數字 + px 字組成的字符串,而不僅僅是一個數字,如果你沒有 decrease 方法,你將有一個未定義的異常:
let width =200;
let diff = 2;
let intervalID = 0;
function increase() {
intervalID = setInterval(zoomIn, 20);
}
function zoomIn() {
if (width < 400) {
width = width + diff;
document.getElementById("img2").style.width = width + "px"; // You assign for example 30px as String
console.log(width);
console.log(document.getElementById("img2").style.width);
} else {
clearInterval(intervalID);
}
}

TA貢獻1876條經驗 獲得超7個贊
width = 1;
function increase() {
intervalID = setInterval(zoomIn, 20);
}
function zoomIn() {
if (width < 400) {
width++;
document.getElementById("img2").style.width = width;
console.log(width);
console.log(document.getElementById("img2").style.width);
} else {
clearInterval(intervalID);
}
}
increase();
例子在這里
添加回答
舉報