[英]Javascript for jumping to top and back on scroll
我有一个功能让一个容器在滚动100px时跳到顶部,并且一旦再次滚动到顶部就跳回原位。 我正在努力在较小的设备上禁用它,但我使用的代码也打破了行为。
$(window).scroll(function() {`
//After scrolling 100px from the top...
if ( $(window).scrollTop() >= 72 || w > 920 ) {
$('#container').css('top', '0px');
//Otherwise remove inline styles and thereby revert to original stying
} else {
$('#container').attr('style', '');
}
});
更新:当您再次滚动到顶部时,此代码实际上应该使容器跳转到前一个位置。 问题在'w> 920'。 我需要它来禁用较小设备上的行为,但它也会破坏功能。 我猜是因为'AND'声明。 窗口宽度保持不变,因为恢复中断。
不工作小提琴
https://jsfiddle.net/xm1yq4to/
工作小提琴
https://jsfiddle.net/s15g7nup/
是否有另一种方法为此指定设备宽度?
我最终以不同的方式执行此操作,而不是检查设备但更改了类
$(document).ready(function() {
// run test on initial page load
checkSize();
// run test on resize of the window
$(window).resize(checkSize);
});
//Function to the css rule
function checkSize(){
if ($(".desktop-menu").css("display") == "block" ){
$(window).scroll(function() {
//After scrolling 100px from the top...
if ( $(window).scrollTop() >= 100 ) {
$('#container').css('top', '0px');
}
//Otherwise remove inline styles and thereby revert to original stying
else {
$('#container').attr('style', '');
}
});
}
return false;
};
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.