Created
February 11, 2014 12:53
-
-
Save dtuite/8934298 to your computer and use it in GitHub Desktop.
Code sample for displaying sharing bar when certain percentage scrolled
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
# Show an element when we are scrolled down past a certain point on the page | |
# but hide it otherwise. | |
jQuery.fn.showWhenScrolled = (options = {}) -> | |
$window = $(window) | |
$element = @ | |
elementVisible = false | |
if options.percentToShowBar | |
# Calculate the point as a percentage of the height of the page. | |
pointToShowBar = options.percentToShowBar * 0.01 * $(document).height() | |
else | |
pointToShowBar = options.pointToShowBar || 600 | |
checkPosition = -> | |
if $window.scrollTop() > pointToShowBar and !elementVisible | |
$element.slideDown 'fast', -> elementVisible = true | |
else if $window.scrollTop() < pointToShowBar and elementVisible | |
$element.slideUp 'fast', -> elementVisible = false | |
# Using setInterval because listenting to the 'scroll' event causes a severe | |
# preformance hit: http://ejohn.org/blog/learning-from-twitter/ | |
setInterval(checkPosition, 500) | |
jQuery -> | |
$('.share-bar').showWhenScrolled(percentToShowBar: 30) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment