我已经在JavaScript中使用下一个公式创建的自定义滚动:当我使用奇数值时所观察到滚动条数学公式错误
// Data used to render the scrollbar
var height; // Height of the viewport and of the scrollbarArea
var scrollHeight; // Height of the content
var scrollTop; // Scrolled position of the content from the top
// Used formulas
scrollbar.height = Math.round(height/scrollHeight * height);
scrollbar.top = Math.round(scrollTop/scrollHeight * height);
一切顺利直到(scrollbar.height + scrollbar.top>高度)高度。我的意思是,由于Math.round()的两个操作,通过添加1px甚至2px,滚动条有时会超出限制。我尝试了不同的解决方法,但找不到通用解决方案,您可以推荐我做什么?
为什么不添加一个处理这些异常的条件'if'语句? –
我不确定我是否理解你的问题,但是你可以在两者中的一个上使用Math.floor(),而在另一个上使用Math.ceil()。那样的话,一个总是往下走,一个总是往上移(而不是两个取整)? –
我尝试过使用Math.ceil()和Math.floor(),但没有帮助,尝试过,如果语句,但他们有点不确定内容的高度增长的情况 – micnic