Hijack the windows scrollbar
This may seem like an odd question but I need to know if it's possible to hijack the window scrollbar so when a user scrolls it it doesn't scroll the page. I want to write some js that instead when the user scrolls the window scrollbar it scrolls a div. I can write the js to 开发者_JAVA技巧detect the scrolls and how much to animate the div etc but not sure how to hijack the window scrollbar and stop it scrolling the window Is this possible?
Instead of trying to "hijack" the browser window, you could try a number of other ways, such as:
<- Overflow ---------------------------------------------------->
[css]:
<style>
html, body {
overflow: none;
}
</style>
[javascript]:
<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>
<- Preventing Default --------------------------------------->
[javascript (untested)]:
<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>
精彩评论