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.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
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>
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 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
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.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>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.