Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I prevent screen scrolling on mouse drag when an absolutely positioned element is (partially) off screen?

I don't want my web page to scroll. I have a <body> of width 100%, I set its overflow to hidden, and it generally works. No scroll bars, no mousewheel / touchpad gestures scroll. But if I click and drag just anywhere, I can still scroll.

The stinker is that I have a div absolutely positioned so it's sticking outside of its parent, partially (and sometimes entirely) off screen. For a handful of reasons, I can't get around that right now.

This fiddle illustrates my problem (I am seeing it in webkit), http://jsfiddle.net/ax5x7/2/

Dragging pretty much anywhere off to the right in the HTML will cause the page to slide along. It's tricky to go back because there are no scroll bars, so if you do this by mistake, it looks bad.

Poking around here found one suggestion related to jQuery's deprecated disableSelection(), but unless I'm doing it wrong (I applied it to body as well as all the children) it doesn't seem to prevent the behaviour. I also found a question a couple of years old that didn't seem to have a satisfactory solution. The bandaid is a bit kludgy, I'd rather not scroll to (0,0) on an interval.

like image 681
Terraflubb Avatar asked May 28 '13 02:05

Terraflubb


1 Answers

This will work, and it won't disable the selection of text:

$(document).on('scroll', function() {
  $(document).scrollLeft(0);
});

You can add $(document).scrollTop(0); if you want to disable scrolling down.

Here's a jsFiddle

like image 198
tom-19 Avatar answered Sep 21 '22 02:09

tom-19