Connecting Tech Pros Worldwide Help | Site Map

Cannot see all content

Claus Mygind's Avatar
Familiar Sight
 
Join Date: Mar 2008
Posts: 173
#1: Jul 22 '09
I have a hidden <div> which I populate and then display relative to the link that activates it.

My problem is this, when the link is near the bottom of the page and the content of the hidden <div> extends below the bottom, I cannot scroll to see it.

I would like to adjust the top of the <div> placement if it should extend below the screen.

Below is the code I use to determine where the <div> should be displayed.
Basically I capture the current mouse position with the event listener ".onmousemove".

If the height of the <div> should exceed the available room I would like to adjust the top location d.style.top = (cY-46) + "px"; to something other than -46 from the relative position.

How can I determine the height of the dynamically created division?
And how can I determine if it is exceeding the bottom of the page?


Expand|Select|Wrap|Line Numbers
  1. var cX = 0; var cY = 0; var rX = 0; var rY = 0;
  2.  
  3. document.onmousemove = UpdateCursorPosition; 
  4.  
  5. function UpdateCursorPosition(e)
  6.     cX = e.pageX; cY = e.pageY;
  7. }
  8.  
  9. function AssignPosition(d) 
  10. {
  11.     if(self.pageYOffset) 
  12.     {
  13.         rX = self.pageXOffset;
  14.         rY = self.pageYOffset;
  15.     }else if(document.documentElement && document.documentElement.scrollTop) 
  16.     {
  17.         rX = document.documentElement.scrollLeft;
  18.         rY = document.documentElement.scrollTop;
  19.     }
  20.     else if(document.body) 
  21.     {
  22.         rX = document.body.scrollLeft;
  23.         rY = document.body.scrollTop;
  24.     }
  25.     if(document.all) 
  26.     {
  27.         cX += rX; 
  28.         cY += rY;
  29.     }
  30.     d.style.left = (cX+107) + "px";
  31.     d.style.top = (cY-46) + "px";
  32. }
  33.  
  34. function ShowContent(d) 
  35. {
  36.     if(d.length < 1) 
  37.     { 
  38.         return; 
  39.     }
  40.     var dd = document.getElementById(d);
  41.     AssignPosition(dd);
  42.     dd.style.display = "block";
  43. }
  44.  
  45.  
  46.  
hsriat's Avatar
Expert
 
Join Date: Jan 2008
Location: Bath, UK
Posts: 1,609
#2: Jul 22 '09

re: Cannot see all content


Quote:

Originally Posted by Claus Mygind View Post

How can I determine the height of the dynamically created division?

You can do that by returning div.style.height, BUT only if you have set it, and have not let it attain the height according to its inner elements.

If you have just text inside the div, then you can calculate approx. height by counting the number of chars in the text, and font size.

Quote:

Originally Posted by Claus Mygind View Post

And how can I determine if it is exceeding the bottom of the page?

Calculate it with div's top y co-od, div's height and screen height (screen.availHeight)
gits's Avatar
Moderator
 
Join Date: May 2007
Location: Munich, Germany
Posts: 4,129
#3: Jul 23 '09

re: Cannot see all content


may be the getComputedStyle() might help? have a look here for the idea and usage.

kind regards
Claus Mygind's Avatar
Familiar Sight
 
Join Date: Mar 2008
Posts: 173
#4: Jul 23 '09

re: Cannot see all content


Quote:

Originally Posted by gits View Post

may be the getComputedStyle() might help? have a look here for the idea and usage.

kind regards

Ok that was a very handy article and did get to the point where I can calculate if the popup I want to display is going to be longer than the available space. Also I can calculate what the new position should be based on the amount of overflow (see code below).

Now my problem is getting the <div> element to move to that new location.

In the code I have cobbled the following together.
1. Two <div> elements have been assigned style attributes "fullWidth" and "allContacts"

2. an event listener "onmousemove" tied to the document detects the location of the link in document when mouse is clicked.

3. The position of the "allContacts" <div> is assigned an offset based on this location.

4. Then from the article you suggested, I am able to calculate the top and height of the "allContacts". I compare that to the height of the "fullWidth" <div> and if the total is greater than height of "fullWidth", I then calculate the new top, where I would like to move the element to.

But how do I get it to move once it has been displayed?

One final note - I am working in FireFox and only want a solution for FireFox.

Expand|Select|Wrap|Line Numbers
  1. <STYLE TYPE="text/css" >
  2. #allContacts {font-size: 16px;
  3.     padding: 10px;
  4.     width: 50%;
  5.     border-width: 1px;
  6.     border-style: solid;
  7.     border-color: #cc0000;
  8. }
  9. #fullWidth {
  10.      position: absolute;
  11.      white-space: nowrap; 
  12.      left: 0%; 
  13.       top: 25px;
  14.       width: 99%;
  15.       color: #333333;
  16.       padding:5px;
  17.       font-size: 10pt;
  18.         overflow: hidden;
  19.       z-index: 98;
  20.  }
  21. </STYLE>
  22.  
  23. <SCRIPT LANGUAGE="JavaScript">
  24. <!--
  25. var cX = 0; var cY = 0; var rX = 0; var rY = 0;
  26.  
  27. document.onmousemove = UpdateCursorPosition; 
  28.  
  29. function UpdateCursorPosition(e)
  30.     cX = e.pageX; cY = e.pageY;
  31. }
  32.  
  33. function AssignPosition(d) 
  34. {
  35.     if(self.pageYOffset) 
  36.     {
  37.         rX = self.pageXOffset;
  38.         rY = self.pageYOffset;
  39.     }else if(document.documentElement && document.documentElement.scrollTop) 
  40.     {
  41.         rX = document.documentElement.scrollLeft;
  42.         rY = document.documentElement.scrollTop;
  43.     }
  44.     else if(document.body) 
  45.     {
  46.         rX = document.body.scrollLeft;
  47.         rY = document.body.scrollTop;
  48.     }
  49.     if(document.all) 
  50.     {
  51.         cX += rX; 
  52.         cY += rY;
  53.     }
  54.     d.style.left = (cX+107) + "px";
  55.     d.style.top = (cY-46) + "px";
  56. }
  57.  
  58. function ShowContent(d) 
  59. {
  60.     if(d.length < 1) 
  61.     { 
  62.         return; 
  63.     }
  64.     var dd = document.getElementById(d);
  65.     AssignPosition(dd);
  66.     dd.style.display = "block";
  67. getOff();
  68. }
  69.  
  70. //get current dimensions and position of an element
  71. function getOff()
  72. {
  73.     var x = document.getElementById('allContacts');
  74.     var y = document.getElementById('fullWidth');
  75.     if ( x.offsetHeight + x.offsetTop > y.offsetHeight)
  76.     {
  77.         x.top = (x.offsetTop - ((x.offsetHeight + x.offsetTop) - y.offsetHeight))+ 'px';
  78. alert(x.top);
  79.     }
  80.     return;
  81. }
  82. //-->
  83. </SCRIPT>
  84.  
  85. <a href="javascript:ShowContent('allContacts')"    
  86.               id="con_"
  87.               onclick="changeStatus(this)";
  88.               return false;"
  89. > View all contacts </a>
  90.  
  91.  
  92.  
Claus Mygind's Avatar
Familiar Sight
 
Join Date: Mar 2008
Posts: 173
#5: Jul 23 '09

re: Cannot see all content


I got the final piece of the puzzle. Just one minor adjustment to the code above.

I only needed to modify the line

x.top = ....

to

x.style.top = ...

Expand|Select|Wrap|Line Numbers
  1. //get current dimensions and position of an element
  2. function getOff()
  3. {
  4.     var x = document.getElementById('allContacts');
  5.     var y = document.getElementById('fullWidth');
  6.     if ( x.offsetHeight + x.offsetTop > y.offsetHeight)
  7.     {
  8.         x.style.top = (x.offsetTop - ((x.offsetHeight + x.offsetTop) - y.offsetHeight))+ 'px';
  9.     }
  10.     return;
  11. }
  12.  
Reply