Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
ClojureScript scroll element into view
;; Based on
(ns example.scroll)
(def speed 500)
(def moving-frequency 15)
(defn cur-doc-top []
(+ (.. js/document -body -scrollTop) (.. js/document -documentElement -scrollTop)))
(defn element-top [elem top]
(if (.-offsetParent elem)
(let [client-top (or (.-clientTop elem) 0)
offset-top (.-offsetTop elem)]
(+ top client-top offset-top (element-top (.-offsetParent elem) top)))
(defn scroll-to-id
(let [elem (.getElementById js/document elem-id)
hop-count (/ speed moving-frequency)
doc-top (cur-doc-top)
gap (/ (- (element-top elem 0) doc-top) hop-count)]
(doseq [i (range 1 (inc hop-count))]
(let [hop-top-pos (* gap i)
move-to (+ hop-top-pos doc-top)
timeout (* moving-frequency i)]
(.setTimeout js/window (fn []
(.scrollTo js/window 0 move-to))

This comment has been minimized.

Copy link
Owner Author

jasich commented Dec 11, 2016

ClojureScript implementation of Gabriel Delépine's Smooth-Scroll project.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.