Skip to content

Instantly share code, notes, and snippets.

@poteto
Last active August 29, 2015 14:10
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save poteto/d88d19dbd23c04b7f9e7 to your computer and use it in GitHub Desktop.
Save poteto/d88d19dbd23c04b7f9e7 to your computer and use it in GitHub Desktop.
import Ember from 'ember';
export default Ember.Mixin.create({
scrollTimeout: 100,
boundingClientRect: 0,
windowHeight: 0,
windowWidth: 0,
enteredViewport: Ember.computed('boundingClientRect', 'windowHeight', 'windowWidth', function() {
var rect, windowHeight, windowWidth;
rect = this.get('boundingClientRect');
windowHeight = this.get('windowHeight');
windowWidth = this.get('windowWidth');
return (
rect.top >= 0 &&
rect.left >= 0 &&
rect.bottom <= windowHeight &&
rect.right <= windowWidth
);
}),
exitedViewport: Ember.computed.not('enteredViewport'),
_updateBoundingClientRect: function() {
var el;
el = this.$()[0];
this.set('boundingClientRect', el.getBoundingClientRect());
},
_setup: (function() {
return Ember.run.scheduleOnce('afterRender', this, function() {
this._updateBoundingClientRect();
this.set('windowHeight', window.innerHeight || document.documentElement.clientHeight);
this.set('windowWidth', window.innerWidth || document.documentElement.clientWidth);
});
}).on('didInsertElement'),
_scrollHandler: function() {
return Ember.run.debounce(this, '_updateBoundingClientRect', this.get('scrollTimeout'));
},
_bindScroll: (function() {
var scrollHandler;
scrollHandler = this._scrollHandler.bind(this);
Ember.$(document).on('touchmove.scrollable', scrollHandler);
Ember.$(window).on('scroll.scrollable', scrollHandler);
}).on('didInsertElement'),
_unbindScroll: (function() {
Ember.$(window).off('.scrollable');
Ember.$(document).off('.scrollable');
}).on('willDestroyElement')
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment