Created
October 7, 2020 14:32
-
-
Save chrisryana/cee98ea48b8347579a4d4a2dd852e96d to your computer and use it in GitHub Desktop.
Получить ширину скроллбара (для macOS если стоит overflow: auto)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
.scrollbar-pain { | |
width: 100px; | |
height: 100px; | |
overflow: scroll; | |
position: absolute; | |
top: -9999px; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
useEffect(() => { | |
// создаем элемент за пределами страницы | |
const scrollDiv = document.createElement('div'); | |
scrollDiv.className = 'scrollbar-measure'; | |
document.body.appendChild(scrollDiv); | |
const scrollbarWidth = scrollDiv.offsetWidth - scrollDiv.clientWidth; | |
// обновляем state | |
setScrollWidth(scrollbarWidth); | |
// удаляем элемент | |
document.body.removeChild(scrollDiv); | |
// вызываем один раз при рендеринге компонента | |
}, []); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment