Created
August 6, 2017 10:44
-
-
Save jgarciaruiz/3a1f78a6b831013a3e109bbb3b216b56 to your computer and use it in GitHub Desktop.
Followbox - jQuery
This file contains 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
<div class="follow-scroll"> | |
Let me follow you. | |
</div> |
This file contains 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
(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); |
This file contains 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
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script> |
This file contains 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 { 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