Skip to content

Instantly share code, notes, and snippets.

@j1cs
Last active July 26, 2017 13:43
Show Gist options
  • Save j1cs/c21fbc02f07f41faf366553a147807dd to your computer and use it in GitHub Desktop.
Save j1cs/c21fbc02f07f41faf366553a147807dd to your computer and use it in GitHub Desktop.
Back on top (with jQuery)
! function() {
$(window).scroll(function() {
$(this).scrollTop() > 300 ? $("#back-top").fadeIn() : $("#back-top").fadeOut()
});
var t = $("<div>");
t.css({
position: "relative",
"text-align": "center"
}), t.attr("id", "back-top");
var a = $("<a>");
t.attr("href", "javascript:;"), t.css({
cursor: "pointer",
position: "fixed",
right: "75px",
"z-index": "1000",
width: "20px",
height: "20px",
"line-height": "20px",
bottom: "90px"
}), t.append(a);
var i = $("<img>");
i.attr("src", "https://raw.githubusercontent.com/glats/AngularJS101/master/chevron-up.png"), i.attr("heigth", "24"), i.attr("width", "24"), a.on("click", function() {
$("body, html").animate({
scrollTop: 0
}, 800)
}), a.append(i), $("body").append(t)
}();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment