We help IT Professionals succeed at work.

Adding DOCTYPE breaks Javascript, and vertical alignment works in FF but not Safari

jwshome
jwshome asked
on
Medium Priority
702 Views
Last Modified: 2013-11-19
I'm creating a menu for a website that uses Javascript to animate the buttons (specifically, to make them expand or contract on mouse-over). The site requires that the visuals for the buttons overlap, so I've re-directed the onmouseover and onmouseout events from a series of blank <a href> elements, which will trigger the images' movement.

I've run into two problems:

First of all, I started programming in HTML, CSS, etc. very recently, and I didn't realize that I would want a !DOCTYPE for my code. When I added the DOCTYPE, my Javascript functions stopped working. I'm not very familiar with either W3C Standards or debugging, so can anyone help me find the error?

Second, when the Javascript functions DID work, the vertical alignment of the buttons inside the <li> didn't work in Safari (they only worked in Firefox). They remained horizontally-centered, but not vertically-centered. I'm trying to use methods that retain a clean block of HTML code (which is why I avoided nesting tables or DIVs).

Finally, this isn't a specific problem, but does anyone have any advice for cleaning up either my CSS, my HTML, or my JavaScript? I've been reading a ton of articles about coding discipline but it's hard to tell whether techniques are still useful or outdated.

Thanks very much!
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 
<html>
<head>
 
<script src="test menu.js" type="text/javascript"></script>
 
<style type="text/css">
 
/*** BEGIN CSS CLEAR ***/
 
html, body, div, span, applet, object, iframe, h1, h2, h3, h4, h5, h6, p, blockquote, pre, a, abbr, acronym, address, big, cite, code, del, dfn, em, font, img, ins, kbd, q, s, samp, small, strike, strong, sub, sup, tt, var, b, u, i, center, dl, dt, dd, ol, ul, li, fieldset, form, label, legend, table, caption, tbody, tfoot, thead, tr, th, td {
margin: 0;
padding: 0;
border: 0;
outline: 0;
font-size: 100%;
vertical-align: baseline;
background: transparent;
}
 
body {
line-height: 1;
}
 
ol, ul {
list-style: none;
}
 
/*** END CSS CLEAR ***/
 
#menu {
width: 170px; height: 670px;
margin: 10px auto;
position: relative;
}
 
#menu li {
width: 170px; height: 170px;
display: block;
position: absolute;
line-height: 170px;
text-align: center;
}
 
#menu a{
width: 125px; height: 125px;
top: 22.5px; left: 22.5px;
display: block;
position: absolute;
z-index:1;
}
 
#menu img {
width: 125px;
opacity: .9;
vertical-align: middle;
z-index: 0;
}
 
#home {top: 0px;}
#aboutus {top: 100px;}
#members {top: 200px;}
#media {top: 300px;}
#rush {top: 400px;}
#business {top: 500px;}
 
#home {background-color: blue;}
#aboutus {background-color: purple;}
#members {background-color: red;}
#media {background-color: orange;}
#rush {background-color: yellow;}
#business {background-color: green;}
 
</style>
</head>
 
<body>
 
<ul id="menu">
	<li id="home"><a href="#1" id="homeMA"></a><img id="b1" src="../textured buttons/home.png"/></li>
	<li id="aboutus"><a href="#1" id="aboutusMA"></a><img id="b2" src="../textured buttons/aboutus.png"/></li>
	<li id="members"><a href="#1" id="membersMA"></a><img id="b3" src="../textured buttons/members.png"/></li>
	<li id="media"><a href="#1" id="mediaMA"></a><img id="b4" src="../textured buttons/media.png"/></li>
	<li id="rush"><a href="#1" id="rushMA"></a><img id="b5" src="../textured buttons/rush.png"/></li>
	<li id="business"><a href="#1" id="businessMA"></a><img id="b6" src="../textured buttons/business.png"/></li>
</ul>
 
<script type="text/javascript">
document.getElementById('homeMA').onmouseover=widthChange1;
document.getElementById('homeMA').onmouseout=widthRestore1;
document.getElementById('aboutusMA').onmouseover=widthChange2;
document.getElementById('aboutusMA').onmouseout=widthRestore2;
document.getElementById('membersMA').onmouseover=widthChange3;
document.getElementById('membersMA').onmouseout=widthRestore3;
document.getElementById('mediaMA').onmouseover=widthChange4;
document.getElementById('mediaMA').onmouseout=widthRestore4;
document.getElementById('rushMA').onmouseover=widthChange5;
document.getElementById('rushMA').onmouseout=widthRestore5;
document.getElementById('businessMA').onmouseover=widthChange6;
document.getElementById('businessMA').onmouseout=widthRestore6;
</script>
 
</body>
</html>
 
JAVASCRIPT:
 
//Bouncing Button JavaScript Functions
//modified from instructions on Hesido.com
 
//********************
 
function doWidthChange(elem,start,end,steps,intervals,powr) { 
	if (elem.widthChangeInt) window.clearInterval(elem.widthChangeInt);
	var actStep = 0;
	elem.widthChangeInt = window.setInterval(
		function() { 
			elem.currentWidth = bounceInOut(start,end,steps,actStep,powr);
			elem.style.width = elem.currentWidth; 
			actStep++;
			if (actStep > steps) window.clearInterval(elem.widthChangeInt);
		} 
		,intervals)
}
 
//********************
 
function bounceInOut(minValue,maxValue,totalSteps,actualStep,powr) {
	// powr: how far the button 'bounces'; the closer to 0, the more it will bounce.
	var bounces = 1;
	var delta = maxValue - minValue;
	var increment = (1 / totalSteps) * actualStep;
	var stepp = minValue + ((1 / powr) * (Math.sin(increment * ((3.14159 * bounces) - Math.asin(powr))) * delta));
	return Math.ceil(stepp);
}
 
//********************
 
function widthChange1() { doWidthChange(document.getElementById('b1'),125,150,15,10,.6); }
function widthRestore1() { doWidthChange(document.getElementById('b1'),150,125,15,10,.6); }
function widthChange2() { doWidthChange(document.getElementById('b2'),125,150,15,10,.6); }
function widthRestore2() { doWidthChange(document.getElementById('b2'),150,125,15,10,.6); }
function widthChange3() { doWidthChange(document.getElementById('b3'),125,150,15,10,.6); }
function widthRestore3() { doWidthChange(document.getElementById('b3'),150,125,15,10,.6); }
function widthChange4() { doWidthChange(document.getElementById('b4'),125,150,15,10,.6); }
function widthRestore4() { doWidthChange(document.getElementById('b4'),150,125,15,10,.6); }
function widthChange5() { doWidthChange(document.getElementById('b5'),125,150,15,10,.6); }
function widthRestore5() { doWidthChange(document.getElementById('b5'),150,125,15,10,.6); }
function widthChange6() { doWidthChange(document.getElementById('b6'),125,150,15,10,.6); }
function widthRestore6() { doWidthChange(document.getElementById('b6'),150,125,15,10,.6); }

Open in new window

Comment
Watch Question

CERTIFIED EXPERT
Top Expert 2014

Commented:
Did that page ever work?

The first thing I see is that you have two Javascript functions that are not in <script></script>

Your doWidthChange and bounceInOut are not with in  <script></script> and I'm 99% sure they should be.

Author

Commented:
Yes -- it initially worked without the !DOCTYPE header in Firefox.

And sorry, I should have specified; everything below the line "JAVASCRIPT:" is in a separate file titled "test menu.js", which I load in <head>.
Consultant & Challenge Subduer
CERTIFIED EXPERT
Top Expert 2009
Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION

Author

Commented:
Kravimir:

Thanks for the help. I think I might have mis-typed some sort of CSS or Javascript declaration to compound the problem, but I reloaded an earlier version of my code and applied your suggestions, and it now works with the !DOCTYPE declaration. Thank you!

However, the vertical-centering problem still persists in Safari. Right now, I'm using a workaround using line-height: 170px; in the <li> formatting section, and a vertical-align: middle; in the img formatting section. This successfully aligns the images in the middle of <li> in Firefox, but not in Safari.

Any tips?
#menu li {
...
line-height: 170px;
text-align: center;
}
 
#menu img {
...
vertical-align: middle;
}

Open in new window

David S.Consultant & Challenge Subduer
CERTIFIED EXPERT
Top Expert 2009

Commented:
You're welcome.

Which version(s) of Safari are you testing in?  Safari 3.2/Win displays it the same as Firefox 3.x. The images I'm testing with are 100x100.

Author

Commented:
Safari 4.0 (I'm working on a Mac).
David S.Consultant & Challenge Subduer
CERTIFIED EXPERT
Top Expert 2009

Commented:
I tested it in Safari 4.0.1 on a Mac and it seems fine.  What dimensions are your images?

Author

Commented:
150x150, but they're resized down from 300x300.
David S.Consultant & Challenge Subduer
CERTIFIED EXPERT
Top Expert 2009

Commented:
Since they're square it shouldn't make a difference, since the "#menu img" rule scales them to 125x125.

Would you please post a screenshot of what it looks like in Safari 4?

Author

Commented:
Hi Kravimir,

I figured it out using a different solution. Thanks for your help!
David S.Consultant & Challenge Subduer
CERTIFIED EXPERT
Top Expert 2009

Commented:
You're welcome.

> I figured it out using a different solution.

Out of curiosity, what was your alternative solution?
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a sample view!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.