最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Animate table column to hideshow it - Stack Overflow

programmeradmin0浏览0评论

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

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

Then after another click, it should animate 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=".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>

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

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

Then after another click, it should animate 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./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

Share Improve this question asked Feb 3, 2016 at 9:29 BlackBlack 20.4k46 gold badges185 silver badges296 bronze badges
Add a ment  | 

5 Answers 5

Reset to default 5

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./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/zcb0a9tz/

If you add on more animating property like opacity, you will see that it works. So animation is working.

$(".test").click
(
   function()
   {
      if ($boolean)
      {
         $boolean = false;
         $(".test").animate
         (
            {  opacity: 0.5,
               'left':'-=100px'
            },
            "fast"
         );  
       }
       else
       {
            $boolean = true;
            $(".test").animate
            (
                {   opacity: 0.25,
                    'left':'+=100px'
                },
                "fast"
            );  
       }    
   }
);

Try this and you will see animation working.

You need to animate width not left property. Try this one :

var main = function(){
  $boolean = true;
  $(".test").click(function(){
    if ($boolean){
      $boolean = false;
      $(".test").animate({'width':'-=50px'},"fast");  
    }else{
      $boolean = true;
     $(".test").animate({'width':'+=50px'},"fast");  
    }    
  }
)}

Additionally you should change css for .test like this:

.test {
  color: red;
  text-overflow: ellipsis;
  white-space: nowrap;
  overflow: hidden;
  display: block;
}

You cannot change left or top for position: static elements. Try making them relative position to preserve their real position.

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;
  position: relative;
}
<script src="https://ajax.googleapis./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>

You have to add a fixe witdth to your table and add this property :

  table-layout: fixed;
  width:200px;

And then you can use this javascript :

var main = function()
{
    $boolean = true;

      $(".test").click(function(){
                if($boolean){
              $boolean = false;
              $( ".test" ).animate({ 
                  width:"20px",
                }, 1500 );
             } else {
                    $boolean = true;
                $( ".test" ).animate({ 
                  width:"100px",
                }, 1500 );
             }
      });
}
$(document).ready(main);

You can see Jsfiddle

发布评论

评论列表(0)

  1. 暂无评论