PDA

Click to See Complete Forum and Search --> : Drop-down dilema - To Flash... or not to Flash?


Daves_shed
08-13-2006, 06:06 PM
My problem is a drop down menu.. I can't decide what to use to make it.

The thing is, I don't want it to be a bog standard one because I really do hate them. Below I have provided a quick impression of what it may look like when 'dropped down'. I want 'info' and 'related sites' to drop down from 'company'.

How it may look (http://www.dlthomas.co.uk/dropdown.jpg)

I can use Flash, but want something less clumsey. I was thinking CSS.... I could probably figure it out from tutorials if it was complicated, but I don't know if this would even be possible in CSS.

Would it be sensible to use CSS layers for it? Then put the two drop-down tabs in layers. I could then manage them with java behaviers.

I'd be very greatfull for any help :):confused:

Daves_shed
08-13-2006, 10:42 PM
Okay I've used CSS layers with java behaviers on this, but it doesn't really work as well as I'd like.

Basically when you click 'company' it shows 2 hidden layers ('info' + 'related sites'). The problem is I can't figure out how to make it dissapear again. I've tried the (onMouseOut) function, but thats no good because the layers dissapear before you can click them.

The drop down only works on this one page at the moment:

http://www.dlthomas.co.uk/new/homepage.html

I really am stuck this time, I've tried my best but can't solve this problem. If you know how to sort it, I'd be very grateful to hear from you.:(

JPnyc
08-13-2006, 10:57 PM
Where's this dropdown? And I assume you mean Javascript? Because java is an entirely different animal.

Daves_shed
08-13-2006, 11:45 PM
yeah javascript sorry (I'm just using the behaviers panel in dreamweaver)

If you click on the tab 'company' it should make two other tabs appear underneath. It only works on this page at the moment as I havent edited the others. The links won't take you anywhere yet either, I havent created the pages for them.

It would work if I put the links in. The problem is, its gonna look odd if I leave it as it is.

the page its on is here:

http://www.dlthomas.co.uk/new/homepage.html

tZ
08-14-2006, 01:02 AM
- the below is not a hand feed way of making your behavior work. Just the general thoery behind applied to a more simplistic exercise.

What I would recommend is attach them to your document and hide them.

Then set up a javascript listener to make them visible when you click over the one in associtaion.

Its not as easy as it sounds but, that is what I would recommend looking into.
So now taking this into account lets say we wanted to switch out the paragraph based on if the user puts the mouse over the div. This effect could not be done without javascript becasue it is a dynamic behavior. The page is not going to reload but, it will automatically switch out the sentence based on the event.

Which brings me to one last point- events. Events are the things that happen with a browser. The simplest way to put this is everyone knows what rolover is. That is essentially a event being triggered. When the user mouses over a given element a event is triggered to change some text or do something. Most programs limit this event to links and such.

However, this is where knowledge of the DOM comes in. Those of use who know the secrets of the DOM know this- every element within holds an event. That means that the browser is capable of reconizing whether or a not the mouse is over any given part of the document.

The only catch is we should define a width and hieght attribute to that element unless its contained with a contained that makes it dependent. In this instance, it is not. So I am going to style the div so it has a set height and width for someone to mouseover rather then spanning the page(by default).


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Floated column example</title>
<meta http-equiv="content-type"
content="text/html; charset=utf-8" />
<style type="text/css">
#text {
width: 200px;
height: 300px;
padding: 10px;
border: thin solid black;
}
</style>
</head>
<body>
<div id= "text">
<p>this is some text in a box</p>
</div>
</body>
</html>


All that code above does is set a width and height and a border with padding- thats all. if your familier with CSS then that should be obvious.

So now lets work on your behavior. Our behavior is that we want to switch out the text based on a rollover of that div. So lets get to work.

First of we start with declaring a javascript script:


<script stype = "text/javascript">
</script>


Now we put all our javascript within that script. You can attach multiple scripts to one document and/or exteneral scripts but, we are going to keep this simple.

usually the best thing to do is put your javscript(bahavior) in the head section of the document. This is best practice for semantic mark-up.


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Floated column example</title>
<meta http-equiv="content-type"
content="text/html; charset=utf-8" />
<script type="text/javascript">
</script>
<style type="text/css">
#text {
width: 200px;
height: 300px;
padding: 10px;
border: thin solid black;
}
</style>
</head>
<body>
<div id= "text">
<p>this is some text in a box</p>
</div>
</body>
</html>


This sets up our script within our document.

So now lets work on the javascript.

In order to get this to work the first thing we need to do is set up a event listener for our div.

This is how we do that:


<script type="text/javascript">
function addl() {
var di = document.getElementById('text');
di.addEventListener('mouseover', switchit, false);
}
window.addEventListener('load', addl, false);
</script>


I'm not going to dwell on thoery here but, this is the what essentially happening. before anything can be targted, we need the page to load. So the bottom line tells the browser to execute the listener after the page loads. The function addl is then added to the page to listen for our event.

Once the addl function is triggered I target the div and give it a variable of di. After which I set up a event listener on it for the mouseover state. Now once the browser detects the mosue over the area it will trigger the function switchit.

We have yet to indicate our switchit function so nothing will hapen at this point. So the next part is telling the browser the behavior that needs to happen on the mouseover of the div.


<script type="text/javascript">
function switchit() {
var str = document.createTextNode('it is being switched out for your own good');
var par = document.getElementsByTagName('p')[0];
var sent = par.firstChild;
par.replaceChild(str, sent);
}
function addl() {
var di = document.getElementById('text');
di.addEventListener('mouseover', switchit, false);
di.addEventListener('mouseout', switchback, false);
}
window.addEventListener('load', addl, false);
</script>


This next part gets a tad bit more complex but, essentially thiis is what is happening. The first line creates a text node with the new text to switch out of the div. Text nodes all allways children of thier containing element. So in this case the paragraphs childr is the text within/text node. I assign that with a variable also so i can refer to it easier later. next I target my paragraph which I want to switch out. I am not switching out the paragraph only the text within so I need a reference to that element also. In this stance, I use a getElementsByTagName(). This return a array of elements unlike the ID target. So I cleaify with the [0] I want the first paragraph in the document. If I specified [1] it would give me the seconf paragraph. There doesn't exist a second paragraph so it would be null. Therefor, I target the one that exist- the first and apply a variable.

Now I have references to all I need to make this work. On promblem though… how do iactually attach this new method to the document. Well there are more then one ways to do this but, since, we are going to be switching out information the best way to do this is to use the replaceChild method.

The replaceChild method works like this. lets say we have a parent and want to replace its children with something else like a new text node. This can be done with relative ease using this method since, we are replacing children[/b] rather then inserting something toally new.

ok…pk… that may not make a whole lot of sense, but just know this is the right one to use for this behavior.

Since, I have a variable whcih contains my parent allready this is done with ease usng the method and telling it what to switch out and replace like so:


par.replaceChild(str, sent);


par being our pargraph sent being the text we want to replace and str being the new text to use.

That simple…

lol

You can test this out by saving it in your notepad as .html and utf-8 file. Then move to the browser and walla. When you mouse over the the area withi the dv the text should swap out.

Ahh… there is that word- swab.

So essentially, all swab based effects can be achieved using something such as the method I described above. yes, it will different but, the basic philosophy stays continigent. Knowing what you want to target and what you want to replace it with is the first step. Finding the most appropriate properties is the second part. Then making it work is the last,lol.

So this may not be the exact effect your after but, think about your effect in terms of the DOM and how it can be created. The same thing I did here with text can be done with images. The only different is you would attach and image rather then text without using the createTextNode object.

As you can see its quite complex to do such a mezzly task but heh… thats the DOM and javascript for you- in other words- DHTML. Combining the DOM with javascript to create dymamic behavior.

disclaimer; this doesn't work in IE and afew other browsers becasue IE is weird,lol. Well not really weird but, I would have to go through much more then present for you to understand why and how to make it work in IE and I even get confussed thinking about. The basic idea though is that IE [b]doesn't[b] support addEventListener instead it uses its own event handler. Then stores the behavior in the window rather then the object it self. yeah… it gets quite complicated so lets just stick with this for now- that damn browser,lol.

The below is the code in whole with an dded switchback function but, you can ignore that.


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Floated column example</title>
<meta http-equiv="content-type"
content="text/html; charset=utf-8" />
<script type="text/javascript">
function switchit() {
var str = document.createTextNode('it is being switched out for your own good');
var par = document.getElementsByTagName('p')[0];
var sent = par.firstChild;
par.replaceChild(str, sent);
}

function switchback() {
var str = document.createTextNode('this is some text in a box');
var par = document.getElementsByTagName('p')[0];
var sent = par.firstChild;
par.replaceChild(str, sent);
}

function addl() {
var di = document.getElementById('text');
di.addEventListener('mouseover', switchit, false);
di.addEventListener('mouseout', switchback, false);
}
window.addEventListener('load', addl, false);
</script>
<style type="text/css">
#text {
width: 200px;
height: 300px;
padding: 10px;
border: thin solid black;
}
</style>
</head>
<body>
<div id= "text">
<p>this is some text in a box</p>
</div>
</body>
</html>


hope that helps good luck.

JPnyc
08-14-2006, 01:40 AM
Part of the problem is I was expecting them to appear onmouseover, which is the normal behavior

Daves_shed
08-14-2006, 03:01 AM
-



Oh my good god.

I think I'd better try to learn some of that funky code. The language you wrote that in is DHTML is it? Is DHTML primarily for movement and dinamics?

I tried your script in Firefox and see how it works. All I have to do now is actually understand how to do it myself.

Are there any online tutorial sites or books you could recomend? You seem to be an expert on it.

Thanks for all your help:)

tZ
08-14-2006, 03:18 AM
lol

expert ha

anyhow, yes this is called DHTML.

Bringing together the DOM with javascript to create dynamic behavior.

Basically said… behavior that isn't dependent on the browser reloading like switching web pages.

Two books that are excellent are:

DOM scripting by Jeremey Kieth

The other is a sitepoint book(more advanced) called DHTML Utopia.

As complex as it seems I have been looking at it for about 2 months using those books above and the interent as a guide. I wouldn't call myself… an expert,lol.

good luck though.

JPnyc
08-14-2006, 03:44 AM
That's an old style way of creating dropdowns that I highly recommend be avoided at all costs. It's about as search engine unfriendly as can be, and frankly it's no longer used. Just hard code the navs into the page, hidden with CSS, then hide/show with javascript.

JPnyc
08-14-2006, 03:54 AM
Here's (http://www.jpages.net/Jupiter/earthweb.htm) a mock-up I did for the web dept. to show them the concept. Maybe you can derive what's going on from it. The actual code that hides and shows the menus is tiny.

Daves_shed
08-14-2006, 10:13 AM
Here's (http://www.jpages.net/Jupiter/earthweb.htm) a mock-up I did for the web dept. to show them the concept. Maybe you can derive what's going on from it. The actual code that hides and shows the menus is tiny.

Ah great. Thats exactly the sort of thing I need to do I think. Only I'd be using picture buttons rather than colour and text links.

<div id="web" class="menus"onmouseover="showEm('web');"onmouseout="hideEm()"style="width:150px; left:260px;">

Does that bit of your code define the amount of space you have to roll out of before it dissapears?

Because that is the problem I'm having. I need to have a larger space than just the jpg thats activating the drop down. The reason I have made you click the menu (on mine) instead of hover over is because the drop menu dissapears before the curser reaches it. If I could figure that out I think It would work.

JPnyc
08-14-2006, 01:12 PM
No, I didn't put any timeouts in it, wasn't necessary. If you wanna use timeouts this gets more complex. I just arranged the menus so there was no room between them and the link that called em. So no delay was needed

Patrick Shannon
08-14-2006, 09:10 PM
I've used Suckerfish navs on many occasions for dropdowns, A List Apart has it all here: http://www.alistapart.com/articles/dropdowns. Pure CSS, no Javascript (with one exception). I've just built out a website that uses them: http://dev.gatewayambulance.com

Now the exception. Internet Explorer f**ks it up. So you have to apply a little Javascript to hack around it. Once you've figured it out, they work very well cross-browser.