I have the following HTML code.
<div id="table1"></div>
<script>
var arr = [
["s1","s2","s3"],
["sk1","sk2","sk3"],
["skl1","skl2"]
];
table_generator("table1", arr);
</script>
Now to add table to this div, I have following javascript code.
<script>
function table_generator(body_table_name, s_array) {
var body_table = document.getElementById(body_table_name);
//Create table section
var table = document.createElement("TABLE");
table.setAttribute("id", "table_"+body_table_name);
//Append to body div
body_table.appendChild(table);
//Add rows
var row_length = s_array.length;
for(var i = 0; i <row_length; i++)
{
row(s_array[i], "row"+i, "table_"+body_table_name);
}
assign_height_width_hover_effects();
}
</script>
To add rows inside the table I have this javascript function
<script>
function row(td_array, row_name, table_name) {
//Create row
var tr = document.createElement("TR");
tr.setAttribute("id", "tr_"+row_name);
document.getElementById(table_name).appendChild(tr);
//Create the tds
for(var i = 0; i<td_array.length; i++)
{
var td = document.createElement("TD");
td.setAttribute("id", "td_"+td_array[i]);
tr.appendChild(td);
//Add td styling
td.style.transition = "all 0.5s ease-in-out";
//Add text to cell
var name = document.createElement("SPAN");
name.setAttribute("id", "name_"+td_array[i]);
name.innerHTML = td_array[i];
td.appendChild(name);
//Add skill info
var info = document.createElement("SPAN");
info.setAttribute("id", "info_"+td_array[i]);
info.innerHTML = "Some info here";
name.appendChild(info);
//Add styling to this info
info.style.position = "absolute";
info.style.width = "inherit";
info.style.height = "inherit";
info.style.overflow = "hidden";
info.style.opacity = 0;
info.style.transition = "all 1s ease-in-out 0.4s";
info.style.transform = "scale(0)";
//Calculate cell width
var width = (td.clientWidth + 1);
if (width > max_width)
{
max_width = width;
}
//Push it in array
main_array.push(td_array[i]);
}
}
</script>
And finally, to give the height and width to table cells and add the hover effects I call the following function
<script>
function assign_height_width_hover_effects() {
//Assigh height and width to all cells in that row
for(var i = 0; i<main_array.length; i++)
{
var td = document.getElementById("td_"+main_array[i]);
var name = document.getElementById("name_"+main_array[i]);
var info = document.getElementById("info_"+main_array[i]);
//Add styling to td
td.style.width = max_width+"px";
td.style.height = max_width+"px";
td.style.textAlign = "center";
td.style.boxShadow = "rgb(0, 0, 0) 0px 0px 0px 0px inset";
//Here is where I need help
$(td).hover(
function(){
$(this).stop().animate({boxShadow: '0 0 '+max_width+'px'}, 'fast');
//$(this).stop().animate({"opacity": 0});
},
function(){
$(this).stop().animate({boxShadow: '0 0 0'}, 'fast');
//$(this).stop().animate({"opacity": 1});
}
);
}
}
</script>
Basically what I want is to animate the following:
When the mouse enters the td table cell:
- The box shadow of the td cell to animate to certain color
- The span element with id "name" which is inside the td cell to fade away, I use opacity 0 to do that.
- And the span element with id "info" which is inside the td cell to appear, I use opacity 1 to do that.
When the mouse leaves the td table cell:
- The box shadow of the td cell to animate back to normal. That is to go away
- The span element with id "name" which is inside the td cell to appear back, I use opacity 1 to do that.
- And the span element with id "info" which is inside the td cell to fade away, I use opacity 0 to do that.
Now I am able to animate the box shadow alone, or the fade in - fade out of the name element but not both together.