2012-04-29 41 views
-1

我已经在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,滚动条有时会​​超出限制。我尝试了不同的解决方法,但找不到通用解决方案,您可以推荐我做什么?

+0

为什么不添加一个处理这些异常的条件'if'语句? –

+0

我不确定我是否理解你的问题,但是你可以在两者中的一个上使用Math.floor(),而在另一个上使用Math.ceil()。那样的话,一个总是往下走,一个总是往上移(而不是两个取整)? –

+0

我尝试过使用Math.ceil()和Math.floor(),但没有帮助,尝试过,如果语句,但他们有点不确定内容的高度增长的情况 – micnic

回答

0

嗯,注意到你的第一个公式最终会完全冲出“高度”....那是你的意图吗?

+0

scrollbar.height和height是两个完全不同的变量 – micnic

+0

yes but height/scrollHeight * height = height * height/scrollheight。对我来说没有意义,但我只有一半通过我的早晨咖啡。 – joshp