Skip to content

Instantly share code, notes, and snippets.

@hellojinjie
Created December 23, 2017 14:42
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 hellojinjie/ac849b9cf7982fc62d2d36ac6bdb0111 to your computer and use it in GitHub Desktop.
Save hellojinjie/ac849b9cf7982fc62d2d36ac6bdb0111 to your computer and use it in GitHub Desktop.
$.fn.smartFloat = function() {
var position = function(element) {
var top = element.position().top, pos = element.css("position");
$(window).scroll(function() {
var scrolls = $(this).scrollTop();
if (scrolls > top) {
if (window.XMLHttpRequest) {
element.css({
position: "fixed",
top: 0
});
} else {
element.css({
top: scrolls
});
}
}else {
element.css({
position: "absolute",
top: top
});
}
});
};
return $(this).each(function() {
position($(this));
});
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment