Skip to content

Instantly share code, notes, and snippets.

@designfrontier
Created November 2, 2011 21:37
Show Gist options
  • Save designfrontier/1335003 to your computer and use it in GitHub Desktop.
Save designfrontier/1335003 to your computer and use it in GitHub Desktop.
Pin me... an awesome jQuery plugin
//pins elements in place with JS
(function($){
$.pinme = function(el){
var base = this;
// Access to jQuery and DOM versions of element
base.$el = $(el);
base.el = el;
// Add a reverse reference to the DOM object
base.$el.data("pinme", base);
base.init = function(){
};
// Run initializer
base.init();
};
$.pinme.defaultOptions = {
};
$.fn.pinme = function(){
return this.each(function(){
(new $.pinme(this));
if($(this).height() < $(window).height()){
var placeholder = $(this).clone();
$(placeholder).html(' ').css({'height': $(this).height(), 'width': $(this).width()}).addClass('pinme-placeholder');
//pin it in place
$(this).css({
'position' : 'fixed',
'top' : $(this).offset().top,
'left' : $(this).offset().left
});
//add the placeholder
$(this).after($(placeholder));
}
});
};
})(jQuery);
@designfrontier
Copy link
Author

Note that this needs to go inside the $(window).load() and not the $(document).ready(). The world ends if you don't do it that way*. Seriously.

So basic example would be:

$(window).load(function(){
    $('.pinme').pinme();
});

Full repo coming soon...

*Okay... so the world doesn't explode 2012 style. But, your positioning will most likely be wrong. .load() doesn't fire until the page has finished loading/rendering while .ready() fires as soon as the document's DOM is complete.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment