Tuesday, February 8th, 2011 | Adventures, Geek Out Lab, jQuery, Tips, Web Dev
Today I launched the new design of Escape From Cubicle Nation, which I’m very proud to have worked on. Props to Brian Morykon for the design, Joseph Hinson for the code.
My last order of business in coding the site was that some of the Youtube videos and embeds had been inserted at a width of 640 pixels, which was 90 pixels wider than the content area. With CSS, the options are really only to choose overflow: hidden;, which then only cuts off the content that is wider, instead of resizing it. So, I wrote a quick little script that some folks might find helpful. May I introduce jQuery resizer.
Here’s the actual function for my fellow geeks:
function resizer( selectors, w ) {
jQuery(selectors).each(function() {
var owidth = jQuery(this).width(); var oheight = jQuery(this).height();
if (owidth > w) {
var newH = (oheight * w / owidth);
jQuery(this).attr({
width: w,
height: newH
});
}; // endif
});
}
Essentially the function loops through every selector you give it, checks it against the width you give it, and if it’s wider, it will recalculate the attributes of width and height based on their original values to scale the image down, it takes two attributes, the selector, and the width. The function can be called like so:
resizer('.content iframe', 500);
Additionally, you can give it a comma separated list of selectors, like so:
resizer('.content iframe, .content embed, .content object, .content img', 500);
So, the very straightforward, bottom line usage is as follows:
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
<script type="text/javascript" charset="utf-8" src="appropriate-path/jq-resizer.js"></script>
<script type="text/javascript">
jQuery(document).ready(function() {
resizer('iframe, img', 550);
});
</script>
You can see a quick demo here.
February 13, 2011
Thomas Hall says:
Could a similar function be used to stretch video or photo embeds to get everything to line up nicely? Is this advisable? Would it slow things down for processing, or end up messing up your images beyond just the normal amount for stretching (i.e., in the way your monitor looks bad at non-native resolution)?
I like your blog
February 14, 2011
Joseph says:
Yeah, you could use this function for the same purpose. Videos will probably be ok, because generally they’re going to look ok, but images will start to get blurry. The quality will especially suffer with Internet Explorer.
June 10, 2011
Adam says:
Hey dude, I just used your Video Proportion Calculator, was super handy for a little project I was working on, thank you!
I was looking for some ads to click in return but couldn’t find any?
Anyway, good karma from me to you ;o)
June 13, 2011
Joseph says:
Thanks Adam. If you use HTML/CSS check out the CSS Extractor, I built those tools to scratch an itch, it’s satisfying when other people find them useful too.
June 18, 2011
Danielle says:
Perfect! Was struggling with ie7/8 on some jquery attr stuff but this works perfectly for what I needed. Thank you so much!
September 23, 2011
Ricardo says:
Hello, I have a video in streaming et want to resize but I cut the image, can you help me ?
November 4, 2011
InnovativePhp says:
Thanks for sharing. I’ll try it out an express more comments
November 19, 2011
Adam says:
Sounds cool. Is this a jQuery plugin, or does it go in the WordPress functions.php file?