Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Smooth scroll Clojurescript example
(def speed 750)
(def moving-frequency 15)
(defn scroll-to-id
[elem target-id]
(let [target (.getElementById js/document target-id)
elem-scroll-top (-> elem .-scrollTop)
hop-count (/ speed moving-frequency)
gap (/ (- (-> target .-offsetTop) elem-scroll-top) hop-count)]
(doseq [i (range 1 (inc hop-count))]
(let [move-to (+ elem-scroll-top (* gap i))
timeout (* moving-frequency i)]
(js/setTimeout #(set! (-> elem .-scrollTop) move-to) timeout)))))
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.