我正在尝试将返回顶部添加到 SharePoint 母版页。返回页首功能正常,但在页面滚动时我无法添加显示/隐藏图标的类。下面是JS代码
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);
});
}
我在 SharePoint 母版页中添加的 HTML 下方
<a href="#" id="back-to-top" title="Back to top">↑</a>
问题:图标在页面上不可见,因为它无法在滚动上添加类显示。
最佳答案
对于共享点而不是 $(window) 我使用 $('#s4-workspace') 及其工作
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);
});
}
关于javascript - SharePoint 返回顶部 : not able to add class on page scroll,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41879353/