Skip to content

Instantly share code, notes, and snippets.

@Lochlan
Last active August 29, 2015 14:05
Show Gist options
  • Save Lochlan/88350fc3bd7044c053f8 to your computer and use it in GitHub Desktop.
Save Lochlan/88350fc3bd7044c053f8 to your computer and use it in GitHub Desktop.
Backbone BackToTopView
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