From 1f9d1c804aed0435e2382022b11bcdbf5ea286c3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Saint-Sevin?= Date: Sat, 10 Feb 2018 11:10:59 +0100 Subject: [PATCH] add browser scrollbar width detection --- simple-scrollbar.js | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/simple-scrollbar.js b/simple-scrollbar.js index 4d52bb1..7ee098b 100644 --- a/simple-scrollbar.js +++ b/simple-scrollbar.js @@ -51,6 +51,7 @@ this.el = d.createElement('div'); this.el.setAttribute('class', 'ss-content'); + this.el.style.width = 'calc(100% + ' + ss.scrollWidth + 'px)'; if (this.direction === 'rtl') { this.el.classList.add('rtl'); @@ -76,8 +77,8 @@ this.target.classList.add('ss-container'); var css = w.getComputedStyle(el); - if (css['height'] === '0px' && css['max-height'] !== '0px') { - el.style.height = css['max-height']; + if (css['height'] === '0px' && css['max-height'] !== '0px') { + el.style.height = css['max-height']; } } @@ -114,9 +115,21 @@ } } + function getScrollWidth() { + var outer = d.createElement('div'); + var inner = d.createElement('div'); + outer.style.width = inner.style.width = '100%'; + outer.style.overflow = 'scroll'; + d.body.appendChild(outer).appendChild(inner); + var scroll_width = outer.offsetWidth-inner.offsetWidth; + outer.parentNode.removeChild(outer); + return scroll_width; + } + d.addEventListener('DOMContentLoaded', initAll); ss.initEl = initEl; ss.initAll = initAll; + ss.scrollWidth = getScrollWidth(); w.SimpleScrollbar = ss; })(window, document);