【发布时间】:2019-03-28 12:43:59
【问题描述】:
我正在使用 vanilla javascript 创建自己的滚动条组件。元素是可调整大小的,所以当它调整大小时,滚动条的最大滚动位置和大小将随之而来。
如果我以正常速度调整大小,一切都很好,但如果我快速测试,计算结果(大小和最大位置)会产生错误的结果,我猜是因为offsetWidth 或offsetHeight 错误。
我的滚动条的渲染功能在调整大小时触发,sbv表示滚动条垂直,sbh表示滚动条水平:
this.sbv.size = this.content.offsetHeight / this.content.scrollHeight;
this.sbv.style.height = (this.sbv.size * 100) + "%";
this.sbv.max = (100 - this.sbv.size * 100) / 100 * this.content.offsetHeight;
this.sbv.pos = this.content.scrollTop / this.content.scrollHeight * this.content.offsetHeight;
this.sbv.style.top = (this.sbv.pos / this.content.offsetHeight * 100) + "%";
this.sbh.size = this.content.offsetWidth / this.content.scrollWidth;
this.sbh.style.width = (this.sbh.size * 100) + "%";
this.sbh.max = (100 - this.sbh.size * 100) / 100 * this.content.offsetWidth;
this.sbh.pos = this.content.scrollLeft / this.content.scrollWidth * this.content.offsetWidth;
this.sbh.style.left = (this.sbh.pos / this.content.offsetWidth * 100) + "%";
有人知道为什么会这样吗?钛酸
【问题讨论】:
-
你应该去抖动这些事件。
-
我使用了
setTimeout或requestAnimationFrame,但仍然无法正常工作。
标签: javascript html css