Skip to content

Instantly share code, notes, and snippets.

@ertugrulcetin
Created May 26, 2017 17:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ertugrulcetin/6c3c5f2939f344454d8e68efe075fd3b to your computer and use it in GitHub Desktop.
Save ertugrulcetin/6c3c5f2939f344454d8e68efe075fd3b to your computer and use it in GitHub Desktop.
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