Javascript 禁用移动设备上的滚动偏移
我有点小问题。这是我的密码Javascript 禁用移动设备上的滚动偏移,javascript,jquery,mobile,scroll,offset,Javascript,Jquery,Mobile,Scroll,Offset,我有点小问题。这是我的密码 $(document).on("scroll", onScroll); $('a[href^="#"]').on('click', function (e) { e.preventDefault(); $(document).off("scroll"); $('a').each(function () { $(this).removeClass('active'); }) $(this).addClass('
$(document).on("scroll", onScroll);
$('a[href^="#"]').on('click', function (e) {
e.preventDefault();
$(document).off("scroll");
$('a').each(function () {
$(this).removeClass('active');
})
$(this).addClass('active');
var target = this.hash,
menu = target;
$target = $(target);
$('html, body').stop().animate({
'scrollTop': $target.offset().top - 53}, 1000, 'swing', function () {
window.location.hash = target;
$(document).on("scroll", onScroll);
});
});
如你所见,我在目标上方设置了一个53px的偏移量。53px是我的固定菜单栏的高度
我想在移动设备上将此偏移设置为0,因为菜单栏是隐藏的
如何做到这一点?您可以使用
窗口。innerWidth
进行此操作。通常移动窗口不超过1024px。在我这样做的过程中,我将从800px开始
例如:
if( window.innerWidth > 801 ){
// offset code to 53 on not mobile
}else{
// offset code to 0 on mobile
}