-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathscrollbar-width.coffee
More file actions
40 lines (29 loc) · 1.12 KB
/
scrollbar-width.coffee
File metadata and controls
40 lines (29 loc) · 1.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# scrollbar-width.coffee
#
# calculate the width of the scrollbar in the browser.
# exports a function (AMD/CommonJS), or sets function
# window.scrollbarWidth (standard browser environment)
# Will cache the value, untill the function is called
# with the recalculate parameter set to true.
'use strict'
scrollbarWidth = null
getScrollbarWidth = (recalculate = false) ->
return scrollbarWidth if scrollbarWidth? and not recalculate
return null if document.readyState is 'loading'
div1 = document.createElement 'div'
div2 = document.createElement 'div'
div1.style.width = div2.style.width = div1.style.height = div2.style.height = '100px'
div1.style.overflow = 'scroll'
div2.style.overflow = 'hidden'
document.body.appendChild div1
document.body.appendChild div2
scrollbarWidth = Math.abs div1.scrollHeight - div2.scrollHeight
document.body.removeChild div1
document.body.removeChild div2
scrollbarWidth
if typeof define is 'function' and define.amd
define [], -> getScrollbarWidth
else if typeof exports isnt 'undefined'
module.exports = getScrollbarWidth
else
@getScrollbarWidth = getScrollbarWidth