Created
November 2, 2011 21:37
-
-
Save designfrontier/1335003 to your computer and use it in GitHub Desktop.
Pin me... an awesome jQuery plugin
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
//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); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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:
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.