Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unable to stop the scrolling of the outer div when end of inner div has reached

Use Case : - If the user is scrolling an inner div it should not scroll the outer div once inner div end has reached.

The following code works perfectly fine if the inner div has already reached the end. i.e the touchstart/touchmove event triggers after the inner div has reached the end.

But in case I do not lift the touch and keeps scrolling and if the end of the inner div has reached in this process, it starts scrolling the outer div.

$('body').find('.scrollableDiv').on(
    {
      'touchstart' :  function(e) {
        touchStartEvent = e;
      },
      'touchmove' : function(e) {
        e.stopImmediatePropagation();
        $this = $(this);
        if ((e.originalEvent.touches[0].pageY > touchStartEvent.originalEvent.touches[0].pageY && this.scrollTop == 0) || 
            (e.originalEvent.touches[0].pageY < touchStartEvent.originalEvent.touches[0].pageY && this.scrollTop + this.offsetHeight >= this.scrollHeight)){
          e.preventDefault();
        }
      },  
  }); 

How do I stop scrolling as soon as the end of the inner div is reached? I am trying to achieve this on a browser on android device. Can someone help me in this regard?

NOTE : The user should even be able to scroll the outer div.

Thanks in advance.

like image 539
Keen Sage Avatar asked Aug 21 '13 17:08

Keen Sage


People also ask

How do I stop Div scrolling?

And it is simply by adding the CSS property overflow: hidden; on the element you want to prevent the scroll.

How do you make an element not scrollable?

To prevent scrolling with this property, just apply the rule overflow: hidden to the body (for the entire page) or a container element. This hides all content beyond the element's border.

How do I keep my Div scrolled to the bottom?

use css position top keep it at the bottom {position : relative; bottom:0;} . Remove the css property once the user has scroll.


1 Answers

I'm afraid I don't have time to write the explicit code to solve this, but I've got a conceptual solution for you that still might help you.

  1. On DOM ready, wrap each overflow: scroll element with a wrapper with position: relative.
  2. On touch start, for each ancestor with overflow: scroll, set overflow: visible, position: absolute, get calculated height & width and apply these as inline styles, and set top to be the negative of scrollTop — and set the wrapper's overflow to hidden: this means it will be in the same position it was in the first place but unable to move through scrolling.
  3. On touch end, remove the styles applied in step 2.

I can already see caveats:

  1. If you're using relative or absolute positioning for layout within your overflow: scroll elements (and they do not have relative or absolute positioning themselves), this will screw with your layout.
  2. If any scrolling elements are wrapped over after DOM ready, you will need to apply a wrapper for each.

…but it's definitely feasible…

like image 92
Barney Avatar answered Jan 05 '23 00:01

Barney