Advertisement
Guest User

Untitled

a guest
Jul 20th, 2019
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.35 KB | None | 0 0
  1. (function(){
  2.  
  3. // window.scrollY cross Browser
  4. var scrollY = function () {
  5. var supportPageOffset = window.pageXOffset !== undefined;
  6. var isCSS1Compat = ((document.compatMode || "") === "CSS1Compat");
  7. return supportPageOffset ? window.pageYOffset : isCSS1Compat ? document.documentElement.scrollTop : document.body.scrollTop;
  8. }
  9.  
  10. window.makeSticky = function (element) {
  11. // Les variables
  12. var rect, // Rectangle de l'élément
  13. top, // Position de l'élément par rapport au haut de la page
  14. constraintRect, // Rectangle de la contrainte
  15. constraintBottom // Position qui fait sortir le rectangle de la contrainte
  16. var offset = parseInt(element.getAttribute('data-offset') || 0, 10)
  17. var constraint = document.body
  18. if (element.getAttribute('data-constraint')){
  19. constraint = document.querySelector(element.getAttribute('data-constraint'))
  20. }
  21. var fake = document.createElement('div')
  22.  
  23. // Fonctions
  24. // Permet de stocker la valeurs utiles pour le calcul des positions
  25. var setValues = function () {
  26. rect = element.getBoundingClientRect()
  27. constraintRect = constraint.getBoundingClientRect()
  28. constraintBottom = constraintRect.top + scrollY() + constraintRect.height - offset - rect.height
  29. top = rect.top + scrollY()
  30. fake.style.width = rect.width + "px"
  31. fake.style.height= rect.height + "px"
  32. }
  33.  
  34. var onScroll = function () {
  35. if (scrollY() > constraintBottom && element.style.position != 'absolute') {
  36. element.style.position = 'absolute'
  37. element.style.bottom = '0'
  38. element.style.top = 'auto'
  39. } else if (scrollY() > top - offset && scrollY() < constraintBottom && element.style.position != 'fixed') {
  40. element.classList.add('fixed')
  41. element.style.position = 'fixed'
  42. element.style.top = offset + "px"
  43. element.style.bottom = 'auto'
  44. element.style.zIndex = '9999999'
  45. element.style.width = rect.width + "px"
  46. element.parentNode.insertBefore(fake, element)
  47. } else if (scrollY() < top - offset && element.style.position != 'static') {
  48. element.classList.remove('fixed')
  49. element.style.position = 'static'
  50. if (element.parentNode.contains(fake)) {
  51. element.parentNode.removeChild(fake)
  52. }
  53. }
  54. }
  55.  
  56. var onResize = function () {
  57. element.style.width = "auto"
  58. element.classList.remove('fixed')
  59. element.style.position = "static"
  60. fake.style.display = "none"
  61. setValues()
  62. fake.style.display = "block"
  63. onScroll()
  64. }
  65.  
  66. // Listener
  67. window.addEventListener('scroll', onScroll)
  68. window.addEventListener('resize', onResize)
  69.  
  70. // Initialisation
  71. setValues()
  72. }
  73.  
  74. var elements = document.querySelectorAll('[data-sticky]')
  75. for (var i = 0; i < elements.length; i++) {
  76. makeSticky(elements[i])
  77. }
  78.  
  79.  
  80. })()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement