IE and CSS

Some old school CSS hacks to reminds you of the good old days…

#selector {
	color: black;
	color: yellow\9; /* IE 8 and up */
	*color: blue; /* IE 7 and up */
	_color: red; /* IE 6 and up */
}

Just a friendly note that if you use any of these IE hack, it will also work on IE9 as well. I suggest using IE conditions instead for the html or body element instead.





Then in your css template, you can specify which IE to target.

#selector {color: black;}
.ie6 #selector {color: red;} /* IE 6 Only */
.ie7 #selector {color: blue;} /* IE 7 Only */
.ie8 #selector {color: yellow;} /* IE 8 Only */

Declaring a javascript function

So over the week, someone asked me what’s the best way to declare a function in javascript. At first I thought it was funny and kind of stupid, but after thinking about it for a while I realized there are few ways you can declare a function.

One

1
2
function fnName(var, var2, var3){/* do something */}
fnName(); // execute the function

Two

1
2
var fnName = function(var, var2, var3){/* do something */}
fnName(); // execute the function

Three

1
(function(var, var2, var3){/* do something */});

Backtrack: Refresh on PHP Variables

Backtrack is a series of posts/articles about re-learning or refreshing on stuff that I’ve learned in the past. Good for beginners or those that just want to read it over and reminisce the good old beginning days.

PHP Variables
Variable is a symbolic representation of a value, it changes overtime. Its names start with a dollar sign ($). It is followed by letter or underscore (_) and can contain letters, numbers, underscores, or dashes. It can’t have spaces and it is case-sensitive.

Variable Names Examples
The comments below are my own opinions because I’d have bad experience with them.

1
2
3
4
5
6
7
8
9
<?php 
	$var
	$Var
	$_var // php uses this itself to define certain variables - so do not use
	$__var // double underscores or more - hate these and never use it
	$myVar
	$my_var
	$my-var // hate these as well - never use it
?>

Using Variables

1
2
3
4
5
<?php
	$var = 5; // set value
	$Var = 10;
	echo $var; // this returns the $var only since it is case sensitive.
?>

Changing Variable

1
2
3
4
5
6
<?php
	$var = 5; // set value
	echo $var; // this returns the value of 5.
	$var = 10; // set new value
	echo $var; // this will return the value of 10
?>

That’s it.

jQuery: Simple Accordion

Wrote this a long time ago but never got the chance to post it. Just a simple little accordion plugin I wrote. Must be work related or something I did out of boredom. I believe it is fully functional. Have not had a chance to test it yet.

vsnAccordion Plugin

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
(function($){ 
$.fn.vsnAccordion = function(settings){
	var s = $(this);
	var n = {
		item: "li",
		target: "a",
		onEvent: "click", // click, mouseover, hover
		holder: "div",
		speed: 500,
		closeOnEvent: true, // true or false
	}
	settings = $.extend(n,settings);
	return this.each(function(){
		var sItem = s.children(n.item);
		var sHolder = sItem.children(n.holder);
 
		sHolder.hide();
		sItem.first().addClass("selected");
		sItem.first().children(n.holder).slideDown(n.speed);
		sItem.children(n.target)[onEvent](function(){
			if( n.closeOnEvent ){
				sItem.removeClass("selected");
				$(this).parent("n.item").addClass("selected");
				sHolder.slideUp(n.speed);
				$(this).siblings(n.holder).slideDown(n.speed);			
			}
			else {
				var selected = $(this).parent(n.item).hasClass("selected");
				if(selected){
					$(this).parent(n.item).removeClass("selected");
					$(this).siblings(n.holder).slideUp(n.speed);
				}
				else {
					$(this).parent(n.item).addClass("selected");
					$(this).siblings(n.holder).slideDown();
				}
			}
			return false;
		}
	});
}
})(jQuery);

Minified Version

1
(function($){$.fn.vsnAccordion = function(settings){var s = $(this);var n = {item: "li",target: "a",onEvent: "click",holder: "div",speed: 500,closeOnEvent: true,}settings = $.extend(n,settings);return this.each(function(){var sItem = s.children(n.item);var sHolder = sItem.children(n.holder);sHolder.hide();sItem.first().addClass("selected");sItem.first().children(n.holder).slideDown(n.speed);sItem.children(n.target)[onEvent](function(){if( n.closeOnEvent ){sItem.removeClass("selected");$(this).parent("n.item").addClass("selected");sHolder.slideUp(n.speed);$(this).siblings(n.holder).slideDown(n.speed);}else {var selected = $(this).parent(n.item).hasClass("selected");if(selected){$(this).parent(n.item).removeClass("selected");$(this).siblings(n.holder).slideUp(n.speed);}else {$(this).parent(n.item).addClass("selected");$(this).siblings(n.holder).slideDown();}}return false;}});}})(jQuery);

HTML

1
2
3
4
5
6
7
8
9
10
<ul id="vsnAccordion">
	<li>
		<a href="#">vsnAccordion Item 1</a>
		<div><p>vsnAccordion Item text 1....</p></div>
	</li>
	<li>
		<a href="#">vsnAccordion Item 2</a>
		<div><p>vsnAccordion Item text 2....</p></div>
	</li>
<ul>

To Use

1
2
3
4
5
$(document).ready(function(){
	$("#vsnAccordion").vsnAccordion();
	// to change the item
	$("#vsnAccordion").vsnAccordion({item: "div",speed: 1000});
})

Updated: December 23, 2011
- fixed javascript

Updated: December 19, 2011
- Fixed “onEvent”

Updated: December 18, 2011
Fixed and updated the javascript.

Updated: December 17, 2011
- the default of this accordion is if you click this, it will close that: clickOnEvent: true
- added: click this to open this, click this to close (if its open): clickOnEvent: false

jQuery: Smooth Scroll Up Top

So just for fun today at work, I wrote a really simple smooth scroll up top. I remembering seeing it somewhere but now I can’t remember. So basically, the script can be described as something along this line. If you scroll down and reached a certain number of pixels or height, the “top” button will fadeIn (appear) else fadeOut (disappear). Below is the code:

1
2
<!-- HTML -->
<span class="scroll-top"><a href="#">TOP</a></span>
1
2
/* CSS */
.scroll-top {display: none; position: fixed; bottom: 10px; left: 10px;}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
/*
	jQuery / Javascript
	Scroll To Top
*/
$(".scroll-top").hide();
$(window).scroll(function(){
	var yaxis = $(window).scrollTop();  // find your y-axis scrolling position
	if ( yaxis >= 200 ){
		$(".scroll-top").fadeIn();
		$(".scroll-top a").click(function(){
			$("html, body").animate({scrollTop: 0});
			return false;	
		});	
	}
	else {
		$(".scroll-top").fadeOut();
	}
});

That’s it. Give it a try. Sorry, no demo today.

jQuery: Dropdown Nav for Mobile Devices

While working on WPMimic, a bbPress theme that is now being converted over to WP and bbPress 2.0 (the plugin one). I made a simple pure css dropdown nav. The problem with the css dropdown nav is that it doesn’t work on mobile devices simply because touch screen doesn’t have a hover over function. To solve this problem, I believe the best way to approach this is/was through javascript (jQuery).

The idea is if I am browsing from a normal desktop browsers, to activate the drop down all I would need to do is hover my mouse over. But if I’m on the mobile devices like an iPad or an iPhone, then I’d need to click on the link to activate the drop down. Lets dive in deeper and say, if my desktop browser javascript is disabled, the drop down nav needs to be a pure css nav.

I know what you’re thinking, why don’t I just use the javascript to target the iPhone or iPad and leave the desktop browser as pure css. Well, I didn’t plan on using javascript but now that i’m doing it I might as well make it animate.

There is one draw back that I have yet to find a solution to yet. If you click on the parent link on these touch devices to activate the drop down, two things could happen. One, the browser will activate the click request and take you to the link that you clicked on and also activate the drop down nav as well. Two, activate the drop down nav but not the link. I’ll leave this problem open until I find a proper way to handle this. Alright, lets get to the code.

HTML
You can find the html and css in this post. I did make two small changes to the css and that is replacing.

1
2
3
4
5
6
// Find these two lines
#nav ul {display: none;}
#nav li:hover ul {display: block; position: absolute; top: 25px; left: 0; background: #fff; width: 175px;}
 
// Replace with this
#nav ul {display: none; position: absolute; top: 25px; left: 0; background: #fff; width: 175px;}

Javascript/jQuery

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
$(function (){
	// find the mobiles devices user-agent names
	var ipad = navigator.platform.indexOf("iPad") != -1;
	var iphone = navigator.platform.indexOf("iPhone") != -1;
	var ipod = navigator.platform.indexOf("iPod") != -1;
	var android = navigator.platform.indexOf("android")>=0;
 
	// if is mobile devices
	if (ipad || iphone || ipod || android) {
		$("#nav li").children("ul").hide();
		$("#nav li a").click(function (){		
			// if childen ul is hidden, click will show (slideDown)
			if( $(this).siblings("ul").is(":hidden")==true){
				$("#nav li").children("ul").slideUp(); // hiding all childen ul first
				$(this).siblings("ul").slideDown(); // activate this ul
			}
			else {
				$(this).siblings("ul").slideUp();
			}
			return false; // disable parent link
		});
	}
	else {
		$("#nav").children("li").hover(function (){
			$(this).children("ul").stop().slideDown();
		},function (){
			$("#nav").children("li").children("ul").slideUp();
		});
	}
});

Use CSS When Javascript is Disabled

1
2
3
4
5
<noscript>
	<style type="text/css">
		#nav li:hover ul {display: block;}
	</style>
</noscript>

I realized that there are a lot of mobiles devices out there, but I honestly don’t care for those devices other than the iOS and Android. Maybe I’ll add windows phone and webOS once I have the device.

DEMO

jQuery: Simple Tabs

This week at work, I was asked to create a simple tabs for a small section of the site. Usually, I would use jQuery UI plugin to get the job done. But we are starting to optimize the site to make it load faster so adding another javascript plugin to the site wasn’t a great idea. Enter this really simple click to show jquery scripts.

Revisit-09-18-11
So after making this, I found a couple of problems with this.
1. click on on the “selected” tab animate the content again.
2. if you have divs within the div id’s, the content would be hidden
3. you can’t bookmark a certain tab.

HTML

1
2
3
4
5
6
7
8
9
<ul id="tabs">
	<li><a href="#first">First</a></li>
	<li><a href="#second">Second</a></li>
</ul>
<div id="tabs-content">
	<!-- the "_" (underscore) is used for bookmarking -->
	<div id="first_">First tab content</div>
	<div id="second_">Second tab content</div>
</div>

Javascript

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
$('#tabs-content').children("div").hide(); // hide child div only.
$('#tabs-content').children("div:first").show(); // show the first child div
$('#tabs li:first').addClass("selected"); // add an "selected" class to the 1st li
$('#tabs li a').click( function() {
	// if li doesn't have class "selected"
	if ( !$(this).parent("li").hasClass("selected") ){
		$('#tab li').removeClass("selected");  // remove all "current" class from li
		$(this).parent("li").addClass("selected");  // find the click li and add "current" class
		$('#tab-content').children("div").hide(); // hide all child divs again
		var tabContent = $('this').attr("href");
		$(tabContent+"_").show();
		return false; // disable this to enable tab bookmarking
	}
	else { return false; }
});

CSS: Drop Down Nav

I’ve been wanting to write this for a long time.  Just never had the chance or the time to do, or maybe its because I’m just too lazy.  I know there are many css articles teaching you how to make a css drop down menu.  You’ll find this to be similar to others as I don’t really see a different way around it.  With that, lets dig right into it.

HTML: the nav structure is very simple and familiar. I’m sure you’ve seen this at least once before…

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<ul id="nav" class="navigation">
	<li><a href="#">Parent</a>
		<ul>
			<li><a href="#">Childen</a></li>
			<li><a href="#">Childen</a></li>
			<li><a href="#">Childen</a></li>
			<li><a href="#">Childen</a></li>
		</ul>
	</li>
	<li><a href="#">Parent</a>
		<ul>
			<li><a href="#">Childen</a></li>
			<li><a href="#">Childen</a></li>
			<li><a href="#">Childen</a></li>
			<li><a href="#">Childen</a></li>
		</ul>
	</li>
</ul>

CSS

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
body	{background: #ccc;}
ul	{margin: 0; padding: 0; border: none; outline: none; list-style: none;}
 
/* Parent Nav */
#nav {display: block; height: 25px; background: #333; padding: 5px 10px 0 10px;}
#nav li {float: left; position: relative;}
#nav li:hover {background: #fff;}
#nav li a {display: block; padding: 5px 10px; color: #fff; text-decoration: none;}
#nav li:hover a,
#nav li a:hover	{color: #000;}
 
/* Hide &amp; Show Child UL */
#nav ul {display: none; position: absolute; top: 25px; left: 0; background: #fff; width: 175px;}
#nav li:hover ul {display: block; }
 
/* Childen Nav */
#nav ul li {display: block; float: none;}
#nav ul li a {display: block; padding: 5px 10px;}

That’s all there is to it. See in action: DEMO