Advertisement
Guest User

Untitled

a guest
Oct 21st, 2016
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.97 KB | None | 0 0
  1. /**
  2. * scroll an overflowed list so that the elementToView is visible
  3. *
  4. * @param {<NodeList item>} elementToView list element to make visible
  5. * @param {<NodeList item>} elementContainer element containing the elementToView
  6. * @param {<NodeList item>} listElement actual list (<ul>?) element to scroll (could be the same as container)
  7. *
  8. *
  9. */
  10. function scrollList(elementToView, elementContainer, listElement) {
  11. let containerHeight, topPos, listItem, temp = null
  12. if (typeof window !== 'undefined' && elementToView && elementContainer && listElement) {
  13. temp = window.getComputedStyle(elementContainer, null).getPropertyValue('padding-top')
  14. temp = parseInt(temp, 10) + parseInt(window.getComputedStyle(elementContainer, null).getPropertyValue('padding-bottom'), 10)
  15. // temp holds the top and bottom padding
  16. // usable space is the offsetHeight - padding
  17. containerHeight = elementContainer.offsetHeight - temp
  18. // amount of pixels away from the top of the container
  19. topPos = elementToView.offsetTop
  20. if (!containerHeight || !topPos) return false
  21. // get the height of a list item in the list to scroll
  22. listItem = listElement.querySelector('li:first-of-type').offsetHeight
  23. // actually do the scrolling now
  24. // scroll the top of the list to show the elementToView on the bottom (as the last visible element in the list)
  25. listElement.scrollTop = (topPos - (containerHeight - listItem))
  26. }
  27. }
  28.  
  29. let focusElement = document.getElementsByClassName('focus')[0]
  30. let containerElement = document.getElementsByClassName('modal')[0]
  31. let listElement = document.getElementsByClassName('chapter-list')[0]
  32. // let's check if any selection index has changed, and then scroll to the correct
  33. // positions to make sure the selected elements are in view
  34. if (chapterlistSelectionIndex !== prevState.chapterlistSelectionIndex) {
  35. scrollList(focusElement, containerElement, listElement)
  36. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement