Skip to content

Instantly share code, notes, and snippets.

@philbirnie
Created May 21, 2014 13:44
Show Gist options
  • Save philbirnie/d3b9594f94f96635dd77 to your computer and use it in GitHub Desktop.
Save philbirnie/d3b9594f94f96635dd77 to your computer and use it in GitHub Desktop.
Dead simple position mover for an element or two to keep everything tidy
/*jslint browser: true*/
/*global jQuery */
var QuickBrick = {
topEntity: null,
bottomEntity: null,
init: function (top, bottom) {
"use strict";
this.topEntity = top;
this.bottomEntity = bottom;
},
recalculate: function () {
"use strict";
var topBottomPos,
bottomTopPos,
newPosition;
jQuery(this.bottomEntity).css(
{
position: "static"
}
);
bottomTopPos = jQuery(this.bottomEntity).offset().top;
topBottomPos = jQuery(this.topEntity).offset().top + jQuery(this.topEntity).outerHeight();
newPosition = topBottomPos - bottomTopPos;
jQuery(this.bottomEntity).css(
{
top: newPosition + "px",
position: "relative"
}
);
},
bindHandlers: function () {
"use strict";
var self = this;
jQuery(window).on("resize", function () {
self.recalculate();
});
},
destroy: function () {
"use strict";
jQuery(window).off("resize");
jQuery(this.bottomEntity).attr("style", "");
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment