Skip to content

Instantly share code, notes, and snippets.

@swannodette
Last active December 14, 2015 23:29
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save swannodette/5165980 to your computer and use it in GitHub Desktop.
Save swannodette/5165980 to your computer and use it in GitHub Desktop.
(defprotocol IOffset
(-offset [x]))
(extend-type js/Element
IOffset
(-offset [x]
[(.-offsetLeft x) (.-offsetTop x)]))
(defprotocol IScroll
(-scroll [x]))
(extend-type js/Element
IScroll
(-scroll [x]
[(.-scrollLeft x) (.-scrollTop x)]))
(defn addv [v0 v1] (map + v0 v1))
(defn subv [v0 v2] (map - v0 v1))
(defn parent-seq [x]
(when-let [p (.-offsetParent x)]
(lazy-seq (cons p (parent-seq p)))))
(defn total-offset [el]
(let [xs (cons el (parent-seq el))
vs (map subv (map -offset xs) (map -scroll xs))]
(reduce addv [0 0] vs)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment