willmcpo / body-scroll-lock

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

Scroll bar width calculated incorrectly #239

Open krutoo opened 2 years ago

krutoo commented 2 years ago

Hi, first of all, thanks for great library

In the source code there is a calculation of the scrollbar width https://github.com/willmcpo/body-scroll-lock/blob/79c4cf2c956eb7d5cf8d54a03d12751bc6ac8aa3/src/bodyScrollLock.js#L76

const scrollBarGap = window.innerWidth - document.documentElement.clientWidth;

it doesn't work correctly if the body has styles min-width: 995px and page has meta:

<meta name="viewport" content="width=device-width" />

It looks like: image

Maybe solution like in this article would be more properly: https://javascript.info/task/scrollbar-width

// create a div with the scroll
let div = document.createElement('div');

div.style.overflowY = 'scroll';
div.style.width = '50px';
div.style.height = '50px';

// must put it in the document, otherwise sizes will be 0
document.body.append(div);
let scrollWidth = div.offsetWidth - div.clientWidth;

div.remove();

alert(scrollWidth);
r0ughnex commented 2 years ago

I can confirm this happens 👍

AVL68 commented 2 years ago

Good day! Any comments from the developers of this library?

rick-liruixin commented 10 months ago

They stopped the repairs. I had to do it myself, in the same way, with a new version of typeScript. And fix these problems for everyone to use. add react hooks、vue3 example

npm i body-scroll-lock-upgrade

repair log,Refer to the releases page. @r0ughnex @krutoo @AVL68