Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Animate table column to hide/show it

I have a very simple table, i try to animate the first column if i press on it.

Full table

After the click it should animate to the left, so that it is not fully displayed anymore:

animated

Then after another click, it should animate back again

animated back again

I tried to achieve this with jquery, but nothing happens:

var main = function()
{
    $boolean = true;
    
    $(".test").click
    (
       function()
       {
          if ($boolean)
      	  {
             $boolean = false;
             $(".test").animate
             (
                {
                   'left':'-=100px'
                },
                "fast"
             );  
           }
      	   else
      	   {
                $boolean = true;
                $(".test").animate
                (
                    {
                        'left':'+=100px'
                    },
                    "fast"
                );  
           }    
       }
  );
}
$(document).ready(main);
table {
  border: 1px solid black;
}

table td {
  border: 1px solid black;
}

table th {
  border: 1px solid black;
}

.test {
    color: red;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<table>
<tr>
  <th class="test">Filename</th>
  <th>value</th>
</tr>
<tr>
  <td class="test">File1</td>
  <td>Test</td>
</tr>
<tr>
  <td class="test">File1</td>
  <td>Test</td>
</tr>
<tr>
  <td class="test">File1</td>
  <td>Test</td>
</tr>
</table>

Why is it not animating and how can i solve it? Thank you

like image 469
Black Avatar asked Jun 23 '26 19:06

Black


1 Answers

Here is a working solution:

var main = function()
{
    $boolean = true;
    
    $(".test").click
    (
       function()
       {
          if ($boolean)
      	  {
             $boolean = false;
             $(".test").animate
             (
                {
                   'max-width':'10px'
                },
                "fast"
             );  
           }
      	   else
      	   {
                $boolean = true;
                $(".test").animate
                (
                    {
                        'max-width':'300px'
                    },
                    "fast"
                );  
           }    
       }
  );
}
$(document).ready(main);
table {
  border: 1px solid black;
}

table td {
  border: 1px solid black;
}

table th {
  border: 1px solid black;
}

.test {
    color: red;
    overflow: hidden;
    max-width: 300px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<table>
<tr>
  <th class="test">Filename</th>
  <th>value</th>
</tr>
<tr>
  <td class="test">File1</td>
  <td>Test</td>
</tr>
<tr>
  <td class="test">File1</td>
  <td>Test</td>
</tr>
<tr>
  <td class="test">File1</td>
  <td>Test</td>
</tr>
</table>
https://jsfiddle.net/zcb0a9tz/
like image 85
Florian Lemaitre Avatar answered Jun 25 '26 10:06

Florian Lemaitre



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!