Last active
August 29, 2015 14:05
-
-
Save Lochlan/88350fc3bd7044c053f8 to your computer and use it in GitHub Desktop.
Backbone BackToTopView
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
define([ | |
'jquery', | |
'underscore', | |
'backbone', | |
], function ($, _, Backbone) { | |
'use strict'; | |
var BackToTopView = Backbone.View.extend({ | |
initialize: function (options) { | |
_(this.options).extend(options); | |
$(window).on('scroll', _(this.moveToPosition.bind(this)).debounce(200)); | |
this.moveToPosition(); | |
}, | |
options: { | |
start_offset: 0, | |
}, | |
moveToPosition: function () { | |
var parentBottom = this.$el.parent().offset().top + this.$el.parent().outerHeight(); | |
var viewportBottom = window.scrollY + window.innerHeight; | |
this.$el.css({ | |
bottom: window.scrollY > this.options.start_offset ? Math.max(parentBottom - viewportBottom, 0) : 0, | |
}); | |
}, | |
}); | |
return BackToTopView; | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment