<style></style> since 2000
One more step closer to being out of beta; and a new site to boot. Check it out at http://ui.jquery.com
Over at GodBit, someone needed to show a div when a heading was clicked, but if the user didn’t have javascript, the heading was a link to a detail page. This sounded like an interesting idea, so I poked around and here is what I ended up with:
$(document).ready(function(){
$("h3 a").parent().next("div").hide();
$("h3 a").click(function() {
$("h3 a").parent().next("div").slideToggle();
return false;
})
});
The key here is return false;—that stops the click from triggering the link.
min-width works in Firefox and Safari, and even IE 7, but not IE 6 -- big surprise. However, we can fake it fairly well. With a little jQuery fun, we can read the min-width value, and then implement it using javascript fairly nicely.
First off, we know we have to attach this to the resize of the browser. IE seems to execute the resize event many, many times during an actual window resize1 -- almost as if it were trying to execute it once every pixel resized (just a guess).
Enter the wresize jQuery plugin. This plugin tries to catch when the resize starts, and more importantly, when it finishes, and then executes a function. The one downside to this plugin is that we cant execute a function that requires us to pass any variables to it. Luckily, we don't need to for this.
Here are the things we need to do:
min-width.min-width on the first div inside the body element.min-width when the browser window widens. Simple, right? Here's the actual min-width function:
function minwidth() {
if ( (jQuery.browser.msie) && (jQuery.browser.version==6) ) {
var cssprop = jQuery("body div").css("min-width");
if ( cssprop == null ) {
cssprop = 760; //No min-width; default to min-width of 760
} else {
cssprop = parseInt(cssprop); //Convert value to numeric;
};
//get document margin to figure browser width to look for
var margin = parseInt( jQuery("body").css("margin-left") ) + parseInt( jQuery("body").css("margin-right") );
//21px is the width of the scroll bar.
if (clientwidth() < (cssprop + margin + 21)) {
jQuery(document.body).width(cssprop); //Set fixed-width
} else {
jQuery(document.body).width("auto"); //Un-set fixed-width
};
};
};
Now that's just the function; we haven't used it yet. We need to execute this function on page load, and use the wresize plugin to execute the same function on resize. Luckily, that's simple too.
$(document).ready(function(){
minwidth(); //call IE minwidth on page load
$( window ).wresize( minwidth ); //IE Resize event - resize for min-width
});
We didn't cover the clientwidth() function -- that's how we're getting the browser's window size. Check out the example page for the final result.
1http://snook.ca/archives/javascript/ie6_fires_onresize/
Well I know everyone was on the edge of their seats, however, all can relax: the bug has been fixed.
All I did was create a way to manage what ‘view state’ I was in, rather then doing a if/then of the current class name. My only made-up explanation I can come up with for why this works is that on page load, jQuery builds a giant array of your page elements and properties. When I start swapping class names around, that array isn’t being updated. Therefor, I just needed to come up with a method for tracking the state and query that rather then the current class name. Now all is well.
See it in action, or, you can download the .NET project and see it in action. I don’t care for .NET, but it’s what I had at the time—that and I don’t know php (yet).
Howdy. I'm Richard W. Baker - a designer based in Jacksonville, Florida with a passion for information architecture and usable interfaces. At my day job, I work for the City of Jacksonville as a User Interface Designer.
If you still have questions, or you're interested in hiring me for your next project, .
coming soon
coming soon