View Single Post
Old 01-30-2013, 11:48 AM   PM User | #1
doughavant
New to the CF scene

 
Join Date: Jan 2013
Posts: 2
Thanks: 1
Thanked 0 Times in 0 Posts
doughavant is an unknown quantity at this point
Efficient way of using if and else.

Hi there! I'm pretty new to Javascript and I was wondering if there was a better method to handle the situation. I'm making a website that uses Javascript to hide and show content via DIV boxes.

It's working great but the only issue is that I think there might be a better way to make it more efficient as the more 'pages' / 'DIV boxes' involved, the longer the code. Here's the current code I have:

Code:
function myself() {
	if($('#myself').css('display') == 'block') {
		$('#myself').hide();
		$('#banner').fadeIn();
	}
	else if($('#about').css('display') == 'block') {
		$('#about').hide();
		$('#myself').fadeIn();
	}
	else if($('#animation').css('display') == 'block') {
		$('#animation').hide();
		$('#myself').fadeIn();
	}
	else {
		$('#banner').hide();
		$('#myself').fadeIn();
	}
}

function about() {
	if($('#about').css('display') == 'block') {
		$('#about').hide();
		$('#banner').fadeIn();
	}
	else if($('#myself').css('display') == 'block') {
		$('#myself').hide();
		$('#about').fadeIn();
	}
	else if($('#animation').css('display') == 'block') {
		$('#animation').hide();
		$('#about').fadeIn();
	}
	else {
		$('#banner').hide();
		$('#about').fadeIn();
	}
	
}

function animation() {
	if($('#animation').css('display') == 'block') {
		$('#animation').hide();
		$('#banner').fadeIn();
	}
	else if($('#about').css('display') == 'block') {
		$('#about').hide();
		$('#animation').fadeIn();
	}
		else if($('#myself').css('display') == 'block') {
		$('#myself').hide();
		$('#animation').fadeIn();
	}
	else {
		$('#banner').hide();
		$('#animation').fadeIn();
	}
doughavant is offline   Reply With Quote