Last active
March 7, 2017 16:10
-
-
Save bloqhead/8521a7e168f8638629c1cfbce6d0c27d to your computer and use it in GitHub Desktop.
Simple jQuery scroll-to-top button.
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
| .scroll-to-top { | |
| position: fixed; | |
| z-index: 10; | |
| bottom: 2em; | |
| right: 2em; | |
| display: inline-block; | |
| padding: 1em; | |
| text-decoration: none; | |
| background: #eee; | |
| -webkit-transition: all 0.5s ease-in-out; | |
| -moz-transition: all 0.5s ease-in-out; | |
| transition: all 0.5s ease-in-out; | |
| -webkit-transform: translateY(0); | |
| -moz-transform: translateY(0); | |
| transform: translateY(0); | |
| } | |
| .scroll-to-top--hidden { | |
| -webkit-transform: translateY(200%); | |
| -moz-transform: translateY(200%); | |
| transform: translateY(200%); | |
| } |
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
| <a href="#" class="scroll-to-top scroll-to-top--hidden">Top</a> |
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
| # | |
| # ScrollToTop | |
| # | |
| class ScrollToTop | |
| constructor: () -> | |
| @window = jQuery window | |
| @document = jQuery "body" | |
| @btn = jQuery ".scroll-to-top" | |
| @btn_hidden_class = "scroll-to-top--hidden" | |
| @distance = 100 | |
| @speed = 400 | |
| @window.scroll @scroll_check | |
| @btn.on "click", @scroll_click | |
| scroll_check : (e) => | |
| if jQuery(window).scrollTop() > @distance | |
| @btn.removeClass @btn_hidden_class | |
| console.log "class removed" | |
| else | |
| @btn.addClass @btn_hidden_class | |
| console.log "class added" | |
| scroll_click : (e) => | |
| @document.animate { scrollTop: 0 }, @speed | |
| new ScrollToTop |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment