这可能看起来像一个奇怪的问题,但我需要知道是否有可能劫持窗口滚动条,所以当用户滚动它时它不会滚动页面。我想写一些js,而当用户滚动窗口滚动条时,它会滚动一个div。我可以写js来检测滚动和动画div等多少,但不知道如何劫持窗口滚动条并停止滚动窗口这可能吗?
答案 0 :(得分:5)
您可以尝试其他一些方法,而不是尝试“劫持”浏览器窗口,例如:
<style>
html, body {
overflow: none;
}
</style>
<script>
window.addEventListener('DOMMouseScroll', onScroll, false);
window.onmousewheel = function onScroll(event) {
// Use "event" to distinguish between up or down
// for which you determine which way to scroll
// the particular div you want.
}
</script>
<script>
window.addEventListener('DOMMouseScroll', onMouseWheel, false);
window.onmousewheel = document.onmousewheel = onMouseWheel; // IE
function onMouseWheel(event) {
event.stopPropagation();
event.preventDefault();
event.cancelBubble = true;
// Use "event" to distinguish between up or down
// for which you determine which way to scroll
// the particular div you want.
return false;
}
</script>