Created
August 30, 2013 01:12
-
-
Save tdreyno/6385307 to your computer and use it in GitHub Desktop.
Port of hoverable body class support from JS to CLJS
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
| (defn setup-scroll-watcher | |
| [body-class] | |
| (let [body-class (name body-class) | |
| listener (shared/listen js/window :scroll) | |
| debounced (shared/debounce listener 200)] | |
| (dom/add-class! (.-body js/document) body-class) | |
| (go | |
| (loop [] | |
| (<! listener) | |
| (dom/remove-class! (.-body js/document) body-class) | |
| (<! debounced) | |
| (dom/add-class! (.-body js/document) body-class) | |
| (recur))))) | |
| (defn set-body-class-when-not-scrolling! | |
| ([body-class] (set-body-class-when-not-scrolling! body-class (not (.-touch js/Modernizr)))) | |
| ([body-class mouse-detected] | |
| (if mouse-detected | |
| (setup-scroll-watcher body-class) | |
| (go | |
| (<! (shared/listen-once js/document :mousemove)) | |
| (set-body-class-when-not-scrolling! body-class true))))) |
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
| ww.util.mouseEventsDetected = !Modernizr['touch']; | |
| ww.util.listeningForFirstMouseEvent = false; | |
| ww.util.listenForFirstMouseEvent = function(cb) { | |
| if (ww.util.listeningForFirstMouseEvent) { return; } | |
| ww.util.listeningForFirstMouseEvent = true; | |
| goog.events.listenOnce(document, 'mousemove', function() { | |
| ww.util.mouseEventsDetected = true; | |
| ww.util.listeningForFirstMouseEvent = false; | |
| cb(); | |
| }); | |
| }; | |
| ww.util.isScrolling = window['isScrolling'] = false; | |
| /** | |
| * Add a 'hoverable' body class when :hover events should | |
| * be attached (eg not scrolling or on touch device). | |
| * @param {String} clsName Name of the class to apply. Default: hoverable. | |
| */ | |
| ww.util.applyBodyClassWhenNotScrolling = function(clsName) { | |
| clsName = clsName || 'hoverable'; | |
| if (!ww.util.mouseEventsDetected) { | |
| ww.util.listenForFirstMouseEvent(function() { | |
| ww.util.applyBodyClassWhenNotScrolling(clsName); | |
| }); | |
| return; | |
| } | |
| goog.dom.classes.add(document.body, clsName); | |
| var debouncedOnScroll = ww.util.debounce(function() { | |
| ww.util.isScrolling = window['isScrolling'] = false; | |
| goog.dom.classes.add(document.body, clsName); | |
| }, 200); | |
| goog.events.listen(window, 'scroll', function() { | |
| if (!ww.util.isScrolling) { | |
| ww.util.isScrolling = window['isScrolling'] = true; | |
| goog.dom.classes.remove(document.body, clsName); | |
| } | |
| debouncedOnScroll(); | |
| }); | |
| }; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment