可以使用 clientHeight 代替等于运算符中的数值,例如我下面的示例吗?
function DOWN() {
var mC = document.getElementById("tester");
var cT = mC.scrollTop;
var pos = 0;
var poscB = mC.clientHeight;
var Ti = setInterval(frame, 15);
function frame() {
if (pos == poscB){
clearInterval(Ti);
} else {
pos++;
mC.style.top = pos + 'px';
}
}
}
是的,clientHeight 返回一个整数,以像素为单位表示元素的高度,可用于数值运算。
注意:此属性会将值四舍五入为整数。如果需要小数值,请使用 element.getBoundingClientRect()。
https://developer.mozilla.org/en-US/docs/Web/API/Element/clientHeight
如果您将 typeof(mc.clientHeight) 打印到控制台,您将看到“数字”
console.log(typeof(mc.clientHeight));
在你的例子中:
if (pos == cB) {
clearInterval(Ti);
} else {
pos++;
mC.style.top = pos + 'px';
}
我想你的意思可能是:
if (pos == poscB) {
clearInterval(Ti);
} else {
pos++;
mC.style.top = pos + 'px';
}
除非您在脚本的其他地方定义了示例中没有的 cB
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句