什么是在值更改时播放转场动画/进行动画处理的最简单方法?

huangapple go评论125阅读模式
英文:

What is the easiest way to animate / play a transistion for some seconds when a value changed?

问题

在我的Blazor Server应用程序中,我有一个标签,我想在值更改后的几秒钟内播放动画(也许是闪烁)。值的更改将作为异步UI事件处理程序的结果触发。目标是引起用户对更改后的值的注意。

简单的代码:

<div>
  <span id="labelThatShouldBeBlinkingOnValueChange" class="">@myValue</span>
</div>

我该如何实现这个目标?也许通过在Blazor组件中使用C#计时器添加一个高亮类?或者我是否需要编写JavaScript/TypeScript?是否可以只使用纯CSS来实现这个目标?

我正在寻找一个简单的解决方案(尽量少的代码),以保持我的UI代码和组件逻辑代码的清晰。

英文:

In my Blazor Server application I have label and I want to play an animation (maybe blinking) for some seconds after the value changed. The value change will be triggered as a consequence of an async UI Event Handler. The goal is to sets the users attention to the changed value.

Simple code:

&lt;div&gt;
  &lt;span id=&quot;labelThatShouldBeBlinkingOnValueChange&quot; class=&quot;&quot;&gt;@myValue&lt;/span&gt;
&lt;/div&gt;

How can I achieve this goal? Maybe adding a highlighting class by a C# timer in the Blazor component? Or do I have to write JavaScript/TypeScript? Is is this possible with pure CSS?

I am looking for a simple solution (as less as possible code) in order to keep my UI Code and Component logic code clean.

答案1

得分: 1

string blinkClass = "";

async Task UpdateValue(string value)
{
   blinkClass = "blink";
   myValue = value;
   await task.Delay(2_000);  // 2 sec
   blinkClass = "";
}

and then in the markup

<div>
  <span id="labelThatShouldBeBlinkingOnValueChange" class="@blinkClass">@myValue</span>
</div>

See here for the .blink CSS. You can omit the --webkit stuff now.


<details>
<summary>英文:</summary>


```c#
string blinkClass = &quot;&quot;;

async Task UpdateValue(string value)
{
   blinkClass = &quot;blink&quot;;
   myValue = value;
   await task.Delay(2_000);  // 2 sec
   blinkClass = &quot;&quot;;  
}

and then in the markup

&lt;div&gt;
  &lt;span id=&quot;labelThatShouldBeBlinkingOnValueChange&quot; class=&quot;@blinkClass&quot;&gt;@myValue&lt;/span&gt;
&lt;/div&gt;

See here for the .blink CSS. You can omit the --webkit stuff now.

huangapple
  • 本文由 发表于 2023年3月7日 14:05:18
  • 转载请务必保留本文链接:https://go.coder-hub.com/75658516.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定