Wednesday, October 20, 2010

Text Flow in Columns (CSS, HTML, JS)

I was helping someone get some text to automatically overflow into adjoining columns. Much like a news paper. This is possible to do at run time with simply some javascript.
   1: <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
   2: "http://www.w3.org/TR/html4/loose.dtd">
   3: <html xmlns="http://www.w3.org/1999/xhtml">
   4:     <head>
   5:         <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
   6:         <title>Text Flow</title>
   7:         <!--jq UI -->
   8:         <link rel="stylesheet" type="text/css" href="./lib/lightness/jquery-ui-1.8.5.custom.css" />
   9:         <!-- jQuery -->
  10:         <script type="text/javascript" src="./lib/jquery/jquery-1.4.2.min.js"></script>
   1:  
   2:         <script type="text/javascript" src="./lib/jquery/jquery-ui-1.8.5.custom.min.js">
   1: </script>
   2:         <!-- Custom JS-->
   3:         <script type="text/javascript" src="./lib/app-js/text-flow.js">
</script>
  11:  
  12:         <style>
  13:             #container {
  14:                 width:1024px;
  15:             }
  16:             
  17:         </style>
  18:     </head>
  19:     <body>
  20:         <p>
  21:             <div id="container">
  22:                 <div id="flow-container"></div>
  23:                 <div id="flow-button-container"></div>
  24:                 <div id="text-container">
  25:                     <h2>What is it?</h2>
  26:                     <p>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.</p>
  27:                     
  28:                     <h2>Why do we use it?</h2>
  29:                     
  30:                     <p>It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout. The point of using Lorem Ipsum is that it has a more-or-less normal distribution of letters, as opposed to using 'Content here, content here', making it look like readable English. Many desktop publishing packages and web page editors now use Lorem Ipsum as their default model text, and a search for 'lorem ipsum' will uncover many web sites still in their infancy. Various versions have evolved over the years, sometimes by accident, sometimes on purpose (injected humour and the like).</p>
  31:                      
  32:                     <h2>Where does it come from?</h2>
  33:                     
  34:                     <p>Contrary to popular belief, Lorem Ipsum is not simply random text. It has roots in a piece of classical Latin literature from 45 BC, making it over 2000 years old. Richard McClintock, a Latin professor at Hampden-Sydney College in Virginia, looked up one of the more obscure Latin words, consectetur, from a Lorem Ipsum passage, and going through the cites of the word in classical literature, discovered the undoubtable source. Lorem Ipsum comes from sections 1.10.32 and 1.10.33 of "de Finibus Bonorum et Malorum" (The Extremes of Good and Evil) by Cicero, written in 45 BC. This book is a treatise on the theory of ethics, very popular during the Renaissance. The first line of Lorem Ipsum, "Lorem ipsum dolor sit amet..", comes from a line in section 1.10.32.</p>
  35:                     
  36:                     <p>The standard chunk of Lorem Ipsum used since the 1500s is reproduced below for those interested. Sections 1.10.32 and 1.10.33 from "de Finibus Bonorum et Malorum" by Cicero are also reproduced in their exact original form, accompanied by English versions from the 1914 translation by H. Rackham.</p>
  37:                     
  38:                     <h2>Where can I get some?</h2>
  39:                     <p>There are many variations of passages of Lorem Ipsum available, but the majority have suffered alteration in some form, by injected humour, or randomised words which don't look even slightly believable. If you are going to use a passage of Lorem Ipsum, you need to be sure there isn't anything embarrassing hidden in the middle of text. All the Lorem Ipsum generators on the Internet tend to repeat predefined chunks as necessary, making this the first true generator on the Internet. It uses a dictionary of over 200 Latin words, combined with a handful of model sentence structures, to generate Lorem Ipsum which looks reasonable. The generated Lorem Ipsum is therefore always free from repetition, injected humour, or non-characteristic words etc.</p>
  40:                 </div>    
  41:             </div>
  42:         </p>        
  43:     </body>
  44: </html>



In its most rudimentary form, the program assumes that number of columns and the width of the outtermost container (<div id=”container” />) are known.

With some jquery, to assist:

  1. I stuck the whole text into 1st column with width = container_width/number_of columns_desired
  2. Styled the column to
    1. show no scrollbars
    2. recalculated this columns and all its siblings to the same height (gives us the viewable area)

The problem that remains is to manipulate the scrollbar positions of the column_2,…, column_n. That is simple.

column_2’s scrollbar top = column_1’s height
column_3’s scrollbar top = column_1’s height + column_2’s height
… …
column_n’s scrollbar top = column_1’s height + column_2’s height + … + column_(n-1)’s height


   1: $(function (){
   2:     var html_text = $('#text-container').html();
   3:     $('#text-container').fadeOut('slow');
   4:     
   5:     var NUM_COLUMNS = 3;
   6:     
   7:     var available_width = $('#container').width();
   8:     var available_height = $('#container').height();
   9:  
  10:     var unit_width = parseInt(available_width/NUM_COLUMNS) -5;
  11:     var unit_height = parseInt(available_width/NUM_COLUMNS);
  12:     
  13:     var units = [];
  14:     
  15:     for (var i = 0; i < NUM_COLUMNS; i++) {
  16:         $('#flow-container').append("<div id='flow-container_unit_"+i+"' class='jq-unit'></div>");
  17:         units.push("flow-container_unit_"+i);
  18:     }
  19:     
  20:     
  21:     $('div.jq-unit').width(unit_width);
  22:     $('div.jq-unit').css({
  23:         "overflow": "hidden",
  24:         "float": "left",
  25:         "text-align":"left", "margin":"2px",
  26:         "padding-top":"2px"
  27:     });
  28:     $('div.jq-unit').html(html_text);
  29:     $('div.jq-unit').height($('div.jq-unit')[0].scrollHeight/NUM_COLUMNS);
  30:     
  31:     var length = units.length;
  32:     var scrl_top_calc =  0;
  33:     
  34:     for (var j=0;j<=length;j++) {
  35:         $('#'+units[j]).scrollTop(scrl_top_calc);
  36:         scrl_top_calc += $('#'+units[j]).height();
  37:     }
  38: });

That will yield a result like the following

TextFlow

The problem I’m running into is that the text displayed my get either cut from the bottoms or the tops. If I manipulate the heights of the display areas, then I get line repeats, which is a bad reading experience.

I thought of putting in a scrollbar (that’s why the jquery-ui imports), but then I thought that defeats the purpose if this excercise.

I might put a couple of buttons that will allow some 5-to-10 pixel scrolling to just read the line that got cut off. Perhaps a bad work around.

If anyone has better solutions, please post replies…

1 comment:

Claude said...

I had to try this. Take a look at my solution: http://code.betancourt.us/multi-column/