943,900 Members | Top Members by Rank

Ad:
Mar 23rd, 2006
0

offsetTop prob - need to determine if a div is visible

Expand Post »
Hi,

I have a resizable container div with overflow:hidden. If an element within the container doesn't fit completely, we want to set its visibility to hidden (rather than letting it crop). I have a function that checks offsetTop and offsetHeight to determine if an element is within the bounds of its container's visible area.

The problem I am having is that in IE, I'm getting the wrong offsetTop values on some elements that wrap. There are 4 image links in the container. If the browser is large enough, they appear on one line. As the browser shrinks, 1 or 2 of them will wrap to the 2nd line. The first element that wraps has a smaller offsetTop value than elements on the 1st line. If 2 elements wrap, the 2nd one has a correct offsetTop.

(btw, I'm also seeing an incorrect offsetHeight in mozilla, but it seems to be working anyway?!)

Can anyone shed some light on this? Is there some better way to determine if an element is completely within the visible area of its container, or am I just doing something wrong?

Thanks!
-Ann

A test page is here: Portfolio.php

And here is the pertinent bit of code:

HTML and CSS Syntax (Toggle Plain Text)
  1. //
  2. // Set height of contentPanel
  3. //
  4. var y = y-196-50;
  5. var panel=document.getElementById('contentPanel');
  6. panel.style.height = y + 'px';
  7. //
  8. //
  9. // Set visibility of child elements in contentPanel
  10. //
  11. function findPosY(obj) // Get full top offset
  12. {
  13. var curtop = 0;
  14. if (obj.offsetParent)
  15. {
  16. while (obj.offsetParent)
  17. {
  18. curtop += obj.offsetTop
  19. obj = obj.offsetParent;
  20. }
  21. }
  22. else if (obj.y)
  23. curtop += obj.y;
  24. return curtop;
  25. }
  26. function setVisibility(node, y, panel) {
  27. var children=node.childNodes;
  28. for (var i=0; i < children.length; i++) {
  29. if (children[i].className && (children[i].className == 'toggle' || children[i].className.search('toggle'))) {
  30. var top=findPosY(children[i])
  31. var height = children[i].offsetHeight;
  32. var panelTop = findPosY(panel);
  33. //alert("offsetTop = " + top + " offsetHeight = " + height);
  34. if ( (top+height) > (y+panelTop) )
  35. children[i].style.visibility = 'hidden';
  36. else
  37. children[i].style.visibility = 'visible';
  38. }
  39. else
  40. setVisibility(children[i], y, panel);
  41. }
  42. }
  43. if (panel.className == 'noScroll') {
  44. panel.style.overflow = 'hidden';
  45. setVisibility(panel, y, panel);
  46. }
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
indigopear.com is offline Offline
1 posts
since Mar 2006

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in HTML and CSS Forum Timeline: Incorporating Excel Spreadsheets
Next Thread in HTML and CSS Forum Timeline: Password Protection from a txt.file





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC