views:

45

answers:

1

Hello guys.

I've been trying to create my own custom scroll bar implementation in JavaScript. It must emulate the look and behavior of MS Office 2007 I checked many solutions out there, but nothing satisfies my needs. My main problem is this: How to calculate a balance between the scroll bar height and the number of lines it will move the target page when itself dragged a pixel? In other words, a balance between the scroll bar precision and the space available to drag it up and down. Is there a ready algorithm for that to reuse? I searched the net but found nothing, quite strange for what must be a common coding problem?

Thanks!

+1  A: 

this is how to calculate the top position of your scrollbar

scrollbar.style.top = element.scrollTop / (element.scrollHeight / element.style.height)

you can use this onscroll of your scroll element. vice versa, this works in the other direction, when you drag your scrollbar.

sorry for this quick'n'dirty answer, will provide more information later if you liked to.

koko
Thanks, but I did not ask how to calculate the top position of my scrollbar. If you have something in mind, please, provide more details.
avok00
you ned the previous algorithm to calculate the scrollbar position if you scroll with your mousewheel. so, if "scrollbar.style.top = element.scrollTop / (element.totalHeight / element.height)" the formula to calculate element.scrollTop should be "element.scrollTop = element.totalHeight / element.height * scrollbar.style.top"
koko
Please, explain what element.scrollTop element.totalHeight and element.height mean. I don't get you at all.
avok00
Yeah sorry, I was in hurry and I didn't explain it really well :) Let´s say you have your div with the id "scrollelement". Its CSS is "height: 500px; overflow: auto". Its scrollHeight is 1500px, because of all that stuff in it. So when I said "totalHeight" I meant document.getElementById(scrollelement).scrollHeight and "height" is simply document.getElementById(scrollelement).style.height
koko
Thanks pal, this solves the problem with where the scroll bar is. I guess the problem of how fast to scroll the element is a trivial one :)
avok00