|
| 1 | +/* |
| 2 | +* Copyright 2020-2023 Gemeente Heerenveen |
| 3 | +* |
| 4 | +* Licensed under the EUPL, Version 1.2 or – as soon they will be approved by the European Commission - subsequent versions of the EUPL (the "Licence"); |
| 5 | +* You may not use this work except in compliance with the Licence. |
| 6 | +* You may obtain a copy of the Licence at: |
| 7 | +* |
| 8 | +* https://joinup.ec.europa.eu/collection/eupl/eupl-text-eupl-12 |
| 9 | +* |
| 10 | +* Unless required by applicable law or agreed to in writing, software distributed under the Licence is distributed on an "AS IS" basis, |
| 11 | +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +* See the Licence for the specific language governing permissions and limitations under the Licence. |
| 13 | +*/ |
| 14 | + |
| 15 | +/** |
| 16 | + * Calculate width of scrollbar |
| 17 | + * |
| 18 | + * Source: https://stackoverflow.com/a/16771535 |
| 19 | + * @returns {int} Width of scrollbar in pixels |
| 20 | + */ |
| 21 | +const _getScrollbarWidth = () => { |
| 22 | + let width = _getScrollbarWidth.width; |
| 23 | + if (width === undefined) { |
| 24 | + let div = document.createElement('div'); |
| 25 | + div.innerHTML = '<div style="width:50px;height:50px;position:absolute;left:-50px;top:-50px;overflow:auto;"><div style="width:1px;height:100px;"></div></div>'; |
| 26 | + div = div.firstChild; |
| 27 | + document.body.appendChild(div); |
| 28 | + width = _getScrollbarWidth.width = div.offsetWidth - div.clientWidth; |
| 29 | + document.body.removeChild(div); |
| 30 | + } |
| 31 | + return width; |
| 32 | +}; |
| 33 | + |
| 34 | +/** |
| 35 | + * Calculate width of scrollbar |
| 36 | + * |
| 37 | + * Source: https://stackoverflow.com/a/16771535 |
| 38 | + * @returns {int} Width of scrollbar in pixels |
| 39 | + */ |
| 40 | +export function getScrollbarWidth() { |
| 41 | + return _getScrollbarWidth(); |
| 42 | +} |
0 commit comments