Ph.Creative is a full service design and communications agency specialising in web design, SEO, internet marketing and branding.

Liverpool

London

Manchester

New York

Call us on +44(0)151 708 2280 or liverpool@ph-creative.com

Call us on +44(0)20 3301 4503 or london@ph-creative.com

Call us on +44(0)161 880 0122 or manchester@ph-creative.com

Call us on (001) 646 340 1025 or newyork@ph-creative.com

Custom Checkboxes with jQuery and CSS

by Craig Wilson 6 November 2009 at 11:17

A short and sweet piece of code, but very powerful in giving you total control over the design of your checkboxes.

Click here to view the demo.

The problem I set out to solve here was a combination of two things. The first was the ability to change the look of checkboxes in web forms. By default they cannot be altered using CSS as the style is determined by the user’s operating system. The second problem was that the existing JavaScript solutions to this problem were bloated with a lot of unnecessary code.

My solution solves a simple problem using very little code, so I hope it comes in useful! Let me walk you through the steps…

Step 1, add a block container around your checkbox.

<div class="container">
    <input type="checkbox" value="1" class="checkbox" />
</div>

That’s all the HTML you're going to need!

Step 2, style your checked and unchecked states as an image. (It’s a good idea to optimise these as a sprite instead of two separate images to save on HTTP requests and avoid a flicker when the images switch.)

Step 3, add your CSS.

The checkbox image should be added to the checkbox container rather than to the checkbox itself.

.container {
    width:20px;
    height:20px;
    background:url(checkbox.gif) no-repeat;
    cursor:pointer;
    overflow:hidden;
    position:relative; /* IE fix to hide overflow */
}

Add your ‘checked’ class, which should just be a change in the background position if you’ve used a sprite.

.checked {
    background-position:0px -20px !important;
}

Next, we want to position our checkbox so it appears outside of the container.

.checkbox {
    position:relative;
    left:30px;
}

If you have a look at your page now, you should see your custom checkbox and your default checkbox sitting pretty beside each other. Next, we’ll move on to the jQuery.

Step 4, add your jQuery.

The theory behind the idea is for the user to click on the block container and use jQuery to automatically check the checkbox that sits inside it. Then we use CSS to hide the default checkbox.

var $this = $(".container");
$(".container").toggle(function(){
    $this.addClass("checked");
    $(".checkbox").attr("checked","checked");
},function(){
    $this.removeClass("checked");
    $(".checkbox").removeAttr("checked");
});

The toggle function here will allow you to add your checked class as well as check the checkbox itself, and then remove the style and checked attribute on every 2nd click.

If you run this in your browser now and click on the custom checkbox, you should see that the default checkbox is being toggled automagically! If you are seeing this, then all that is left to do is hide your default checkbox using CSS. So in your container class, simply add the following:

overflow:hidden;
position:relative; /* IE fix to hide overflow */

So the whole container class should look like this:

.container {
    width:20px;
    height:20px;
    background:url(checkbox.gif) no-repeat;
    cursor:pointer;
    overflow:hidden;
    position:relative; /* IE fix to hide overflow */
}

I’ve tested this code in Firefox 3, IE6, IE7, IE8, Safari, Chrome and Opera all on Windows and it works fine in all browsers. Have a look at the demo and let me know what you think!

Currently rated 5.0 by 4 people

  • Currently 5/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Tags: , ,

css | development | jquery

jQuery Plugin: Scroll to Top v3

by Craig Wilson 29 October 2009 at 15:13

After some more great feedback I've decided to release version 3 of our jQuery Scroll to Top plugin. Smile

There have been 2 main updates to this one:

1. Following user comments from v2, I've added the additional option of choosing when the scroll to top link appears. So you can choose for it to appear only after the user has scrolled down further than, say, 700 pixels.

Implementation of the new feature is easy. First of all, if you're new to the plug in, follow the original instructions to help get set up. (Plus you might want to have a look at version 2 for information on the speed and easing options.) Then, you can simply add the new option as follows:

$("#toTop").scrollToTop({
   speed: "fast",
   ease:  "easeOutBounce",
   start: 700
});

Easy!

2. The plugin now includes licensing information, as the script is now released under the open souce MIT License! Good news for all, so remember to comment and let me know if you make any cool updates to the script.

Also, feel free to post a link to your website if you're using the plugin! We'd love to see it. Smile

scroll-to-top-v3.zip (25.74 kb)

Currently rated 4.8 by 5 people

  • Currently 4.8/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Tags: , ,

jquery | web design | Web Technologies

jQuery Plugin: Scroll to Top v2

by Craig Wilson 22 June 2009 at 17:25

Hi guys,

It's been a little overdue, but I've finally had some spare time to update our jQuery Scroll to Top plugin after some great feedback from our readers. Thanks. Smile

In the new version, I've included the options to choose your speed as well as support for the excellent jQuery Easing plugin (version 1.3 is included in the download pack, but please remember to check the jQuery Easing homepage for the latest version!)

If you're new to the plug-in, please look at the original post for set up instructions.

For those upgrading, grab the latest version at the end of this post, which contains an example set up.

You can extend the options by using the following:

$("#toTop").scrollToTop({
   speed: "fast",
   ease:  "easeOutBounce"
});

You can see the full list of easing options at the jQuery Easing homepage. For speed, you can use the usual "slow", "normal", "fast" or millisecond timing. Of course, you can use one or the other, or none at all.

Enjoy!

Version 2.0: scroll-to-top-v2.zip (25.67 kb)

Update: Version 3 now released!

Currently rated 5.0 by 6 people

  • Currently 5/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Tags: ,

jquery | web design | Web Technologies

JQuery - CSS Style Switch - Mouse Detection

by Jim Taylor 15 May 2009 at 12:09

Our latest projectEco Environments’ recently went live, which we’re really proud of. So much so, we thought we’d brag about it a little!

We decided to incorporate an energy saving concept by urging site users to turn off the light before they leave.

We added mouse detection to the site in the flash, so when the mouse cursor went above the top navigation towards the top of the browser, a warning sign appears.

For the actual light switch we used jQuery. Clicking the light switch switches the CSS Style Sheets, giving the impression the lights have been switched off. Highlighting only the data capture form for their latest email campaign.

You’ll also noticed the flash at the top also changes from day to night, with house lights flickering on and the sun changing to a moon.



Of course you can always switch the lights back on... if you’re afraid of the dark!

Currently rated 5.0 by 7 people

  • Currently 5/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Tags: , ,

css | development | jquery | web design

jQuery Trick: Hidden Links

by Craig Wilson 30 March 2009 at 10:31

Sorry, I know the name sounds like a poor attempt at some black hat SEO, but that’s not the case (I just can’t think of a better name for it.) Remember my hidden message CSS trick? This is a jQuery trick in the same theme. With this, you can hide links within links, so by default they go to any normal page, but contain a hidden link on double click – taking your in the know visitors to a secret page.

It’s all very easy to set up. First, create your normal link.

<a href="http://www.google.com">Google.com</a>
Then we’re going to give it an ID for our jQuery code.
<a id="hiddenLink" href="http://www.google.com">Google.com</a>
We’re going to use jQuery’s click and dblclick events for this. So first we’ll go ahead and set up the single click event first.
$("a#hiddenLink").click(function(){
  var timeout = setTimeout(function() {
    location.href=$("a#hiddenLink").attr("href");
  }, 500);
  return false;
});
As you can see, we have to set up a single click event to delay your browser from immediately visiting the href link. We’re only delaying it by half a second, not enough time to notice a delay but just enough time to get that double click in.

So now we’ll set up the double click event, which will contain the URL to your secret page.

$("a#hiddenLink").dblclick(function(){
  location.href="http://wikipedia.org";
});
And that’s it! Click here for a demo (or double click for some fun.)

Currently rated 4.4 by 7 people

  • Currently 4.428571/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Tags: , ,

web design | Web Technologies | jquery


 

Search

Recent Comments

Comment RSS

Our Latest Tweets

Office Music

Don't judge us by our tastes!

© Copyright Ph.Creative 2009. All rights reserved. Terms & Conditions | Privacy Policy | Sitemap (XML) | Log in

Website design by Ph.Creative

^