Advertisement
Guest User

Untitled

a guest
Mar 29th, 2017
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.94 KB | None | 0 0
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="utf-8">
  5. <meta name="viewport" content="width=device-width">
  6. <title>JS Bin</title>
  7. <style id="jsbin-css">
  8. html, body {
  9. margin: 0;
  10. padding: 0;
  11. }
  12.  
  13. .main {
  14. height: 2000px;
  15. background: linear-gradient(to top, yellow, purple)
  16. }
  17.  
  18. .bar {
  19. position: fixed;
  20. width: 5px;
  21. background: red;
  22. min-height: 15%;
  23. right: 0;
  24. top: 0;
  25. }
  26. </style>
  27. </head>
  28. <body>
  29. <div class="bar">
  30. </div>
  31. <div class="main"></div>
  32. <script id="jsbin-javascript">
  33. var $main = document.querySelector('.main')
  34. var height = $main.offsetHeight
  35. var wHeight = window.innerHeight
  36.  
  37. var barHeight = wHeight / height * wHeight
  38. var $bar = document.querySelector('.bar')
  39. $bar.style.height = barHeight + 'px'
  40. barHeight = $bar.offsetHeight
  41.  
  42. var $body = document.querySelector('body')
  43. window.addEventListener('scroll', function () {
  44. var total = (wHeight - barHeight) / barHeight
  45. var progress = $body.scrollTop / (height - wHeight)
  46. var translate = progress * total * 100
  47. $bar.style.transform = 'translateY(' + translate + '%)'
  48. })
  49. </script>
  50.  
  51.  
  52. <script id="jsbin-source-css" type="text/css">html, body {
  53. margin: 0;
  54. padding: 0;
  55. }
  56.  
  57. .main {
  58. height: 2000px;
  59. background: linear-gradient(to top, yellow, purple)
  60. }
  61.  
  62. .bar {
  63. position: fixed;
  64. width: 5px;
  65. background: red;
  66. min-height: 15%;
  67. right: 0;
  68. top: 0;
  69. }</script>
  70.  
  71. <script id="jsbin-source-javascript" type="text/javascript">var $main = document.querySelector('.main')
  72. var height = $main.offsetHeight
  73. var wHeight = window.innerHeight
  74.  
  75. var barHeight = wHeight / height * wHeight
  76. var $bar = document.querySelector('.bar')
  77. $bar.style.height = barHeight + 'px'
  78. barHeight = $bar.offsetHeight
  79.  
  80. var $body = document.querySelector('body')
  81. window.addEventListener('scroll', function () {
  82. var total = (wHeight - barHeight) / barHeight
  83. var progress = $body.scrollTop / (height - wHeight)
  84. var translate = progress * total * 100
  85. $bar.style.transform = 'translateY(' + translate + '%)'
  86. })
  87. </script></body>
  88. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement