简体   繁体   English

淡入/淡出突出显示到斑马条纹表

[英]fade in/out highlight to zebra striped table

I have a table with a number of data rows, which is zebra striped using the following CSS: 我有一个包含许多数据行的表,该表使用以下CSS进行了斑马条纹处理:

#datatable tr:nth-child(odd) {
background-color: #fff;
}

#datatable tr:nth-child(even) {
background-color: #fafafa;
}

The data in the table is continually updated via ajax calls, and I want to add a temporary highlight to particular rows where data has changed. 该表中的数据通过ajax调用不断更新,我想向数据已更改的特定行添加一个临时高亮显示。

I want this new colour (red if the data value has decreased, green if the data value has increased), to nicely fade in, stay for a second or two, then fade back out to the original row colour again. 我希望这种新颜色(如果数据值减少则为红色,如果数据值增加则为绿色)很好地淡入,停留一两秒钟,然后再次淡出为原始行颜色。

I have this working just now using jQuery and CSS; 我现在使用jQuery和CSS进行工作;

$('#row_id').addClass("temphighlight");
setTimeout(clearHighlighting, 3000);

function clearHighlighting(){
$("#row_id").removeClass("temphighlight");
}

#datatable tr.temphighlight {
background-color: #c6efce;
transition: background 1.0s ease;
}

This works for the fade in, but after the delay, the CSS class is removed immediately and there is no fade out. 这适用于淡入,但是在延迟之后,CSS类将立即删除并且没有淡出。

How can I get this highlighting to have a better effect, where it fades in and also fades out? 如何在突出显示和渐隐的同时使突出显示具有更好的效果?

I have checked several similar SO questions, but the answers either don't work or don't apply - such as suggestions that you animate a fade to white, which doesn't work for me because my rows are alternatively coloured differently due to the row striping. 我已经检查了几个类似的SO问题,但是答案不起作用或不适用-例如建议您设置淡入淡出的动画效果,这对我不起作用,因为由于行的不同,我的行的颜色也不同行条纹。

Something like this? 像这样吗

 .table{ border: 1px solid #eee; } .table .row{ padding: 10px; box-sizing: border-box; } .table .row:nth-child(odd) { background-color: #fff; } .table .row:nth-child(even) { background-color: #fafafa; } .table .row.increased{ animation: 1s increased; } .table .row.decreased{ animation: 1s decreased; } @keyframes increased{ 0%{ background: initial; } 50%{ background: #a2ffa9; } 100%{ background: initial; } } @keyframes decreased{ 0%{ background: initial; } 50%{ background: #ff6f6f; } 100%{ background: initial; } } 
 <div class="table"> <div class="row increased"> row </div> <div class="row"> row </div> <div class="row"> row </div> <div class="row decreased"> row </div> </div> 

Just put the transition on a rule that is not removed from the element. 只需将过渡放在未从元素中删除的规则上即可。

 setTimeout(function() { $('#row_id').addClass("temphighlight"); setTimeout(clearHighlighting, 3000); }, 2000); function clearHighlighting() { $("#row_id").removeClass("temphighlight"); } 
 #datatable tr { transition: background 2.0s ease; } #datatable tr:nth-child(odd) { background-color: #fff; } #datatable tr:nth-child(even) { background-color: #fafafa; } #datatable tr.temphighlight { background-color: #c6efce; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <table id="datatable"> <tr> <td>One</td> </tr> <tr> <td>Two</td> </tr> <tr id="row_id"> <td>Three</td> </tr> <tr> <td>Four</td> </tr> </table> 

How's this? 这个怎么样? You can change the class to be added to either pulseUp or pulseDown dependent on if the value is going up or down. 您可以更改要添加到pulseUppulseDown的类, pulseDown取决于该值是向上还是向下。

Try clicking an element in the example provided. 尝试单击提供的示例中的元素。

 $("td").click(function() { var element = this; $(element).addClass("pulseUp"); setTimeout(function () { $(element).removeClass("pulseUp"); },500); }); 
 #datatable tr:nth-child(odd) { background-color: #fff; } #datatable tr:nth-child(even) { background-color: #fafafa; } .pulseUp{ animation-name: pulseUp; animation-duration: 0.5s; animation-timing-function: ease; } .pulseDown { animation-name: pulseDown; animation-duration: 0.5s; animation-timing-function: ease; } @keyframes pulseUp { 50% { background-color: green; } } @keyframes pulseDown { 50% { background-color: red; } } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <table id="datatable"> <tr> <td>1</td> <td>2</td> <td>3</td> </tr> <tr> <td>hello</td> <td>mr</td> <td>Smith</td> </tr> <tr> <td>Goodbye</td> <td>Sir</td> <td>Smith</td> </tr> </table> 

you can change opacity 你可以改变不透明度

myFunction();
var val = 1.0;
function myFunction() {

val -= 0.02;
    $('.myDiv').css("opacity", val);
    setTimeout(myFunction, 20);
}

https://codepen.io/piscu/pen/LOzNqa https://codepen.io/piscu/pen/LOzNqa

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM