Last active
January 30, 2017 17:28
-
-
Save James1x0/fb94567cb37865d9cfd9 to your computer and use it in GitHub Desktop.
Ember Scrollspy
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
import Ember from 'ember'; | |
import windowBinderMixin from '../mixins/window-binder'; | |
export default Ember.View.extend(windowBinderMixin, { | |
templateName: 'scroll-spy', | |
classNames: [ 'scroll-spy-view', 'affix' ], | |
// Bind the window events on view insertion | |
didInsertElement: function () { | |
this.setupWindowBindings(); | |
}, | |
// Teardown the window event bindings on view destruction | |
willDestroyElement: function () { | |
this.teardownWindowBindings(); | |
}, | |
windowDidScroll: function () { | |
Ember.run.once(this, this.handleWindowScroll); | |
}.observes('didScroll'), | |
handleWindowScroll: function () { | |
var self = this, | |
$this = this.$(), | |
spyOffset = $this.offset(); | |
this.set('controller.content', this.get('controller.content').map(function (section) { | |
var $secEl = $('.enrollment-card[data-section="' + section.title + '"]'), | |
secOffset = $secEl.offset(); | |
section.active = (spyOffset.top + 20 > secOffset.top && spyOffset.top < ( $secEl.height() + secOffset.top ) ) ? true : false; | |
return section; | |
})); | |
setTimeout(function () { | |
self.set('didScroll', false); | |
}, 500); | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment