Skip to content

Instantly share code, notes, and snippets.

@iammerrick
Created May 3, 2011 15:21
Show Gist options
  • Save iammerrick/953520 to your computer and use it in GitHub Desktop.
Save iammerrick/953520 to your computer and use it in GitHub Desktop.
define(function(){
return Backbone.View.extend({
el: "#bone-navigation",
events: {
"click a.toggle" : "toggleView"
},
initialize: function(){
_.bindAll(this, "toggleView", "windowScroll", "windowResize");
$(window).bind("scroll", this.windowScroll);
$(window).bind("resize", this.windowResize);
this.initNavigationTop();
},
toggleView: function(e){
e.preventDefault();
console.log("Toggle View");
$(e.target).toggleClass("grid");
},
initNavigationTop: function(){
var element = $(this.el);
element.data("topPosition", element.position().top);
this.setWidth();
},
setWidth: function(){
var element = $(this.el);
element.css('width', $('.documents-list').width());
},
windowResize: function(){
this.setWidth();
},
windowScroll: function(e){
var element = $(this.el), elementOffset = $(this.el).data("topPosition"), scrollTrigger = $(window).scrollTop();
if(elementOffset < scrollTrigger && element.css("position") !== "fixed"){
element.css({
"position": "fixed",
"top" : 0
});
} else if(elementOffset > scrollTrigger && element.css("position") === "fixed"){
element.css({
"position": "absolute",
"top" : elementOffset
});
}
}
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment