Skip to content

Instantly share code, notes, and snippets.

@Sanix-Darker
Last active October 28, 2017 00:10
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Sanix-Darker/2cadbf16319f863e0dd3afead8f28528 to your computer and use it in GitHub Desktop.
Save Sanix-Darker/2cadbf16319f863e0dd3afead8f28528 to your computer and use it in GitHub Desktop.
[JS]Fixed a div when scroll
$.fn.followTo = function (pos) {
var $this = this,
$window = $(window);
$window.scroll(function (e) {
if ($window.scrollTop() > pos) {
$this.css({
position: 'fixed',
top: 0
});
} else {
$this.removeAttr('style');
}
});
};
// Calling the function
$('#div').followTo(50);
@Sanix-Darker
Copy link
Author

As simple as it

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment