好的,正如问题所说,我在一个点上设置了一个元素,但由于某些原因,我需要它向上滚动一点,然后停在另一个固定点上。我的朋友帮了我一点代码,但我们都不是Javascript的专家,所以类似的问题并没有真正帮助我弄清楚这件事。我现在的代码是
function absToFix() {
var udaljenost = $(window).scrollTop();
var sideb = $('#sidebar').offset().top;
var distance = (sideb - udaljenost);
return distance;
}
$(document).ready(function() {
var distance = absToFix();
$(window).scroll(function() {
distance = absToFix();
if (distance < 220) {
$('#sidebar').css({
position: 'fixed',
top: '220px'
});
} else {
$('#sidebar').css({
position: 'absolute',
top: '340px'
});
}
});
});
一般来说,它是有效的,在某种意义上,它站在它应该在的地方,并在它应该停止滚动的地方停止滚动,然而,随着进一步的滚动,它只是在固定点和向上滚动之间保持闪烁,而我真的不知道如何停止它。
发布于 2016-02-25 00:23:47
您可以使用以下函数:
function disableScroll() {
if (window.addEventListener) // older FF
window.addEventListener('DOMMouseScroll', preventDefault, false);
window.onwheel = preventDefault; // modern standard
window.onmousewheel = document.onmousewheel = preventDefault; // older browsers, IE
window.ontouchmove = preventDefault; // mobile
document.onkeydown = preventDefaultForScrollKeys;
}
function enableScroll() {
if (window.removeEventListener)
window.removeEventListener('DOMMouseScroll', preventDefault, false);
window.onmousewheel = document.onmousewheel = null;
window.onwheel = null;
window.ontouchmove = null;
document.onkeydown = null;
}
$(document).ready(function() {
var distance = absToFix();
$(window).scroll(function() {
distance = absToFix();
if (distance < 220) {
disableScroll();
} else {
$('#sidebar').css({
position: 'absolute',
top: '340px'
});
}
});
});
https://stackoverflow.com/questions/35615889
复制