Last active
August 21, 2019 04:05
-
-
Save edison7500/d03846d18ca8e47be986608bfa31ee4d to your computer and use it in GitHub Desktop.
jquery.scrollToTop.js
This file contains hidden or 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
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