Created
March 1, 2018 14:14
-
-
Save morozVA/dc1370b44743ddbc0cce71d39bf29c3d to your computer and use it in GitHub Desktop.
js scrollTop - кнопка наверх
This file contains 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
<button id="toTop" class=""><i class="material-icons">keyboard_arrow_up</i></button> | |
#toTop.active { | |
opacity: 1; | |
visibility: visible; | |
} | |
#toTop { | |
opacity: 0; | |
visibility: hidden; | |
position: fixed; | |
right: 15px; | |
bottom: 15px; | |
width: 40px; | |
height: 40px; | |
font-size: 20px; | |
text-align: center; | |
color: #fff; | |
background: #1a4d78; | |
-webkit-box-shadow: 0 2px 5px 0 rgba(0,0,0,0.26); | |
box-shadow: 0 2px 5px 0 rgba(0,0,0,0.26); | |
z-index: 2; | |
-webkit-transition: .5s; | |
-o-transition: .5s; | |
transition: .5s; | |
padding: 0; | |
} | |
// ==================================================== | |
// Скролл наверх | |
// ==================================================== | |
$('#toTop').click(function(e) { // клик по кнопке скролла | |
e.preventDefault(); // отменяем стандартное действие кнопки | |
$('html, body').animate({ // плавно скроллим страницу вверх | |
scrollTop: 0 // скроллим до самого верха | |
}, 600); // со скоростью 600ms | |
}); | |
var scroll_button = function() { // создаем функцию отлавливания скролла | |
if ($('html, body').scrollTop() > 150) { // если сайт проскроллен на >150px вниз | |
$('#toTop').addClass('active'); // показываем кнопку | |
} else { // если сайт проскроллен на <150px вниз | |
$('#toTop').removeClass('active'); // скрываем кнопку | |
} | |
} | |
scroll_button(); // объявляем функцию отлавливания скролла | |
$(window).scroll(function() { | |
scroll_button(); // объявляем функцию отлавливания скролла | |
}) // END Скролл наверх =========== |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment