Skip to content

Instantly share code, notes, and snippets.

@franzenzenhofer
Created November 10, 2012 16:26
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save franzenzenhofer/4051578 to your computer and use it in GitHub Desktop.
Save franzenzenhofer/4051578 to your computer and use it in GitHub Desktop.
jquery plugin that replaces elements with absolute positioned clones of themselves, while hiding & silencing the originals
(function($){
isString = function (obj) {
return toString.call(obj) == '[object String]';
};
$.fn.bodysnatch = function() {
var collection = this;
return collection.each(function(a,b) {
var element = $(this);
var clone = element.clone();
w = element.width()
h = element.height()
if ( w && h)
{
clone.attr('style', window.getComputedStyle(element[0]).cssText);
clone.css({
position: 'absolute',
top: element.offset().top,
left: element.offset().left,
width: element.width(),
height: element.height(),
margin:0,
//padding: 0
});
}
else //probably images without a width and height yet
{
clone.css({
position: 'absolute',
top: element.offset().top,
left: element.offset().left,
margin:0,
//padding: 0
});
}
$('body').append(clone);
if(element[0].id) {
element[0].id=element[0].id+'_snatched';
}
element.addClass('snatched');
clone.addClass('bodysnatcher');
//stop audio and videos
element.css('visibility','hidden');
if(element[0].pause){
element[0].pause();
element[0].src='';
}
collection[a]=clone[0]
});
};
})(jQuery);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment