Multi Column Lists Revisited

A couple of weeks ago I talked about creating multi column lists using a simple jQuery plugin I had written.

Browsing some of my favourite blogs today, I stumbled upon this article by Trent Walton. The techniques shown in the article could be used to reproduce the exact same effect using pure CSS, as Borat would say “Very Nice!..”.

Just to recap the basic technique:

ul {
	-webkit-column-count: 3;
	-webkit-column-gap: 20px;
	-moz-column-count: 3;
	-moz-column-gap: 20px;
	-o-column-count: 3;
	-o-column-gap: 20px;
	column-count: 3;
	column-gap: 20px;
}

Would produce 4 equal columns with a 20px gutter (see Trent’s article for more detail).

Creating the same effect using my jQuery plugin:

$(document).ready(function() {
	$('ul').multilists({ cols: 3 });
});

Obviously, using pure CSS is the preferred option, but it’s good to have a fallback for users whose browsers don’t support the property. This is possible using the fantastic Modernizr library. Here is a basic implementation (un-tested, sorry!):

The CSS:

ul li {
	float: left;
	margin-right: 20px;
}

.csscolumns ul {
	margin-right: 0; /* margin removed in favour of column-gap */
	-webkit-column-count: 3;
	-webkit-column-gap: 20px;
	-moz-column-count: 3;
	-moz-column-gap: 20px;
	-o-column-count: 3;
	-o-column-gap: 20px;
	column-count: 3;
	column-gap: 20px;
}

See more about the .csscolumns class here.

The JavaScript:

$(document).ready(function() {
	if (!Modernizr.csscolumns) {
		$('ul').multilists({ cols: 3 });
	}
});

Obviously, this assumes you have jQuery, Modernizr & my MultiLists plugin referenced in your code.

National Capitol Columns by cliff1066

The above basically tells the browser to render the list using the CSS property if it is supported, and if not, use the plugin as a fallback.

Please leave a comment if you have any thoughts or comments, or, if you’ve noticed an obvious mistake and feel like putting me right! :$

Posted in CSS, PHP, jQuery | 2 Comments

Give your users a ‘big target’ with jQuery

This is a little bit of code I’ve found myself re-writing quite a bit. I though it was time I documented this just for reference.

What do you mean big target?

Okay, lets take the following common layout trend as an example (excuse the half-ass Photoshop mock-up):

Big Target Example

In this scenario, if the user wants to find out more about ‘Products & Services’ they need to click the ‘Find out more’ button (which is pretty straightforward, right?). The idea of big target is to make this process even simpler by making the entire block clickable (in our case #mydiv).

To do this we can using the following jQuery snippet:

$(document).ready(function() {
	$('#mydiv').css('cursor', 'pointer').click(function() {
		window.location=$('a', this).attr('href');
	});
});

This code simply opens the url of the child a tag when #mydiv is clicked. We are also adding cursor: pointer; using the jQuery .css() function, this is so that if the user has JavaScript turned off, they are not misled to think the area is clickable.

This technique does have some limitations, for example if there is more than one a tag inside #mydiv then jQuery will always take the first. This could easily be fixed by using a :first or :last pseudo selector.

Going a bit further…

Okay, so say I want to re-use this in different ways across my site, how can I adapt it to make it more functional? By adding a class of target to #mydiv, and trigger to the a tag, we can now use the following:

$(document).ready(function() {
	$('.target').css('cursor', 'pointer').click(function() {
		window.location=$('a.trigger', this).attr('href');
	});
});

This means that anything in our document with class="target" will point to whatever the a tag with class="trigger" links to.

Going further still!…

If you’re looking to take this to the next level, Leevi Graham has written a great big target plugin with loads of extra features/functions.

If you have any comments, suggestions or examples you’d like to share then please leave a comment!

Posted in CSS, Coding, Quick Tips, jQuery | Leave a comment

Short URLs with Google Apps

Whether short URLs are a good idea or not is still up for debate. But I if you’re going to rely on a 3rd-party service to host short URLs, Google is probably a safe bet…

So, with that in mind, here’s how you can host your own short URLs using Google…

  1. Sign up for a Google Apps account if you haven’t already (why?!):
    http://www.google.com/a/
  2. When logged in to your account, add the Google Short Links service to your account.
  3. Once you’ve completed the setup process, you’ll get the Short Links option on your dashboard:
  4. You can now start adding short URLs using the simple control panel (located at the root of your short URL path, e.g. http://short.mydomain.com):
  5. That’s it!

This is certainly not the best or the most fancy solution, but it’s simple and it works, and, chances are it’s not going anywhere anytime soon.

It’s also worth noting that there is an API available (with very limited documentation) if you want to get clever and create your URLs automatically.

Please leave a comment and let me know how you get on!

Posted in Quick Tips, Thinking | 1 Comment

UPDATED: WordPress and Google Analytics

Okay, so after just checking my Google Analytics account I’ve come to realise a lot of the ‘visitors’ to my site are, infact, me…

The usual way of combatting this relies on tracking your IP. This works fine until you access your site from work, or your ISP issues you with a new IP = lame.

As a better alternative, we can use some of the conditionals built into WordPress to check if who you are before showing the tracking code.

To do this, first we need to create our own add_tracking() function, and add it to our theme’s functions.php file. Below is the simple function I have created:

/**
 * Google Analytics tracking code conditional.
 * Checks if user is not an admin before adding tracking <script>.
 */
function add_tracking() {
	if (!current_user_can('level_10')) {
		return true;
	}
}

This simply returns TRUE if the user is NOT logged in as an admin.

Next wrap your Analytics tracking <script> tag in a conditional as below:

<?php if (add_tracking()): ?>
<script type="text/javascript">

  var _gaq = _gaq || [];
  _gaq.push(['_setAccount', 'UA-XXXXXXXX-X]);
  _gaq.push(['_trackPageview']);

  (function() {
    var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
    ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
    var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
  })();

</script>
<?php endif; ?>

All done :-) now your tracking code will only display when you’re not logged into the WordPress control panel – no matter what IP you use.

UPDATE:

Okay, so having tried this for a couple of weeks I’ve found that whilst the technique works fine, the admin sessions expire too quickly to make it effective. I have now updated my code to create a tracking variable if the condition is met. I then exclude this variable from my Google Analytics reports:

  1. Remove the add_tracking condition wrapping the Google Analytics tracking code.
  2. Add a new conditional to your <body> to add a JavaScript _setCustomVar() call:
    <body <?php body_class(); if (!add_tracking()) { echo ' onload="javascript: pageTracker._setCustomVar(1, \'is_admin\', \'true\', 1);"'; } ?>>
  3. Once you have this running you can add a custom filter to your GA reports to exclude data collected when the ‘is_admin’ variable is set:
    Filter Type: Custom filter > Exclude
    Filter Field: User Defined
    Filter Pattern: is_admin
    Case Sensitive: No

…P.S. Please leave a comment if you know a better way of doing any of this!

Posted in PHP, WordPress | Tagged , | Leave a comment

UPDATED: Multi Column Lists meet jQuery

Recently, I’ve been working on a project that features a three column ordered list in its main content area. Previously I would have probably built this as three separate <ol> tags floated left.

This solution is far from ideal as it throws up all sorts of issues: numbering, positioning, scaling etc. I wanted to be able to achieve the same effect using a single list. After a couple of minutes of Googling I found this on ALA. The solution in this article looks something like this:

<ol>
<li class="column1">Item #1</li>
<li class="column1">Item #2</li>
<li class="column1">Item #3</li>
<li class="column2 reset">Item #4</li>
<li class="column2">Item #5</li>
<li class="column2">Item #6</li>
<li class="column3 reset">Item #7</li>
<li class="column3">Item #8</li>
<li class="column3">Item #9</li>
</ol>

This is combined with some dodgy (sorry!) CSS to produce the desired outcome. Not only is this pretty ugly, it’s also unscalable: if I want to add or remove items from the list I have to completely rewrite the CSS – not so good.

Tidying up with some jQuery.

As a better alternative, I set about porting this same technique into a jQuery plugin. This way all the logic calculations (margins, widths etc.) can be calculated and applied automatically, allowing unlimited expansion.

The code, is mega simple, it just loops through the list applying negative margins based on how many columns you want. For example:

$(document).ready(function() {
$('#mylist').multilists({ cols: 3 });
});

This would split the list #mylist into three equal columns, leaving your HTML/CSS untouched.

It is also possible to specify a pixel width for each column:

$(document).ready(function() {
$('#mylist').multilists({ cols: 3, colWidth: 200 });
});

Checkout the demo!

Please feel free to try this out on your own projects. Just remember to leave a comment and let me know how you get on!

UPDATE:

I have now made a couple of fixes as well as adding a callback argument to the function, grab the updated code on github: View the source (1.0.2)

Posted in CSS, jQuery | 27 Comments

Why I can’t be bothered to build my site…

Okay, so having just binned off revision 103209 of my site (having coded it and built into ExpressionEngine beforehand), I have now decided to give up for now and just get something up and running (this).

I now hope by adopting a Rinse & Repeat strategy, I can finally nurture the site that I really want.

We’ll see…

'waschsalon' by Gastev

Posted in Thinking | Leave a comment

Hello world!

Welcome to WordPress. This is your first post. Edit or delete it, then start blogging!

Posted in Uncategorized | 1 Comment