Created
May 26, 2017 17:59
-
-
Save ertugrulcetin/6c3c5f2939f344454d8e68efe075fd3b to your computer and use it in GitHub Desktop.
Smooth scroll Clojurescript example
This file contains 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
(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