www.webdeveloper.com
Recent Articles
  • Finding Slow Running Queries in ASE 15
  • A More Advanced Pie Chart for Analysis Services Data
  • Adobe AIR Programming Unleashed: Working with Windows
  • Performance Testing SQL Server 2008's Change Data Capture Functionality
  • The ABC's of PHP: Introduction to PHP
  • How to Migrate from BasicFiles to SecureFiles Storage
  • Why the Twitter Haters Are Wrong
  • User Personalization with PHP: Beginning the Application
  • Whats in an Oracle Schema?
  • Lighting Enhancement in Photoshop
  •  

    Go Back   WebDeveloper.com > Client-Side Development > JavaScript

    JavaScript JavaScript (not Java) Discussion and technical support, including AJAX and frameworks (JQuery, MooTools, Prototype...)

    Reply
     
    Thread Tools Rate Thread Display Modes
      #1  
    Old 01-25-2009, 03:14 PM
    hoopplaya4 hoopplaya4 is offline
    Registered User
     
    Join Date: Nov 2008
    Posts: 12
    Detect Vertical Overflow in a DIV

    Hi All:

    Hopefully this is an easy question. I'm learning JavaScript right now, and I'd like to know if and how I might use JavaScript to detect the vertical overflow of text in a DIV. I don't want to have to use scrollbars when the text overflows.

    For example, right now I have:
    Code:
    <div id="container">
       <div id="item">This is item 1</div>
       <div id="item">This is item 2</div>
       <div id="item">This is item 3</div>
       <div id="item">This is item 4</div>
    </div>
    Now, if only item 1, 2, and 3 are visible, I'd like JavaScript to catch the overflow, and then replace the last visible item (item 3), with a new DIV:
    Code:
    <div id="item><a href='more.html'>More...</a></div>
    I found a solution that's similar here. However, my issue pertains to vertical space (whereas the link is with horizontal). I think I'm going to have to user offsetHeight, but I'm not the best at Javascript yet.

    Any push in the right direction will be a tremendous help! Thank you.
    Reply With Quote
      #2  
    Old 01-26-2009, 04:36 AM
    vwphillips vwphillips is offline
    Registered User
     
    Join Date: Jun 2004
    Location: Portsmouth UK
    Posts: 2,167
    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    
    <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
    
    <head>
      <title></title>
    <style type="text/css">
    /*<![CDATA[*/
    .container {
      width:200px;height:50px;border:solid black 1px;
    }
    
    /*]]>*/
    </style>
    <script language="JavaScript" type="text/javascript">
    /*<![CDATA[*/
    
    function Overflow(obj){
     if (typeof(obj)=='string') obj=document.getElementById(obj);
     for (var clds=obj.childNodes,nu,cldary=[],z0=0;z0<clds.length;z0++){
      cldary.push(clds[z0]);
      if (clds[z0].offsetTop+clds[z0].offsetHeight<obj.offsetHeight) nu=z0;
     }
     for (var ary=[],z1=nu+1;z1<cldary.length;z1++){
      ary.push(obj.removeChild(cldary[z1]));
     }
     if (ary.length>1){
      var newobj=document.createElement(obj.tagName);
      newobj.className=obj.className;
      obj.parentNode.insertBefore(newobj,obj);
      obj.parentNode.insertBefore(obj,newobj);
      for (var z2=0;z2<ary.length;z2++){
       newobj.appendChild(ary[z2]);
      }
      Overflow(newobj);
     }
    }
    /*]]>*/
    </script>
    
    </head>
    
    <body onload="Overflow('container');" >
    
    Note: id names MUST&nbsp;BE unique.
    <div id="container"  class="container">
    tom
       <div >This is item 1</div>
       <div >This is item 2</div>
       <div >This is item 3</div>
       <div >This is item 4</div>
       <div >This is item 5</div>
       <div >This is item 6</div>
       <div >This is item 7</div>
       <div >This is item 8</div>
       <div >This is item 9</div>
    </div>
    </body>
    
    </html>
    __________________
    Vic

    God loves you and will never love you less.

    http://www.vicsjavascripts.org.uk
    remove any spaces between java & script
    Reply With Quote
      #3  
    Old 01-26-2009, 10:18 AM
    hoopplaya4 hoopplaya4 is offline
    Registered User
     
    Join Date: Nov 2008
    Posts: 12
    Thanks VERY much for the reply, vwphillips. However, I guess I'm too much of a newbie to understand it all.

    How would I go about inserting my new object with the script you posted?
    Reply With Quote
      #4  
    Old 01-26-2009, 11:29 AM
    vwphillips vwphillips is offline
    Registered User
     
    Join Date: Jun 2004
    Location: Portsmouth UK
    Posts: 2,167
    this puts the new divs in an existing div

    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    
    <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
    
    <head>
      <title></title>
    <style type="text/css">
    /*<![CDATA[*/
    .container {
      width:200px;height:50px;border:solid black 1px;
    }
    
    /*]]>*/
    </style>
    <script language="JavaScript" type="text/javascript">
    /*<![CDATA[*/
    
    function Overflow(obj,pobj){
     if (typeof(obj)=='string') obj=document.getElementById(obj);
     if (!pobj){
      pobj=obj;
      pobj.ary=[];
     }
     for (var clds=obj.childNodes,ary=[],z0=0;z0<clds.length;z0++){
      if (clds[z0].offsetTop+clds[z0].offsetHeight>obj.offsetHeight) ary.push(clds[z0]);
     }
     if (ary.length>0){
      for (var z1=0;z1<ary.length;z1++) obj.removeChild(ary[z1]);
      var newobj=document.createElement(obj.tagName);
      newobj.className=obj.className;
      obj.parentNode.insertBefore(newobj,obj);
      obj.parentNode.insertBefore(obj,newobj);
      for (var z2=0;z2<ary.length;z2++) newobj.appendChild(ary[z2]);
      pobj.ary.push(newobj);
      return Overflow(newobj,pobj);
     }
     else return pobj.ary;
    }
    
    function MoveOverflow(objid,newid){
     var overflowdivs=Overflow(objid);
     for (var z0=0;z0<overflowdivs.length;z0++){
      document.getElementById(newid).appendChild(overflowdivs[z0]);
     }
    
    }
    /*]]>*/
    </script>
    
    </head>
    
    <body onload="MoveOverflow('container','tst2');" >
    
    Note: id names MUST&nbsp;BE unique.
    <div id="container"  class="container">
    tom
       <div >This is item 1</div>
       <div >This is item 2</div>
       <div >This is item 3</div>
       <div >This is item 4</div>
       <div >This is item 5</div>
       <div >This is item 6</div>
       <div >This is item 7</div>
       <div >This is item 8</div>
       <div >This is item 9</div>
    </div>
    <br />
    <br />
    <div id="tst2" ></div>
    </body>
    
    </html>
    this put the overflow in an existng div

    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    
    <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
    
    <head>
      <title></title>
    <style type="text/css">
    /*<![CDATA[*/
    .container {
      width:200px;height:50px;border:solid black 1px;
    }
    
    /*]]>*/
    </style>
    <script language="JavaScript" type="text/javascript">
    /*<![CDATA[*/
    
    function Overflow(obj,pobj){
     if (typeof(obj)=='string') obj=document.getElementById(obj);
     if (!pobj){
      pobj=obj;
      pobj.ary=[];
     }
     for (var clds=obj.childNodes,ary=[],z0=0;z0<clds.length;z0++){
      if (clds[z0].offsetTop+clds[z0].offsetHeight>obj.offsetHeight) ary.push(clds[z0]);
     }
     for (var z1=0;z1<ary.length;z1++) obj.removeChild(ary[z1]);
     return ary;
    }
    
    function MoveOverflow(objid,newid){
     var overflowdivs=Overflow(objid);
     for (var z0=0;z0<overflowdivs.length;z0++){
      document.getElementById(newid).appendChild(overflowdivs[z0]);
     }
    
    }
    /*]]>*/
    </script>
    
    </head>
    
    <body onload="MoveOverflow('container','tst2');" >
    
    Note: id names MUST&nbsp;BE unique.
    <div id="container"  class="container">
    tom
       <div >This is item 1</div>
       <div >This is item 2</div>
       <div >This is item 3</div>
       <div >This is item 4</div>
       <div >This is item 5</div>
       <div >This is item 6</div>
       <div >This is item 7</div>
       <div >This is item 8</div>
       <div >This is item 9</div>
    </div>
    <br />
    <br />
    <div id="tst2" ></div>
    </body>
    
    </html>
    __________________
    Vic

    God loves you and will never love you less.

    http://www.vicsjavascripts.org.uk
    remove any spaces between java & script
    Reply With Quote
      #5  
    Old 01-26-2009, 11:44 AM
    hoopplaya4 hoopplaya4 is offline
    Registered User
     
    Join Date: Nov 2008
    Posts: 12
    Great! This is very close to what I need. The only other thing I'd like is:

    When items are overflowed into the other DIV, I'd like the "tst2" DIV to be hidden. (Which I know how to do). But then, have a link show up at the bottom of the "container" DIV, that says: "Show More..."

    For example, on the event of an overflow:

    Code:
    <div id="container"  class="container">
    tom
       <div >This is item 1</div>
       <div >This is item 2</div>
       <div >This is item 3</div>
       <div >This is item 4</div>
       <div ><a href="some js to change display of 'tst2'">More items...</a></div>
    
    <div id="tst2" style="display:none;"> 
       <div >This is item 5</div>
       <div >This is item 6</div>
       <div >This is item 7</div>
       <div >This is item 8</div>
       <div >This is item 9</div>
    </div>
    </div>
    Reply With Quote
      #6  
    Old 01-26-2009, 04:17 PM
    hoopplaya4 hoopplaya4 is offline
    Registered User
     
    Join Date: Nov 2008
    Posts: 12
    Do you know how I'd go about doing this sort of thing?
    Reply With Quote
      #7  
    Old 01-27-2009, 05:13 AM
    vwphillips vwphillips is offline
    Registered User
     
    Join Date: Jun 2004
    Location: Portsmouth UK
    Posts: 2,167
    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    
    <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
    
    <head>
      <title></title>
    <style type="text/css">
    /*<![CDATA[*/
    .container {
      width:200px;height:50px;border:solid black 1px;
    }
    
    /*]]>*/
    </style>
    <script language="JavaScript" type="text/javascript">
    /*<![CDATA[*/
    
    function Overflow(obj,pobj){
     if (!pobj){
      pobj=obj;
      pobj.ary=[];
     }
     for (var clds=obj.childNodes,ary=[],z0=0;z0<clds.length;z0++){
      if (clds[z0].offsetTop+clds[z0].offsetHeight>obj.offsetHeight) ary.push(clds[z0]);
     }
     for (var z1=0;z1<ary.length;z1++) obj.removeChild(ary[z1]);
     return ary;
    }
    
    function MoveOverflow(obj,newid,more){
     if (typeof(obj)=='string') obj=document.getElementById(obj);
     var ofdivs=Overflow(obj);
     var newobj=document.getElementById(newid);
     for (var z0=0;z0<ofdivs.length;z0++){
      newobj.appendChild(ofdivs[z0]);
     }
     var more=document.getElementById(more);
     if (more){
      if (ofdivs.length>0){
        obj.appendChild(more);
      }
      else {
        obj.removeChild(more);
      }
     }
     var lst=ofdivs[ofdivs.length-1];
     newobj.style.display='none';
    }
    
    function Toggle(id,lk){
     var obj=document.getElementById(id);
     obj.style.display=obj.style.display=='none'?'block':'none';
     lk.innerHTML=lk.innerHTML=='More'?'Less':'More';
    }
    
    /*]]>*/
    </script>
    
    </head>
    
    <body onload="MoveOverflow('container','tst2','more');" >
    
    Note: id names MUST&nbsp;BE unique.
    <div id="container"  class="container">
    <div ><a id="more" href="#" onclick="Toggle('tst2',this); return false;" >More</a></div>
       <div >This is item 1</div>
       <div >This is item 2</div>
       <div >This is item 3</div>
       <div >This is item 4</div>
       <div >This is item 5</div>
       <div >This is item 6</div>
       <div >This is item 7</div>
       <div >This is item 8</div>
       <div >This is item 9</div>
    </div>
    <div id="tst2" style="border:solid black 1px;"></div>
    </body>
    
    </html>
    __________________
    Vic

    God loves you and will never love you less.

    http://www.vicsjavascripts.org.uk
    remove any spaces between java & script
    Reply With Quote
      #8  
    Old 01-28-2009, 10:55 AM
    hoopplaya4 hoopplaya4 is offline
    Registered User
     
    Join Date: Nov 2008
    Posts: 12
    That was it! Awesome, thank you very much. Learned something new as well.
    Reply With Quote
      #9  
    Old 03-12-2009, 08:13 AM
    Pomax Pomax is offline
    Registered User
     
    Join Date: Mar 2009
    Posts: 2
    Hmm, a question on modifying that a little

    This is a fascinating bit of forum post, and close to a problem I was trying to solve by browsing the internet for solutions - I have a large wad of text in a div, rather than div (or other formatting) elements, and would like to detect at which word in the text overflow occurs, so I can relocate the rest of the text to a next (either predefined or dynamically added) container element.

    so turning

    <div>
    This is some random text with a lot of words and will overflow after the upcoming comma, and then this text should be relocated to a new div
    </div>

    becoming

    <div>
    This is some random text with a lot of words and will overflow after the upcoming comma
    </div>
    <div>
    and then this text should be relocated to a new div
    </div>

    any ideas? (other than wrapping every individual word with a div? =)

    - Pomax
    Reply With Quote
      #10  
    Old 03-15-2009, 07:59 AM
    Pomax Pomax is offline
    Registered User
     
    Join Date: Mar 2009
    Posts: 2
    figured it out

    After a few days of tinkering I figured out how to do what I asked anyway, so for those interested in the result, http://pomax.nihongoresources.com/downloads/bookstyle/

    (the relevant js is http://pomax.nihongoresources.com/do...egeneration.js)

    - Pomax
    Reply With Quote
    Reply

    Bookmarks


    Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
     
    Thread Tools
    Display Modes Rate This Thread
    Rate This Thread:

    Posting Rules
    You may not post new threads
    You may not post replies
    You may not post attachments
    You may not edit your posts

    BB code is On
    Smilies are On
    [IMG] code is Off
    HTML code is Off
    Forum Jump


    All times are GMT -5. The time now is 01:13 PM.



    Acceptable Use Policy

    Internet.com
    The Network for Technology Professionals

    Search:

    About Internet.com

    Legal Notices, Licensing, Permissions, Privacy Policy.
    Advertise | Newsletters | E-mail Offers

    Powered by vBulletin® Version 3.7.3
    Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.