I have a overflow-y:scroll;
.
Links to anchors within innerContent are located on parent page, not in the
Inspired by Morlem's code, but working the other way around: stop the propagation only if scrolling out. In pure JS:
container.addEventListener('mousewheel', function(e) {
var scrollingOverTheTop = (e.wheelDelta > 0 && this.scrollTop == 0);
var scrollingOverTheBottom = (e.wheelDelta < 0 && (this.scrollTop >= this.scrollHeight - this.offsetHeight));
if (scrollingOverTheBottom || scrollingOverTheTop) {
e.preventDefault();
e.stopPropagation();
}
}, true);
This jsfiddle works in Chrome for me. Not tested in other browsers.
Catches the mousewheel event, uses the event data to scroll manually, then cancels the original event. Seems potentially messy for production.
$('#scroll').bind('mousewheel', function(e){
$(this).scrollTop($(this).scrollTop()-e.originalEvent.wheelDeltaY);
//prevent page fom scrolling
return false;
});
Use a fixed layout. In this jsfiddle I have a working example for it. Study the css. You don't need javascript for it.
And if you do decide to use javascript (again, like KooiInc said, if you don't need js, don't use it), you can try using event.cancelBubble = true
, which would prevent the event from propagating to the parent container so the page would not see your inner-div scrolling. Additional command you can use is event.preventDefault()
, which prevents browser from triggering default behavior (i.e. scrolling) to the event.
I just solved my issue with this by creating the following in CSS:
body.scroll_locked {
height: 100%;
width: 100%;
overflow: hidden;
}
Then, when I show my modal/lightbox, which is JavaScript (jQuery), I add the scroll_locked class to the body to lock it in place and remove the class to go back. Seems like you could do the same for a mouseenter/mouseleave event for a div that's always on the page and you want to have the same effect.
$('body').addClass('scroll_locked');
$('body').removeClass('scroll_locked');