Created
June 8, 2016 16:48
-
-
Save joshuaogle/d3e32bf7f41317f0847ed60092e5058c to your computer and use it in GitHub Desktop.
Sticky
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
| .sticky-top { | |
| @include position(fixed, $base-spacing null null); | |
| } | |
| .sticky-bottom { | |
| top: auto; | |
| } |
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
| class AppName.Sticky | |
| constructor: ($element) -> | |
| $element = $element | |
| $context = $($element.attr("data-sticky")).first() | |
| @_measure($element, $context) | |
| $(document).on "scroll", => @_measure($element, $context) | |
| return | |
| _measure: ($element, $context) -> | |
| LARGE_SCREEN_SIZE = 1024 | |
| SPACING = 24 | |
| if $(window).width() < LARGE_SCREEN_SIZE | |
| @_unBottom() | |
| else if $element.is(":visible") | |
| windowHeight = $(window).height() | |
| windowTop = $(window).scrollTop() | |
| windowBottom = windowTop + windowHeight | |
| elementWidth = $element.width() | |
| contextTop = $context.offset().top - SPACING | |
| contextBottom = contextTop + $context.height() | |
| offsetSpaceBelowElement = windowHeight - $element.height() | |
| offsetTrigger = contextBottom + offsetSpaceBelowElement - SPACING | |
| offsetBottom = windowTop - contextBottom + windowHeight | |
| if windowTop > contextTop | |
| @_top($element, elementWidth) | |
| else | |
| @_unTop($element) | |
| if windowBottom > offsetTrigger | |
| @_bottom($element, offsetBottom) | |
| else | |
| @_unBottom($element, elementWidth) | |
| return | |
| _top: ($element, elementWidth) -> | |
| $element | |
| .addClass "sticky-top" | |
| .css "width", elementWidth | |
| _unTop: ($element) -> | |
| if $element.hasClass "sticky-top" | |
| $element | |
| .removeClass "sticky-top" | |
| .removeAttr "style" | |
| _bottom: ($element, offsetBottom) -> | |
| $element | |
| .addClass "sticky-bottom" | |
| .css "bottom", offsetBottom | |
| _unBottom: ($element, elementWidth) -> | |
| if $element.hasClass "sticky-bottom" | |
| $element | |
| .removeClass "sticky-bottom" | |
| .css "width", elementWidth | |
| $ -> | |
| $("[data-sticky]").each -> | |
| new AppName.Sticky $(this) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment