faqts : Computers : Programming : Languages : JavaScript : Tables

+ Search
Add Entry AlertManage Folder Edit Entry Add page to http://del.icio.us/
Did You Find This Entry Useful?

28 of 40 people (70%) answered Yes
Recently 7 of 10 people (70%) answered Yes

Entry

How can I move a layer on top of a table cell?
How can I move a layer on top of a table cell?

Feb 15th, 2009 21:32
Inschi jelii, John Martin, Martin Honnen,


NN6, IE4+ and Opera 6 allow you to exactly determine the position of 
every element on the page by adding the
  offsetLeft
and
  offsetTop
properties along the 
  offsetParent
hierarchy. That works for td elements as well as other elements.
NN4 doesn't have that complete dom, you can only determine the 
position of layers/CSS-positioned elements. Fortunately, at least with 
simple tables, NN allows you to use CSS
  position: relative
and then gives you access to 
  document.layers['cellId'].pageX/pageY
Therefore the following example page which writes a random table and 
then allows to display a layer on top of a selected table cell works 
with NN4, NN6, IE4+ and Opera 6. However the NN4 solution is fragile, 
expect it to break if you nest tables, apply width attributes to the 
table or to cell elements or do other things html allows but NN4's 
restricted dom implementation can't cope with.
http://lahjatideoita.blogspot.com
http://ideaspara-regalar.blogspot.com
http://idearegalo1.blogspot.com
http://geschenkeidee.blogspot.com
http://darkovenapady.blogspot.com
*****************************************************************
<html>
<head>
<title>
cell position
</title>
<script type="text/javascript">
if (document.layers) 
  document.write(
'<style type="text/css">.scriptable { position: relative; }<\/style>');
function getCellPosition (cellId) {
  if (document.layers)
    return { x: document.layers[cellId].pageX, 
y: document.layers[cellId].pageY };
  else if (document.all || document.getElementById) {
    var cell = document.all ? document.all[cellId] : 
document.getElementById(cellId);
    var coords = {x: 0, y: 0 };
    while (cell) {
      coords.x += cell.offsetLeft;
      coords.y += cell.offsetTop;
      cell = cell.offsetParent;
    }
    return coords;
  }
}
function moveCellLayerOnCell (rowIndex, cellIndex) {
   var coords = getCellPosition('cellRow' + rowIndex + 'Col' + 
cellIndex);
   if (document.layers) {
     document.layers.cellLayer.left = coords.x;
     document.layers.cellLayer.top = coords.y;
     document.layers.cellLayer.visibility = 'show'
   }
   else if (document.all) {
     document.all.cellLayer.style.left = coords.x + 'px';
     document.all.cellLayer.style.top = coords.y + 'px';
     document.all.cellLayer.style.visibility = 'visible';
   }
   else if (document.getElementById) {
     document.getElementById('cellLayer').style.left = coords.x + 'px';
     document.getElementById('cellLayer').style.top = coords.y + 'px';
     document.getElementById('cellLayer').style.visibility = 'visible';
   } 
}
</script>
<style type="text/css">
#cellLayer {
  position: absolute;
  visibility: hidden;
  width: 50px; height: 25px;
  background-color: lightyellow;
  layer-background-color: lightyellow;
  clip: rect(0 50px 25px 0);
}
</style>
</head>
<body>
<table border="1">
<script type="text/javascript">
var rows = Math.floor(Math.random() * 10) + 5;
var cols = Math.floor(Math.random() * 10) + 3;
var html = '';
for (var r = 0; r < rows; r++) {
  html += '<tr>';
  for (var c = 0; c < cols; c++)
    html += '<td class="scriptable" id="cellRow' + r + 'Col' 
+ c + '">' + r + ', ' + c + ' Kibology<\/td>';
  html += '<\/tr>';
}
document.write(html);
</script>
</table>
<div id="cellLayer"></div>
<form name="gui">
<select name="rowIndex">
<script type="text/javascript">
for (var i = 0; i < rows; i++)
  document.write('<option value="' + i + '">' + i + '<\/option>');
</script>
</select>
<select name="cellIndex">
<script type="text/javascript">
for (var i = 0; i < cols; i++)
  document.write('<option value="' + i + '">' + i + '<\/option>');
</script>
</select>
<input type="button" value="move layer on top of cell"
       onclick="moveCellLayerOnCell(
this.form.rowIndex.options[this.form.rowIndex.selectedIndex].value, 
this.form.cellIndex.options[this.form.cellIndex.selectedIndex].value);"
/>
</form>
</body>
</html>
As a followup to the above someone asked whether/how it is possible to
position a layer on top of a table cell that sits in a scrollable
container element (as you can have with IE5+, Netscape 6+, Mozilla,
Opera 7+ when you use CSS width/height to restrict the width and/or
height of the container and set CSS overflow to scroll or auto). There
are different approaches to solve that, below I try to make as much use
of CSS and the DOM as possible by using DOM scripting to make the layer
a child of the container element, using DOM scripting of CSS to set
position to relative for the container so that the layer then only needs
to be positioned on the offset the table cell has to the container.
The approach works flawlessly with IE6/Win and with Mozilla 1.5 and
later and also with Opera 7 (tested with 7.20 and 7.50 preview) where
sometimes however for Opera for some cells the position is some pixels
too far to the right while I am not sure what makes the difference for
those cells.
The main flaw however is that it doesn't work with Mozilla 1.4 or
earlier meaning it doesn't work with Netscape 7.0 or 7.1. I don't know
what is causing this, it seems the layer after being moved with DOM
scripting into the container is still being positioned relative to the
body which seems to be a major flaw with the CSS implementation in those
Mozilla builds. I don't have the time to try to construct a workaround
for this problem so the example below is currently (April 2004) not
really suitable for public web sites with Netscape 7 users but rather
for intranets where you are able to get users to use MSIE or Mozilla
1.5+ or Opera 7:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
          "http://www.w3.org/TR/html4/strict.dtd">
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Positioning a layer on top of an element in a scrollable
container element</title>
<script type="text/javascript">
function getPageCoords (element) {
  var coords = { x: 0, y: 0 };
  while (element) {
    coords.x += element.offsetLeft;
    coords.y += element.offsetTop;
    element = element.offsetParent;
  }
  return coords;
}
function getContainerCoords (element, container) {
  var coords = { x: 0, y: 0 };
  while (element && element != container && contains(container,
element.offsetParent)) {
    coords.x += element.offsetLeft;
    coords.y += element.offsetTop;
    element = element.offsetParent;
  }
  return coords;
}
function contains (container, containee) {
  var contained = false;
  while (containee) {
    if (container == containee) {
      contained = true;
      break;
    }
    containee = containee.parentNode;
  }
  return contained;
}
function moveOntoInContainer (layer, target, container) {
  if (container.appendChild && layer.parentNode != container) {
    container.appendChild(layer);
    container.style.position = 'relative';
  }
  var coords = getContainerCoords (target, container);
  if (typeof container.scrollLeft != 'undefined') {
    layer.style.left = coords.x + 'px';
    layer.style.top = coords.y + 'px';
    layer.style.width = target.offsetWidth + 'px';
    layer.style.height = target.offsetHeight + 'px';
    layer.style.visibility = 'inherit';
  }
}
</script>
<script type="text/javascript">
var rows, cols;
function makeRandomTable () {
  var html = '';
  html += '<table border="1">';
  html += '<tbody>';
  rows = Math.floor(Math.random() * 10) + 10;
  cells = Math.floor(Math.random() * 10) + 3;
  for (var i = 0; i < rows; i++) {
    html += '<tr>';
    for (var j = 0; j < cells; j++) {
      html += '<td id="cellRow' + i + 'Col' + j + '">';
      html += i + ', ' + j + ': Kibology';
      html += '<\/td>';
    }
    html += '<\/tr>';
  }
  html += '<\/tbody>';
  html += '<\/table>';
  document.write(html);
}
function makeGUI () {
  var html = '';
  html += '<form action="" onsubmit="return false;">';
  html += '<p>';
  html += '<label>row index: ';
  html += '<select name="rowIndex">';
  for (var i = 0; i < rows; i++) {
    html += '<option value="' + i + '">' + i + '<\/option>';
  }
  html += '<\/select>';
  html += '<\/label>';
  html += '<label>column index: ';
  html += '<select name="colIndex">';
  for (var j = 0; j < cells; j++) {
    html += '<option value="' + j + '">' + j + '<\/option>';
  }
  html += '<\/select>';
  html += '<\/label>';
  html += '<input type="button" ';
  html += 'onclick="moveLayerOntoCell(this.form);" ';
  html += 'value="move layer onto cell"';
  html += '>';
  html += '<\/p>';
  html += '<\/form>';
  document.write(html);
}
function moveLayerOntoCell (form) {
  var rowIndex = form.rowIndex.selectedIndex;
  var colIndex = form.colIndex.selectedIndex;
  var cell, layer, container;
  if (document.getElementById) {
    cell = document.getElementById('cellRow' + rowIndex + 'Col' + colIndex);
    container = document.getElementById('aContainer');
    layer = document.getElementById('aLayer');
    moveOntoInContainer(layer, cell, container);
  }
}
</script>
<style type="text/css">
#aContainer {
  overflow: auto;
  height: 200px;
}
#aContainer table {
  width: 100%;
}
#aLayer {
  left: 0; top: 0;
  position: absolute;
  z-index: 10;
  visibility:  hidden;
  background-color: lightyellow;
  width: 25px; height: 25px;
  -moz-opacity: 0.67;
  opacity: 0.67;
  filter: alpha(opacity=67);
}
</style>
</head>
<body>
<div id="aContainer">
<script type="text/javascript">
makeRandomTable();
</script>
</div>
<div>
<script type="text/javascript">
makeGUI();
</script>
</div>
<div id="aLayer"></div>
<noscript>
<p>
Example of using script obviously needs script enabled.
</p>
</noscript>
</body>
</html>
http://www.webs4soft.com/Links4.htm
http://hotelsinindia.webs4soft.com/hotels-in-delhi.htm
http://indiatravel.webs4soft.com/Resources.htm
http://indianmovies.webs4soft.com/Kuch-Kuch-Hota-Hai.htm
http://real-estate.webs4soft.com/property-tips.htm
http://foodhealthcaretips.webs4soft.com/Resources.htm
http://www.websitecompanyindia.com/seo-Links.htm
http://www.websitecompanyindia.com/seo-Links.htm
http://indianjewelry.websitecompanyindia.com/dimond-ring.htm
http://www.bestindiaeducation.com/Link-Exchange.htm
http://bestjobconsultant.bestindiaeducation.com/PARTNERS.htm
http://eloctronicandmobilestore.bestindiaeducation.com/mobile-links.htm
http://easyloanservice.bestindiaeducation.com/Home-Insurance-Links.htm
http://www.rajhealthcenter.com/Cosmetic-Surgery.htm
http://creativebusinessgroup.rajhealthcenter.com/Business-Links.htm
http://onlinefreeinternetgames.rajhealthcenter.com/Games-Links.htm
http://top-beauty-tips.rajhealthcenter.com/Beauty-Links.htm
http://www.indiatourpoint.com/Travel-Links.htm
http://four-wheeler-buy-tips.indiatourpoint.com/Auto-Links.htm
http://watch-online-free-cricket-match.indiatourpoint.com/Sports-Links.htm
http://directory.indiatourpoint.com/
http://www.bestlifeindia.com/Resources1.htm
http://directory.bestlifeindia.com/
http://freeorkutscrapandsms.bestlifeindia.com/SMS-Links.htm
http://onlinegiftshop.bestlifeindia.com/Gift-Links.htm
http://www.freemusicpoint.com/Music-Links.htm
http://love-dating.freemusicpoint.com
http://online-art-presentation.freemusicpoint.com/
http://onlinefurnitureshop.freemusicpoint.com/