Solved

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

Posted on 2009-07-10
11
630 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

0
Comment
Question by:jwshome
  • 5
  • 5
11 Comments
 
LVL 57

Expert Comment

by:giltjr
Comment Utility
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.

0
 

Author Comment

by:jwshome
Comment Utility
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>.
0
 
LVL 42

Accepted Solution

by:
David S. earned 250 total points
Comment Utility
Units are required on most CSS properties. So change

elem.style.width = elem.currentWidth;

to

elem.style.width = elem.currentWidth+'px';


I see that the CSS Reset rule that your using sets the vertical-align:baseline on table-cells. I've seen bugs caused by that.

Another thing that I see is that you named each event handler function you're using. Normally I would just use an anonymous function like this:
document.getElementById('homeMA').onmouseover=function(){

  doWidthChange(document.getElementById('b1'),125,150,15,10,.6);

};

Open in new window

0
 

Author Comment

by:jwshome
Comment Utility
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

0
 
LVL 42

Expert Comment

by:David S.
Comment Utility
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.
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

Author Comment

by:jwshome
Comment Utility
Safari 4.0 (I'm working on a Mac).
0
 
LVL 42

Expert Comment

by:David S.
Comment Utility
I tested it in Safari 4.0.1 on a Mac and it seems fine.  What dimensions are your images?
0
 

Author Comment

by:jwshome
Comment Utility
150x150, but they're resized down from 300x300.
0
 
LVL 42

Expert Comment

by:David S.
Comment Utility
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?
0
 

Author Comment

by:jwshome
Comment Utility
Hi Kravimir,

I figured it out using a different solution. Thanks for your help!
0
 
LVL 42

Expert Comment

by:David S.
Comment Utility
You're welcome.

> I figured it out using a different solution.

Out of curiosity, what was your alternative solution?
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
This article discusses four methods for overlaying images in a container on a web page
This tutorial demonstrates how to identify and create boundary or building outlines in Google Maps. In this example, I outline the boundaries of an enclosed skatepark within a community park.  Login to your Google Account, then  Google for "Google M…
This tutorial walks through the best practices in adding a local business to Google Maps including how to properly search for duplicates, marker placement, and inputing business details. Login to your Google Account, then search for "Google Mapmaker…

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now