views:

45

answers:

1

Below is my HTML and CSS. I want to use javascript to determine when one of the LIs is overflown. I then want to put that item into a popup menu, similar to Win32 toolbars. I need to know when an LI is overflown and which LIs are overflow.

I'm fine if this only works in modern browsers and I don't want to use a framework like JQuery.

<div id="menu">
  <ul>
    <li>item1</li>
    <li>item2</li>
    <li>item3</li>
  </ul>
</div>

#menu ul {
margin: 0px;
list-style-type: none;
list-style-image: none;
height: 28px;
overflow: hidden;
}
+4  A: 

You need to do this with JavaScript. Compare scrollHeight and offsetHeight. If the scrollHeight is bigger, it's overflowed. E.g.

function overflowed(ElementID) {
  var Element=document.getElementById(ElementID);

  return Element.scrollHeight>Element.offsetHeight;
}

Code in action.

Gert G
This isn't working for me. It always returns false. Maybe it's becacuse I'm using overflow:hidden and not overflow:scroll.
j40
If you look at the "Code in action" example, you can see that my example uses `overflow: hidden;`. Which browser are you using?
Gert G
Also, make sure you test it on your menu, not the `DIV`. It's the `UL` in your example that has `overflow: hidden;`.
Gert G
It does work, but I'm trying to do something a little different. I need to know when an <li> is overflown, and which <li>s are overflown. This helps with the when, but not the which. Do you know how to tell which ones are overflown?
j40
@j40 - Have a look at this example: http://jsfiddle.net/46F8q/
Gert G
You'll want to loop through the elements children then and check to see if their 'offsetTop' is greater than the parent element's offsetHeight. To get the children in your case you will want to use:var children = Element.getElementsByTagName('li');
Jim Jeffers