Skip to content

Instantly share code, notes, and snippets.

@kas-cor
Last active March 29, 2017 21:09
Show Gist options
  • Save kas-cor/ee78307c4a8d433138f8162a38ef5d8d to your computer and use it in GitHub Desktop.
Save kas-cor/ee78307c4a8d433138f8162a38ef5d8d to your computer and use it in GitHub Desktop.
Кнопка back to top или scroll to top
window.jQuery || document.write('<script src="//ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>');
document.addEventListener("DOMContentLoaded", function () {
$(function () {
var elem = "scroll-to-top"; // id object
var img = "//path/to/img/file.png"; // url to img
var obj;
$("body").append('<div id="' + elem + '"></div>');
obj = $("#" + elem);
$(obj).css({
"display": "none",
"background": "url('" + img + "') no-repeat",
"background-size": "contain",
"position": "fixed",
"right": "20px",
"bottom": "80px",
"height": "60px",
"width": "60px",
"opacity": ".9",
"cursor": "pointer"
});
$(window).scroll(function () {
if ($(this).scrollTop() > 20) {
$(obj).stop(true).fadeIn();
} else {
$(obj).stop(true).fadeOut();
}
});
$(obj).click(function () {
$("body, html").stop(true).animate({scrollTop: 0}, 600);
return false;
});
});
});
@kas-cor
Copy link
Author

kas-cor commented Nov 22, 2016

Установка

Перед закрывающем тегом </body>, вставляем стоку:

<script src="/path/to/script.js" type="text/javascript"></script>

Параметры

Переменные:
elem - id контейнера кнопки (изменяется если уже занят)
img - Путь до изображения кнопки (стрелки)

Положение и размер:

...
"right": "20px", // Отступ от правого края
"bottom": "80px", // Отступ от нижнего края
"height": "60px", // Высота изображения
"width": "60px", // Ширина изображения
...

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