Last active
December 14, 2015 23:29
-
-
Save swannodette/5165980 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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