javascript - table row become fade after some time not working -


this table row requirement first time displaying row in yellow color after 4 seconds color become fade.how possible .

$('#invoice').prepend('<tr><td>invoice</td><td>analysing</td><td>analysing</td></tr>');

i have 3 solutions you: pure javascript, jquery , css

pure javascript solution:

this creating new tr , appending table innerhtml set td tags. same jquery appending table little different. have done can target specific tr has been create settimeout run.

function demo(){  var table = document.getelementbyid('invoice');  var tr = document.createelement('tr');        table.appendchild(tr);        tr.innerhtml='<td>invoice</td><td>analysing</td><td>analysing</td>';    // opacity change    settimeout(function(){ tr.style.opacity="0.5"}, 4000);    //background        //settimeout(function(){ tr.style.background="rgba(255,255,0,.2)"}, 4000);  }
table tr{background:yellow;opacity:1;}
<button id="create" onclick="demo()">add</button>  <table id="invoice">  <tr><td>invoice</td><td>analysing</td><td>analysing</td></tr>  </table>

update, have commented out background change , replaced opacity fade content well. if want background change remove opacity line , un-comment background line.


jquery solution:

$(document).ready(function () {    $( "#create" ).click(function() {    $('#invoice').prepend('<tr><td>invoice</td><td>analysing</td><td>analysing</td></tr>');    settimeout(function () {  $('tr').css('transition', 'opacity .5s ease-in');  $('tr').css('opacity', '0.5');  }, 4000);  });  });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>  <button id="create">add</button>  <table id="invoice">  </table>

one issue jquery solution above: if create multiple td's timer go first 1 created might find elements change in less 4 seconds


css solution:

i know css isn't tagged in question believe more solutions better.

i have added in support browsers shouldn't have problems, can test them remove ones don't want.

// jquery demo purposes of creating dynamic elements.  $(document).ready(function () {    $( "#create" ).click(function() {    $('#invoice').prepend('<tr><td>invoice</td><td>analysing</td><td>analysing</td></tr>');  });  });
#invoice tr {    background:yellow;       -webkit-animation: opfade 1s; /* safari, chrome , opera > 12.1 */         -moz-animation: opfade 1s; /* firefox < 16 */          -ms-animation: opfade 1s; /* internet explorer */           -o-animation: opfade 1s; /* opera < 12.1 */              animation: opfade 1s;              animation-delay: 4s;              -webkit-animation-fill-mode: forwards;              animation-fill-mode: forwards;  }  @keyframes opfade {      { opacity: 1;}        { opacity: 0.5;}  }    /* firefox < 16 */  @-moz-keyframes opfade {      { opacity: 1; }        { opacity: 0.5; }  }    /* safari, chrome , opera > 12.1 */  @-webkit-keyframes opfade {      { opacity: 1; }        { opacity: 0.5; }  }    /* internet explorer */  @-ms-keyframes opfade {      { opacity: 1; }        { opacity: 0.5; }  }    /* opera < 12.1 */  @-o-keyframes opfade {      { opacity: 1; }        { opacity: 0.5; }  }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>  <button id="create">add</button>  <table id="invoice">  <tr><td>invoice</td><td>analysing</td><td>analysing</td></tr>  </table>


if have questions of source code above please feel free comment below , possible.

i hope helps, happy coding!


Comments

Popular posts from this blog

sql - invalid in the select list because it is not contained in either an aggregate function -

Angularjs unit testing - ng-disabled not working when adding text to textarea -

How to start daemon on android by adb -