Skip to content

Instantly share code, notes, and snippets.

@rsslldnphy
Last active August 29, 2015 14:17
Show Gist options
  • Save rsslldnphy/d08f6279148b137e7618 to your computer and use it in GitHub Desktop.
Save rsslldnphy/d08f6279148b137e7618 to your computer and use it in GitHub Desktop.
Get the scrollbar width
;; Adapted from http://stackoverflow.com/a/8079681
(defonce scrollbar-width
(let [inner (.createElement js/document "p")
outer (.createElement js/document "div")]
(doto (.-style inner)
(aset "width" "100%")
(aset "height" "500px"))
(doto (.-style outer)
(aset "position" "absolute")
(aset "top" "0px")
(aset "left" "0px")
(aset "visibility" "hidden")
(aset "background" "red")
(aset "width" "200px")
(aset "height" "150px")
(aset "overflow" "hidden"))
(.appendChild outer inner)
(.appendChild (.-body js/document) outer)
(let [w1 (.-offsetWidth inner)
_ (aset (.-style outer) "overflow" "scroll")
w2 (.-offsetWidth inner)
w2 (if (= w1 w2) (.-clientWidth outer) w2)]
(.removeChild (.-body js/document) outer)
(- w1 w2))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment