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();
例子在这里
添加回答
举报