Reputation: 447
I am trying to add back to top to the SharePoint master page. Back to top functionality is working but on page scroll I am not able to add the class which is show/hide the icon. Below is the JS code
if ($('#back-to-top').length) {
var scrollTrigger = 100, // px
backToTop = function () {
var scrollTop = $(window).scrollTop();
if (scrollTop > scrollTrigger) {
$('#back-to-top').addClass('show');
} else {
$('#back-to-top').removeClass('show');
}
};
$(window).on('scroll', function () {
backToTop();
});
$('#back-to-top').on('click', function (e) {
e.preventDefault();
$('#s4-workspace').animate({scrollTop: 0}, 700);
});
}
Below HTML I have added in SharePoint master-page
<a href="#" id="back-to-top" title="Back to top">↑</a>
Issue: Icon is not visible on page because its not able to add class show on scroll.
Upvotes: 0
Views: 1223
Reputation: 447
For sharepoint instead of $(window) I used $('#s4-workspace') and its working
if ($('#back-to-top').length) {
var scrollTrigger = 100, // px
backToTop = function () {
var scrollTop = $('#s4-workspace').scrollTop();
if (scrollTop > scrollTrigger) {
$('#back-to-top').addClass('show');
} else {
$('#back-to-top').removeClass('show');
}
};
$('#s4-workspace').bind('scroll', function () {
backToTop();
});
$('#back-to-top').on('click', function (e) {
e.preventDefault();
$('#s4-workspace').animate({scrollTop: 0}, 700);
});
}
Upvotes: 1