Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Disable outside scrolling while in scrollable div [duplicate]

I've got a few divs that hold information that you can scroll through. But when you hit the top or bottom it changes to the browsers scroll which scrolls the entire page.

I'd like to disable outside scrolling while the mouse is inside of this particular div (there's three of this type of div).

An example of what I'm trying to acheive is youtubes playlist selector.

Here's an example at youtube Hover your mouse inside the popular uploads part and trying scrolling all the way up and down.

EDIT: just tried to use the link in another browser and the video selection part doesn't seem to show up, but it's the "add to playlist" part that enables it.

Does anybody know of a way to do this?

like image 454
Trevor Wood Avatar asked Jul 23 '14 15:07

Trevor Wood


People also ask

How do I disable scroll behavior in CSS?

To hide the scrollbar and disable scrolling, we can use the CSS overflow property. This property determines what to do with content that extends beyond the boundaries of its container. To prevent scrolling with this property, just apply the rule overflow: hidden to the body (for the entire page) or a container element.

How Do I Stop background scroll when Modal is open?

Approach: A simple solution to this problem is to set the value of the “overflow” property of the body element to “hidden” whenever the modal is opened, which disables the scroll on the selected element.


2 Answers

Here's a FIDDLE

<div>
  ... some content ...
</div>

div {
  height: 400px;
  overflow: scroll;
}

$('div').on('mousewheel DOMMouseScroll', function(e) {
  var scrollTo = null;

  if(e.type === 'mousewheel') {
     scrollTo = (e.originalEvent.wheelDelta * -1);
  }
  else if(e.type === 'DOMMouseScroll') {
     scrollTo = 40 * e.originalEvent.detail;
  }

  if(scrollTo) {
     e.preventDefault();
     $(this).scrollTop(scrollTo + $(this).scrollTop());
  }
});
like image 191
mdesdev Avatar answered Oct 18 '22 11:10

mdesdev


Untested, but might be the answer:

$( "div" ).scroll(function( event ) {
    event.stopPropagation();//Do not bubble up the DOM, do not scroll document.
});

stopPropagation() is used to prevent the event of bubbling up the DOM. This way other elements are not notified of this event.

Read more: http://api.jquery.com/event.stoppropagation/

like image 23
Salketer Avatar answered Oct 18 '22 11:10

Salketer