Better flexible site layout with resize detection.

This is an update to a previous post located here: Flexible site layout with resize detection.

A while back I posted an article about a simple and quick javascript  that would add or remove styles to whatever elements you want based on window width. This is a great way to make a single site compatible with multiple display types, everything from a phone to a desktop with an unreasonable large screen if you’d like. That code used an if statement, and was rather ineffective, I have since rewritten it to be easier to deploy to new themes and came up with this.

Snipplr: http://snipplr.com/view/60562/flexible-site-layout-with-resize-detection-now-improved/

Short, more concise, and easier to implement all you do is create an object with a simple {width: ‘style name’} format and the script will do the rest. It’s then up to you do add all the appropriate styles to your CSS document. This can of course all be done with CSS and the @media tag, but I prefer this method because it simple to deploy and it’s switches are much faster. As always leave a comment with any question or problems and I’ll get back to you as soon as I can.

Flexible site layout with resize detection

As phone sand tablets become more popular it’s becoming more common to see sites that can dynamically handle them, as well as notmal users, without having to load a special “mobile version”. There are several way to create such fluid sites, but today I’ll be talking about one I made not to long ago.

WordPress twentlyeleven, the new theme with wordpress 3.2, is a fluid width site, as you shrink your window the relative sizes get smaller, eventually breaking down into a single column site for mobile devices. They do this with CSS3 @media conditions, and while this is a perfectly usable method I found it left a little to be desired. First it bloats your stylesheet with all it’s extra group and if your using something that deletes comments, like the CSS color changer I posted last week, those group will get removed and the site will no longer be fluid (in fact it will probably be broken to boot). The second this I dislike about the @media method is the lag. Whenever  it changed between sizes the browser pauses for a moment while it figures everything out. I know this is probably a minor concern to most people but it annoyed me to no end, I wanted quick, fluid changes. For those reasons I wrote a short little jQuery javascript to do the dynamic changing.

Snipplr: http://snipplr.com/view/59044/flexible-site-layout-with-resize-detection/

The script adds a style to every element in the selector variable, which is anything that jQuery would understand as a selector (in the example it’s several different divs separated by commas, but I’ve done it with various other selectors). The classes that are added, as well as the sizes they are added at, are controlled by the if statement. In the example, the sizes “tiny_screen”, “small_screen”, “medium_screen”, and “large_screen” where added if the window was under 320px, between 321px and 768px, between 769px and 1024px, and anything larget that 1024px respectively. Those sized are all easily changeable (reducible or expandable if you want more or less steps). Using an if statement to control the sizes isn’t the most efficient way sure, but if your under 5 size options it’s just as fact as any other method, possible even faster if your number of levels is low. That said, if anybody wants to use more and notices it being problematic let me know and I’ll remake it using another method.

Settings up default styles, for those people without javascript enabled, can be done in two ways. First is to use the cascade effect of stylesheets, so say if one of your elements is the #main tag, you can give the normal #main the styles you want, like width:800px;, then give #main.small_screen a different style, the one you want on a small screen, say width:400px. Because #main.small_screen is more detailed that #main, it’s rules will override #main’s rules and it’ll give you the smaller, updated width. This methods does have the disadvantage of making some thing more difficult to adjust in the style sheet later (because you have to hunt it down and look between them all). I suggest simply giving those items a default class. I figure most people with javascript turned off are using medium screens, so in that case my html code would include both an id of main and a class of medium_screen, the script will adjust that ass soon as the page loads if it’s bigger or smaller assuming it can run and if not they should be able to use the site just fine.

I’ve got an example of this right on the resizer example page  so you can see it in action. If you have any questions/comments/criticisms post them below, as well as if you use this in a project or improve upon it and I’ll update with that information included as well.

Simple MySQL to CSV

I needed to make a quick export earlier this week so that we could get records into another program. At first I though I’d need to find a php library to make a csv for me (to save me the time of making the script myself) but it terns out that the process couldn’t be easier. The csv format is very easy to create and really seems to follow several key rules. First, you put things in quotation marks. If it’s a number you don’t have to, but just to be safe I put everything in quotation mark (it was also easier coding wise). Second, if there is a quotation in a cell you must put another quotation before it. Finally, you must create a new line at the end of every new like (obviously), With That in mind I made this:

Snipplr: http://snipplr.com/view/58408/simple-mysql-to-csv/

The code is pretty self explanatory, just doest what you need to as mentioned in the first paragraph. I tested it (admittedly in a limited fashion) and it works great. I don’t think there could be an potential problems but as this was created in less than 5 minutes as really just a quick test I’m not sure. If anybody find anything that might cause issues let me know and I’ll expand upon it but as of now it does exactly what I created it for.