willmcpo / body-scroll-lock

Body scroll locking that just works with everything 😏
MIT License
4.04k stars 337 forks source link

Content in the target not scrolling #100

Open paragonn opened 5 years ago

paragonn commented 5 years ago

I implemented the script on a plain vanilla js config and it is successfully locking the body scroll, however the content in the target is not scrolling. Their are 4 pages (slides) inside the target as part of a stepped form. The second step in the form is larger than the mobile screen size however it is not scrolling. You can see a demo here: https://dev.ijm.org and click on the red Give button. Help would be greatly appreciated!

diachedelic commented 5 years ago

Can you try using the allowTouchMove option?

paragonn commented 5 years ago

Thanks @diachedelic -- I applied the allowTouchMove to the #draw1-wrap div and the scroll now works, however the body unfortunately scrolls too?

diachedelic commented 5 years ago

Can you paste your allowTouchMove function here?

paragonn commented 5 years ago

Sure thing @diachedelic, here it is:

disableBodyScroll(targetElement, {
  allowTouchMove: el => (document.querySelector("#draw1-wrap"))
});  
diachedelic commented 5 years ago

allowTouchMove expects a boolean, try this:

disableBodyScroll(targetElement, {
  allowTouchMove: el => (el === document.querySelector("#draw1-wrap"))
}); 
paragonn commented 5 years ago

Thanks @diachedelic -- The body scroll lock is now working again, however the content on page/slide 2 is not vertical scrolling. Do you think it could have something to do with a styling in the css?

retailonline commented 5 years ago

Same problem here.

diachedelic commented 5 years ago

@paragonn #draw1-wrap is possibly not the correct element to target, or you need to target other elements too - make sure you target all elements which have overflowing content.

diachedelic commented 5 years ago

image

Looking at your demo, try targeting div.drawer instead

paragonn commented 5 years ago

Thanks for your assistance @diachedelic -- I tried your recommendation and overflow content still does not vertical scroll. Here is the code for the current staging (dev.ijm.org) of the problem:

disableBodyScroll(targetElement, {
  allowTouchMove: el => (el === document.querySelector("#draw1-wrap"))
});   

Where the targetElement is defined like so:

const targetElement = document.querySelector("#draw1");

Do you have any other recommendations?

diachedelic commented 5 years ago

I see you've used the body-scroll-lock-ignore attribute - for that attribute to work, you need to adjust your allowTouchMove function like

  disableBodyScroll(container, {
    allowTouchMove: el => {
      while (el && el !== document.body) {
        if (el.getAttribute('body-scroll-lock-ignore') !== null) {
          return true
        }

        el = el.parentNode
      }
    },
  });

And you can safely remove body-scroll-lock-ignore from all the children elements - you only need it on the outermost scrolling element, all elements within will automatically be allowed to scroll.

diachedelic commented 5 years ago

So just leave it on #draw1-wrap like <div id="draw1-wrap" body-scroll-lock-ignore> (doing body-scroll-lock-ignore="" might do the opposite of what you want...)

limaneto commented 5 years ago

If there is a parent between body and target's parent, and that parent has overflowing content, is it going to work? Or the scroll lock should be applied on the component inside that between parent?

diachedelic commented 5 years ago

@limaneto I'm having a hard time visualising your issue, could you post a JS Fiddle or something?

limaneto commented 5 years ago

do you guys have any live example with React?

diachedelic commented 5 years ago

I have no idea, I'll wait for someone else to answer that

On 16 Jul 2019, at 10:34 am, Lima Neto notifications@github.com wrote:

do you guys have any live example with React?

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/willmcpo/body-scroll-lock/issues/100?email_source=notifications&email_token=AACLZQHQOZY3P47L24MFPATP7UJSVA5CNFSM4HDV7MXKYY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGODZ7K5TQ#issuecomment-511618766, or mute the thread https://github.com/notifications/unsubscribe-auth/AACLZQEWWGBDWNSPEPA2WK3P7UJSVANCNFSM4HDV7MXA.

bobberg commented 5 years ago

For the people that still have this problem after trying the fix... Try destroying the body and turning the div into the body dynamically. And add a close button that reloads the page. Hacky, but works perfectly in my project.

llausa commented 4 years ago

Used this package in React, added the attribute to the component as a prop.

The rest of the code was as suggested by the Documentation.

To get this working, we grabbed the component through a class and <className>.setAttribute('body-scroll-lock-ignore', 'true') within componentDidUpdate()

Keep in mind you could do the same with ID, jsut replace the code above with ID instead of Class stuff, etc

Might help out some of you :smiley:

abeledovictor commented 3 years ago

@llausa mind creating a small snippet/code sandbox for react?

I'm struggling with the same problem, for now, I avoided using the scroll lock in just one view where I have a drawer where I need the scroll