Skip to content

Instantly share code, notes, and snippets.

@jgarciaruiz
Created August 6, 2017 10:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jgarciaruiz/3a1f78a6b831013a3e109bbb3b216b56 to your computer and use it in GitHub Desktop.
Save jgarciaruiz/3a1f78a6b831013a3e109bbb3b216b56 to your computer and use it in GitHub Desktop.
Followbox - jQuery
<div class="follow-scroll">
Let me follow you.
</div>
(function($) {
var element = $(".follow-scroll"),
originalY = element.offset().top;
var topMargin = 20,
topDistance = '';
$(window).on('scroll', function(event) {
var scrollTop = $(window).scrollTop();
var topDistance = 0;
if (scrollTop < originalY){
topDistance = 0
}
else{
topDistance = scrollTop - originalY + topMargin
}
element.stop(false, false).animate({
top:topDistance
}, 500);
});
})(jQuery);
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
html { box-sizing: border-box; }
*, *:before, *:after { box-sizing: inherit; }
body{
height:2000px;
}
.follow-scroll{
background: #eee; border: 1px solid #ccc;
padding:20px;
width:200px;
text-align:center;
position:relative;
top:20px;
border:1px solid tomato;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment