offsetTop prob - need to determine if a div is visible

Reply

Join Date: Mar 2006
Posts: 1
Reputation: indigopear.com is an unknown quantity at this point 
Solved Threads: 0
indigopear.com indigopear.com is offline Offline
Newbie Poster

offsetTop prob - need to determine if a div is visible

 
0
  #1
Mar 23rd, 2006
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. }
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the HTML and CSS Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC