SpyreStudios

Web Design and Development Magazine

  • Design
  • Showcase
  • Inspirational
  • Tutorials
  • CSS
  • Resources
  • Tools
  • UX
  • More
    • Mobile
    • Usability
    • HTML5
    • Business
    • Freebies
    • Giveaway
    • About SpyreStudios
    • Advertise On SpyreStudios
    • Get In Touch With Us

How to Style iOS Sliding Checkboxes with jQuery

December 9, 2011 by Jake Rocheleau

With so much hype surrounding the new iPhone 4S release we’ve seen a lot more iOS app development hitting the mainstream. As such their techniques in design and user interfaces are sure to trickle out among the mass of web designers. In this case I’d like to spend some time examining the iOS slider switches common since early iOS 4.

This tutorial is a straightforward walkthrough about how to implement and work with these inputs. Through jQuery we can dynamically create a hidden input field which holds the checked/unchecked position of the slider bar. In my demo we’ve created a mockup settings page which mirrors the current switch value openly in the form.

featured image - iOS sliding check boxes demo screenshot

Live Demo – Download

Jumping into Core HTML

To again start us off let’s focus on the basic HTML5 template. Each new project I start follows this same template, as it provides crucial aspects into each design as a boilerplate. I would also like to point out this iOS toggle switch repo found on Github which we’ll be working with. The developer has crafted a beautiful set of icons we can utilize with jQuery.

But to start off let’s include the standard jQuery library from Google CDN and a new style.css file. Also I’ve kept the beautiful font Alice included from the Google Fonts API CDN. Check out my code example below:

[html wraplines=”false”]
<!DOCTYPE html>
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>iOS-Style Sliding Checkboxes with jQuery</title>
<link rel="stylesheet" type="text/css" href="style.css" />
<link href=’http://fonts.googleapis.com/css?family=Questrial|Alice’ rel=’stylesheet’ type=’text/css’ />
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js" type="text/javascript"></script>
<script type="text/javascript" src="jquery.ios-toggle.js"></script>
</head>

<body>
</body>
</html>
[/html]

Initially the style.css is blank, but we can match many of the default styles from this tutorial written by the developer dzy0451 on Github. The fonts for Questrial and Alice have been included to keep consistent styles within his example and ours. I personally love the Alice font, it has a Times New Roman look with more block-formatted letters.

You may notice an additional jquery.ios-toggle.js which is included in the Github code. This file holds a lot of the complicated functions and math calculations required to change the sliding images. We won’t need to edit this part, although you can go through and change some of the settings if you can pick apart the script. Such features include animation style, speed, and delays which can all be implemented in jQuery.

HTML – Main Body Area

Inside our body element I’ve wrapped all the content inside a core div with the ID #w. This saves code in our stylesheet and makes targeting properties a lot simpler. Inside are two distinct sections – HTML and jQuery functions.

[html wraplines=”false”] <h1>iOS-Input Form Buttons</h1>
<span class="labelform">Remember Personal Settings</span>
<a href="javascript:void(0)" class="toggle-switch small on" id="remsettings" rel="remsettings"></a>
<small id="remsettingsmsg">Turned On</small>

<div class="cleardiv"></div>

<span class="labelform">Keep Log of History(30 days)</span>
<a href="javascript:void(0)" class="toggle-switch small off" id="sitehistory" rel="sitehistory"></a>
<small id="sitehistorymsg">Turned Off</small>

<div class="cleardiv"></div>

<span class="labelform">Recieve E-mail Notices</span>
<a href="javascript:void(0)" class="toggle-switch small off" id="emails" rel="emails"></a>
<small id="emailsmsg">Turned Off</small>
<div class="cleardiv"></div>
[/html]

Above is the form HTML code which you’ll find inside the main wrapper. Each of the anchor links are given 2 classes – toggle-switch and small. The .toggle-switch class tells jQuery to replace that element with the dynamic images used in the iostoggle script. You can include small or large styles depending on the type of switch you’re looking to use. Within a crowded settings page I find the compact toggle switch fits into the interface much easier.

You should also notice I’ve included 3 different small elements labeled with a different ID. Inside I added some text setting the default state of each button status, and the inner content will change as you toggle a switch. Normally you wouldn’t include this in your page but it provides an example of how you can pull a response into the server-side code and update the settings in your database.

Standard document.ready() Call

Now let’s focus on a bit of jQuery code outside the wrapper div. This calls out to a few of the functions in our custom jQuery ios-toggle script, but nothing absolutely confusing. It’s a fairly small code block to consider – however I’ve broken it into 2 parts for easier analysis.

[javascript wraplines=”false”] $(function(){
$(‘.toggle-switch’).toggleSwitch();
$(‘.toggle-switch’).change(function(){
if(this.id == "remsettings"){
var usersetup = $(‘#remsettings’).toggleSwitch(‘val’);
if(usersetup == "on") { $(‘#remsettingsmsg’).html(‘Turned On’); }
else {
$(‘#remsettingsmsg’).html(‘Turned Off’);
}
}
[/javascript]

If you look under the basic usage section in the original developer’s guide it mentions the need for a toggleSwitch(); function. This is opened once the document has finished loading all DOM elements, and then executes a mass find & replace for all toggle-switch classes. The change() event handler will be run whenever the user changes the value in one of these input sliders.

Then we go into a series of if/else statements to look for which one we’ve toggled. The first block checks if we’re editing the “remove settings” feature, and if so we want to update the message text underneath. By using the toggleSwitch(‘val’); we can pull out the current value and see if the user is changing to on or off.

[javascript wraplines=”false”] else if(this.id == "sitehistory"){
var usersetup = $(‘#sitehistory’).toggleSwitch(‘val’);
if(usersetup == "on") { $(‘#sitehistorymsg’).html(‘Turned On’); }
else {
$(‘#sitehistorymsg’).html(‘Turned Off’);
}
}
else if(this.id == "emails") {
var usersetup = $(‘#emails’).toggleSwitch(‘val’);
if(usersetup == "on") { $(‘#emailsmsg’).html(‘Turned On’); }
else {
$(‘#emailsmsg’).html(‘Turned Off’);
}
}

});

});
[/javascript]

The other two code blocks are merely replicas checking against site history and e-mail notification settings. You could setup a variable array to loop through these values instead. However the taxing cost on memory would add up over time. Yet this method is solid to implement anywhere in your web app where you feel checkboxes just don’t work right.

CSS with Images

One last talking point to bring up is centered around the image-based inputs. Both of the images have been included in the download .zip archive and found within the original Github repository. Since the developer is Chinese he’s included an English and Chinese character version of these sliders.

You are certainly encouraged to build your own graphics if they fit your design better. I find these compact switches are perfect since they don’t use any labeling or text, and they also don’t require much space on the page.

free Mac OS X Photoshop - iPad PSD

There also hasn’t been a whole lot of freebies in this department. Although I have found a few goodies on Dribbble. The designer Chris Brummel is fantastic on mobile and has offered his iOS5 styled toggle switch PSD free for download! The width/height sizes are vastly different in comparison, but you can find pixel-perfect retina display toggles in this free iPad GUI kit built for Photoshop CS4.

Any way you choose to implement the CSS toggles will come out great. The backing jQuery library and smaller ios-toggles script fit perfect with these images. And of course you can always estimate new image dimensions if you customize the default values.

Live Demo – Download

Conclusion

Be sure to mess around and see if you can fit these cool toggle switches somewhere in your layout. I have found that users are very receptive to UI elements they’re commonly familiar with. And the iOS mobile platform is by no means a stranger to the tech world. If you have similar implementations of this method or custom graphics please share with us in the discussion area below.

Filed Under: jQuery

Recent Posts

  • 31 Fresh Design Elements for Spring and Easter
  • 10 Templates for Music Concert Flyers
  • How to Build a Web Scraper Using Node.js
  • Best PHP Books, Courses and Tutorials in 2022
  • How to Get Your First Web Design Client

Archives

  • April 2022
  • March 2022
  • February 2022
  • January 2022
  • December 2021
  • November 2021
  • October 2021
  • September 2021
  • August 2021
  • July 2021
  • June 2021
  • May 2021
  • April 2021
  • March 2021
  • February 2021
  • January 2021
  • December 2020
  • November 2020
  • October 2020
  • September 2020
  • August 2020
  • July 2020
  • June 2020
  • May 2020
  • April 2020
  • March 2020
  • February 2020
  • January 2020
  • December 2019
  • November 2019
  • October 2019
  • September 2019
  • August 2019
  • July 2019
  • June 2019
  • May 2019
  • April 2019
  • March 2019
  • February 2019
  • January 2019
  • December 2018
  • November 2018
  • October 2018
  • September 2018
  • August 2018
  • July 2018
  • June 2018
  • May 2018
  • April 2018
  • March 2018
  • February 2018
  • January 2018
  • December 2017
  • November 2017
  • October 2017
  • September 2017
  • August 2017
  • July 2017
  • June 2017
  • May 2017
  • April 2017
  • March 2017
  • February 2017
  • January 2017
  • December 2016
  • November 2016
  • October 2016
  • September 2016
  • August 2016
  • July 2016
  • June 2016
  • May 2016
  • April 2016
  • March 2016
  • February 2016
  • January 2016
  • December 2015
  • November 2015
  • October 2015
  • September 2015
  • August 2015
  • July 2015
  • June 2015
  • May 2015
  • April 2015
  • March 2015
  • February 2015
  • January 2015
  • December 2014
  • November 2014
  • October 2014
  • September 2014
  • August 2014
  • July 2014
  • June 2014
  • May 2014
  • April 2014
  • March 2014
  • February 2014
  • January 2014
  • December 2013
  • November 2013
  • October 2013
  • September 2013
  • August 2013
  • July 2013
  • June 2013
  • May 2013
  • April 2013
  • March 2013
  • February 2013
  • January 2013
  • December 2012
  • November 2012
  • October 2012
  • September 2012
  • August 2012
  • July 2012
  • June 2012
  • May 2012
  • April 2012
  • March 2012
  • February 2012
  • January 2012
  • December 2011
  • November 2011
  • October 2011
  • September 2011
  • August 2011
  • July 2011
  • June 2011
  • May 2011
  • April 2011
  • March 2011
  • February 2011
  • January 2011
  • December 2010
  • November 2010
  • October 2010
  • September 2010
  • August 2010
  • July 2010
  • June 2010
  • May 2010
  • April 2010
  • March 2010
  • February 2010
  • January 2010
  • December 2009
  • November 2009
  • October 2009
  • September 2009
  • August 2009
  • July 2009
  • June 2009
  • May 2009
  • April 2009
  • March 2009
  • February 2009
  • January 2009
  • December 2008
  • November 2008
  • October 2008
  • May 2008
  • April 2008

Categories

  • Accessibility
  • Android
  • Apps
  • Art
  • Article
  • Blogging
  • Books
  • Bootstrap
  • Business
  • CSS
  • Design
  • Development
  • Ecommerce
  • Fireworks
  • Flash
  • Freebies
  • Freelance
  • General
  • Giveaway
  • Graphic Design
  • HTML5
  • Icons
  • Illustrator
  • InDesign
  • Infographics
  • Inspirational
  • Interview
  • Jobs
  • jQuery
  • Learning
  • Logos
  • Matrix
  • Minimalism
  • Mobile
  • Motion Graphics
  • Music
  • News
  • Photoshop
  • PHP
  • Promoted
  • Rails
  • Resources
  • Showcase
  • Tools
  • Tutorial
  • Twitter
  • Typography
  • Uncategorized
  • Usability
  • UX
  • Wallpapers
  • Wireframing
  • WordPress
  • Work

Meta

  • Log in
  • Entries feed
  • Comments feed
  • WordPress.org

SpyreStudios © 2022