Skip to content

Instantly share code, notes, and snippets.

@mattaebersold
Created November 11, 2013 02:14
Show Gist options
  • Save mattaebersold/7406693 to your computer and use it in GitHub Desktop.
Save mattaebersold/7406693 to your computer and use it in GitHub Desktop.
Example of my JS
define(function(require) {
// Dependencies
var $ = require('jquery'),
_ = require('underscore'),
Backbone = require('backbone'),
app = require('modules/app'),
hammer = require('plugins/jquery-hammer');
// Init view
var View = {
isHomeNav: false
};
// Constructor
View.initialize = function() {
_.bindAll(this);
// Selectors
this.$win = $(window);
this.$body = $('body');
this.$home = this.$el.find('.home');
this.$about = this.$el.find('.about');
this.$inspiration = this.$el.find('.inspiration');
this.$grabber = $('.grabber');
this.$mobile_nav = $('.mobile-nav');
this.$close = this.$mobile_nav.find('.close');
// IF HOME, then hide the nav, and add the scroll listener
if(this.$body.hasClass('home')) {
//this.$home.remove();
app.on('scroll', this.onHomePageScroll);
} else {
// animate in the nav
this.$el.addClass('on');
}
this.$grabber.hammer().on('tap', this.openMobileNav);
this.$close.hammer().on('tap', this.closeMobileNav);
};
// Home Page Scroll
View.onHomePageScroll = function(e) {
if(this.$win.scrollTop() <= app.windowHeight) {
if(this.$el.hasClass('on')) this.$el.removeClass('on');
return;
}
if(this.$win.scrollTop() <= app.windowHeight+130) {
if(this.$el.hasClass('on')) this.$el.removeClass('on');
} else {
if(!this.$el.hasClass('on')) this.$el.addClass('on');
}
};
// Toggle the mobile nav
View.openMobileNav = function(e) {
this.$mobile_nav.addClass('active');
};
// Close the mobile nav
View.closeMobileNav = function(e) {
this.$mobile_nav.removeClass('active');
};
// Return the view
return Backbone.View.extend(View);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment