I have the soft scrolling functions of the menu for a One Page sessions as well as a smooth Back to Top:
var $doc = $('html, body');
$('a').not('#back-to-top').click(function (e) {
e.preventDefault();
$doc.animate({
scrollTop: $($.attr(this, 'href')).offset().top
}, 500);
});
$(window).on('scroll', function () {
if ($(window).scrollTop() > 100) {
$('#back-to-top').addClass('show');
} else {
$('#back-to-top').removeClass('show');
}
});
$('#back-to-top').on('click', function (e) {
e.preventDefault();
$doc.animate({
scrollTop: 0
}, 700);
});
css:
#back-to-top {
position: fixed;
bottom: 35px;
right: 35px;
z-index: 9999;
width: 42px;
height: 42px;
text-align: center;
line-height: 40px;
background: #333;
color: #fff;
cursor: pointer;
border: 1px solid #fff;
border-radius: 2px;
text-decoration: none;
transition: opacity 0.2s ease-out;
opacity: 0;
}
#back-to-top:hover {
background: #000;
opacity: 1 !important;
}
#back-to-top.show {
opacity: 0.4;
}
How to simplify these actions, could you unify in a single action?