Created
June 29, 2018 03:47
-
-
Save codeniko/345b85cc0f99a737da981d02f3899c51 to your computer and use it in GitHub Desktop.
Sticky floater example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <html> | |
| <head> | |
| <style> | |
| html, body, div { | |
| margin: 0px; | |
| padding: 0px; | |
| } | |
| .content { | |
| height: 900px; | |
| width: 100%; | |
| margin: 0px; | |
| padding: 0px; | |
| position: relative; | |
| } | |
| .footer { | |
| height: 100px; | |
| background-color: red; | |
| width: 100%; | |
| } | |
| .floater { | |
| width: 20px; | |
| height: 20px; | |
| background-color: blue; | |
| position: absolute; | |
| bottom: 12px; | |
| right: 12px; | |
| } | |
| .sticky { | |
| position: fixed; | |
| } | |
| </style> | |
| </head> | |
| <body> | |
| <div class="content"> | |
| <div class="floater sticky"> | |
| </div> | |
| </div> | |
| <div class="footer"> | |
| </div> | |
| <script> | |
| window.onscroll = checkFloater | |
| const footer = document.getElementsByClassName('footer')[0] | |
| const floater = document.getElementsByClassName('floater')[0] | |
| checkFloater() | |
| function checkFloater() { | |
| const footerOffset = footer.offsetTop | |
| const yPos = window.pageYOffset + window.innerHeight | |
| if (yPos >= footerOffset) { | |
| floater.classList.remove('sticky') | |
| } else { | |
| floater.classList.add('sticky') | |
| } | |
| } | |
| </script> | |
| </body> | |
| </html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment