Scrollable.js 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. /* via http://jsfiddle.net/elGrecode/00dgurnn/ */
  2. window.initScrollable = function () {
  3. var scrollContainer = document.querySelector('.scrollable'),
  4. scrollContentWrapper = document.querySelector('.scrollable .content-wrapper'),
  5. scrollContent = document.querySelector('.scrollable .content'),
  6. contentPosition = 0,
  7. scrollerBeingDragged = false,
  8. scroller,
  9. topPosition,
  10. scrollerHeight;
  11. function calculateScrollerHeight() {
  12. // *Calculation of how tall scroller should be
  13. var visibleRatio = scrollContainer.offsetHeight / scrollContentWrapper.scrollHeight;
  14. if (visibleRatio == 1)
  15. scroller.style.display = "none";
  16. else
  17. scroller.style.display = "block";
  18. return visibleRatio * scrollContainer.offsetHeight;
  19. }
  20. function moveScroller(evt) {
  21. // Move Scroll bar to top offset
  22. var scrollPercentage = evt.target.scrollTop / scrollContentWrapper.scrollHeight;
  23. topPosition = scrollPercentage * (scrollContainer.offsetHeight - 5); // 5px arbitrary offset so scroll bar doesn't move too far beyond content wrapper bounding box
  24. scroller.style.top = topPosition + 'px';
  25. }
  26. function startDrag(evt) {
  27. normalizedPosition = evt.pageY;
  28. contentPosition = scrollContentWrapper.scrollTop;
  29. scrollerBeingDragged = true;
  30. window.addEventListener('mousemove', scrollBarScroll);
  31. return false;
  32. }
  33. function stopDrag(evt) {
  34. scrollerBeingDragged = false;
  35. window.removeEventListener('mousemove', scrollBarScroll);
  36. }
  37. function scrollBarScroll(evt) {
  38. if (scrollerBeingDragged === true) {
  39. evt.preventDefault();
  40. var mouseDifferential = evt.pageY - normalizedPosition;
  41. var scrollEquivalent = mouseDifferential * (scrollContentWrapper.scrollHeight / scrollContainer.offsetHeight);
  42. scrollContentWrapper.scrollTop = contentPosition + scrollEquivalent;
  43. }
  44. }
  45. function updateHeight() {
  46. scrollerHeight = calculateScrollerHeight() - 10;
  47. scroller.style.height = scrollerHeight + 'px';
  48. }
  49. function createScroller() {
  50. // *Creates scroller element and appends to '.scrollable' div
  51. // create scroller element
  52. scroller = document.createElement("div");
  53. scroller.className = 'scroller';
  54. // determine how big scroller should be based on content
  55. scrollerHeight = calculateScrollerHeight() - 10;
  56. if (scrollerHeight / scrollContainer.offsetHeight < 1) {
  57. // *If there is a need to have scroll bar based on content size
  58. scroller.style.height = scrollerHeight + 'px';
  59. // append scroller to scrollContainer div
  60. scrollContainer.appendChild(scroller);
  61. // show scroll path divot
  62. scrollContainer.className += ' showScroll';
  63. // attach related draggable listeners
  64. scroller.addEventListener('mousedown', startDrag);
  65. window.addEventListener('mouseup', stopDrag);
  66. }
  67. }
  68. createScroller();
  69. // *** Listeners ***
  70. scrollContentWrapper.addEventListener('scroll', moveScroller);
  71. return updateHeight;
  72. };