The event scroll
can’t cancel. That’s very clear in the specification of W3C.
The only way I know how to get around this is:
overflow: hidden;
- an event receiver that forces the scroll position not to change
The first option, to give overflow: hidden;
no body is widely used in cases where the scroll
should not happen. In your case you want the scroll
work until a certain point and then stop working. To use this idea you have to read the scroll position and apply the overflow: hidden;
in the body
or parent element of the other.
Example:
CSS
div {
height: 4000px;
border: 1px solid #ccf;
}
.pararScroll {
overflow: hidden;
background-color: #eee;
}
JS
window.addEventListener('scroll', function (e) {
var scrollTop = window.pageYOffset || document.documentElement.scrollTop;
if (scrollTop > 1000) document.body.classList.add('pararScroll');
});
jsFiddle: http://jsfiddle.net/51p1tq4u/
The second option need you to save the value of the last scroll position. So at each event scroll
, the position must be the one prior to that event scroll
.
Example:
var lastScroll;
window.addEventListener('scroll', function (e) {
if (lastScroll) return window.scrollTo(0, lastScroll);
var scrollTop = window.pageYOffset || document.documentElement.scrollTop;
if (scrollTop > 1000) lastScroll = scrollTop;
});
jsFiddle: http://jsfiddle.net/51p1tq4u/1/
And if you want to turn the scroll back on, you just have to change the value of lastScroll
to a value that has boolean value false
.
tried the event.stopImmediatePropagation() ?
– SneepS NinjA