Skip to content

Instantly share code, notes, and snippets.

@edison7500
Last active August 21, 2019 04:05
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 edison7500/d03846d18ca8e47be986608bfa31ee4d to your computer and use it in GitHub Desktop.
Save edison7500/d03846d18ca8e47be986608bfa31ee4d to your computer and use it in GitHub Desktop.
jquery.scrollToTop.js
import $ from "jquery";
import plugin from "./plugin";
class ScrollToTop {
constructor(element, options) {
const $element = $(element);
$(window).scroll(function () {
if ($(this).scrollTop() > options.offset) {
$element.fadeIn();
} else {
$element.fadeOut();
}
});
$element.click(function (e) {
e.preventDefault();
$("html, body").animate({
scrollTop: 0
}, options.speed);
});
}
}
ScrollToTop.DEFAULTS = {
offset: 100,
speed: 500,
};
plugin("ScrollToTop", ScrollToTop);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment