css关键帧动画_带有关键帧动画CSS计时器

css关键帧动画

This article started in typical fashion for me: that is, as another topic entirely. I plan to write an article on animated CSS3 media query transitions, but I was sidetracked by the question of how to display a changing browser width using alone. Thus, this CSS3 odometer; for the purpose of this article, I’ve turned it into a timer.

对我而言,本文以典型的方式开始:即完全是另一个主题。 我打算写一篇有关CSS3动画查询过渡动画的文章,但我的问题是如何仅使用来显示不断变化的浏览器宽度。 因此,此CSS3里程表; 出于本文的目的,我将其变成了计时器。

Ultimately the technique shown here could be put to a lot of uses. First, an explanation of the basic markup:

最终,此处显示的技术可以被大量使用。 首先,对基本标记进行说明:

The display needs three “wheels” of numbers for each segment of the timer, so a series of HTML lists makes eminent sense. As you will see, all of the lists will move to the beat of the same CSS animation routine. They’re also all wrapped in a <div> with an id of timer. You have been on this page for

显示屏的每个计时器段需要三个“转轮”数字,因此一系列HTML列表非常有意义。 如您所见,所有列表都将移至同一CSS动画例程的节拍中。 它们也都包裹在一个idtimer<div>中。 您一直在此页面上

<div id="timer">
	<ul id="timer-tens">
		<li>8
		<li>9
		<li>0
		<li>1
		<li>2
		<li>3
		<li>4
		<li>5
		<li>6
		<li>7
		<li>8
		<li>9
	</ul>
	<ul id="timer-ones">
		<li>9
		<li>0
		<li>1
		<li>2
		<li>3
		<li>4
		<li>5
		<li>6
		<li>7
		<li>8
		<li>9
		<li>0
	</ul>
</div> seconds

Note that the numerals above and below the digit shown in the center of the wheel at any moment are included.

请注意,任何时候都包括在车轮中心所示数字的上方和下方的数字。

(I would have dearly loved to use a proper ordered list to display the numbers, but the fact that most browsers do not yet support the HTML5 start attribute for ordered lists, and my desire to show a glimpse of the broader counter, made that impractical. I’ve not shown the code for the hundreds-of-seconds counter to save space).

(我非常喜欢使用适当的有序列表来显示数字,但是事实是,大多数浏览器尚不支持有序列表的HTML5 start属性,而且我希望能看到更广泛的计数器,这使这不切实际。为了节省空间,我没有显示数百秒计数器的代码)。

Then the CSS. Obviously, I need to restrict the visible area of each list. That will be the job of the containing div:

然后是CSS。 显然,我需要限制每个列表的可见区域。 那将是包含div的工作:

div#timer {
	display: inline-block;
	height: 3.6rem;
	overflow: hidden;
}

I’ve used inline-block so the <div> will still fall in line with the surrounding text, but still be provided with a height. Let’s look at the CSS for the counters:

我使用了inline-block因此<div>仍将与周围的文本保持一致,但仍具有height 。 让我们看一下计数器CSS:

div#timer ul {
	display: inline-block;
	list-style-type: none;
	width: 2rem;
	padding-left: 0;
	margin-top: 0;
	position: relative;
	top: -1.3rem;
	line-height: 32px;
}

This is pretty simple. The line-height specified here will interact with the general rules for the <body>:

这很简单。 此处指定的line-height将与<body>的一般规则交互:

body {
	background: #000;
	color: #fff;
	text-align: center;
	font-size: 32px;
}

The two values guarantee that every digit in both lists is separated by the same amount of vertical space. It’s this until that we use to move the counters in the animation:

这两个值确保两个列表中的每个数字都由相同数量的垂直空间分隔开。 直到我们用来在动画中移动计数器之前,都是这样:

@keyframes rollover {
	0% { top: -51px; }
	10% { top: -83px; }
	20% { top: -115px; }
	30% { top: -147px; }
	40% { top: -179px; }
	50% { top: -211px; } 
	60% { top: -243px; }
	70% { top: -275px; }
	80% { top: -307px; }
	90% { top: -339px; }
    100% { top: -371px; }
}

The spacing of the numerals means that each animation sequence will start and end on the same digit, and that every numeral will always be displayed in the center of its wheel.

数字的间距意味着每个动画序列都将在同一数字上开始和结束,并且每个数字将始终显示在其转轮的中心。

Now the really neat part: the animation of the tumblers. I wanted the movement between each numeral to be a very deliberate “tick”, so I created a custom easing curve and applied it to every step of the animation:

现在真正整洁的部分是:随行杯的动画。 我希望每个数字之间的移动是一个非常刻意的“刻度”,因此我创建了一个自定义缓动曲线并将其应用于动画的每个步骤:

ul#timer-ones {
	animation: rollover 10s cubic-bezier(1.000, 0.005, 0.995, 0.090) infinite;
	}

The cool part is the fact that we apply the exact same animation sequence to the tens-of-seconds counter, as it moves in exactly the same way, just over a greater amount of time:

最酷的部分是,我们将完全相同的动画序列应用于数十秒计数器,因为它以完全相同的方式移动,只是时间更长:

ul#timer-tens {
	animation: rollover 100s cubic-bezier(1.000, 0.005, 0.995, 0.090) infinite;
	}

That’s essentially it, with the addition of a gradient for the <div> and alpha-masked, absolutely positioned PNG images inside to “shadow” the tumblers.

基本上就是这样,为<div>和内部带有alpha遮罩的,绝对定位的PNG图像添加了渐变,以“盖住”翻转开关。

(For this article I haven’t turned on the animation for the hundreds-of-seconds counter, as I don’t expect anyone to stick around on the page for that long… that said, it will be very interesting to look at my Google Analytics numbers later this week to see if the time-on-page metric has gone up due to visitors being obsessed, Hook-like, with a ticking clock).

(对于这篇文章,我并没有打开数百秒计数器的动画,因为我不希望有人在页面上停留那么长时间……也就是说,看看我的动画会非常有趣Google Analytics(分析)在本周晚些时候进行计数,以查看网页停留时间指标是否由于访问者痴迷(类似于Hook)和滴答作响的时钟而增加了)。

翻译自: https://thenewcode.com/505/CSS-Timer-With-Keyframe-Animation

css关键帧动画

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: CSS关键帧动画可以通过使用@keyframes规则来实现。@keyframes规则定义了动画关键帧,即动画在不同时间点的状态。 关键帧动画有以下属性: 1. animation-name:指定动画的名称。 2. animation-duration:指定动画的持续时间。 3. animation-timing-function:指定动画的时间函数,用于控制动画的速度变化。 4. animation-delay:指定动画的延迟时间。 5. animation-iteration-count:指定动画的循环次数。 6. animation-direction:指定动画的方向,可以是正向、反向或交替。 7. animation-fill-mode:指定动画在执行前和执行后的状态。 8. animation-play-state:指定动画的播放状态,可以是运行或暂停。 以上属性可以通过在元素的样式中使用animation属性来实现关键帧动画。例如: div { animation: myanimation 2s ease-in-out 1s infinite alternate; } 这个例子中,动画名称为myanimation,持续时间为2秒,时间函数为ease-in-out,延迟时间为1秒,循环次数为无限,方向为交替。 ### 回答2: CSS关键帧动画可以通过 @keyframes 规则来实现,其语法如下: ``` @keyframes animation-name { 0% { /* CSS 属性和值 */} 50% { /* CSS 属性和值 */} 100% { /* CSS 属性和值 */} } ``` 在该规则中,animation-name 定义动画的名称,可以在后面的 animation 属性中引用;0%、50%、100% 分别表示动画发生的时间点,可以分别设置 CSS 属性和对应的值,顺序随便;最后,可以在元素中使用 animation 属性将动画应用到指定元素上。 以下是常用的 @keyframes 属性: - animation-name:指定动画名称,需要与元素中的 animation 属性中的动画名称相同才能生效。 - animation-duration:指定动画持续时间,单位为秒或毫秒。 - animation-delay:指定动画延迟时间,单位为秒或毫秒。 - animation-timing-function:指定动画的时间函数,用于控制动画的速度。常见的时间函数有 linear、ease、ease-in、ease-out、ease-in-out 等。 - animation-iteration-count:指定动画的播放次数,可以为数字、infinite 或者关键字 alternate(动画循环次数为基数或偶数时反向播放)。 - animation-direction:指定动画的播放方向,可以为 normal、reverse、alternate 或者 alternate-reverse。 - animation-fill-mode:指定动画在播放前和播放后的状态,可以为 none、forwards、backwards 或者 both。 除了以上属性,@keyframes 还支持其他一些属性,如 animation-play-state 控制动画的播放状态,animation-iteration-delay 控制每次迭代之间的间隔等。总的来说,CSS关键帧动画提供了很多属性,可以灵活地控制元素的动画效果。 ### 回答3: CSS关键帧动画是通过指定不同的动画状态来实现的,也称为 by 动画或逐动画。通过在指定的关键帧(即动画状态)中定义属性值,使用者可以把元素从一个状态逐渐变为另一个状态,从而创造出动画效果。现在让我们来看看如何实现 CSS关键帧动画以及关键帧动画都有哪些属性: 关键帧动画的实现 要实现 CSS 中的关键帧动画,我们需要使用 @keyframes 规则。这通常包括定义一个动画的名称以及一系列用于指定动画状态的关键帧。例如,下面是一个简单的示例: ``` @keyframes my-animation { 0% { opacity: 0; } 50% { opacity: 0.5; } 100% { opacity: 1; } } ``` 在上述示例中,我们定义了一个名为 my-animation 的动画,并指定了三个关键帧:0%、50% 和 100%。每个关键帧都包含 CSS 属性,该属性将元素从前一个状态逐渐转换到下一个状态。在这个例子中,我们将元素的透明度从 0 到 1 进行了渐变。 关键帧动画的属性 除了要创建关键帧,我们还可以使用各种不同的属性和值来指定其他关键帧动画的行为。以下是一些主要的属性: 1. animation-name 指定要使用的动画名称。 2. animation-duration 指定动画的持续时间,以秒或毫秒为单位。 3. animation-timing-function 指定动画执行的节奏或缓动类型。例如,linear、ease、ease-in、ease-out 或 ease-in-out。 4. animation-delay 指定动画的延迟时间,以秒或毫秒为单位,在该时间之后才开始执行动画。 5. animation-iteration-count 指定动画应该执行多少次。例如,使用无限循环可以设置为 infinite。 6. animation-direction 指定元素在动画结束后是否应该反转方向,并反向播放动画。例如,normal 或 alternate。 7. animation-fill-mode 指定动画是否应该在执行前或执行后将其应用于元素的样式。例如,none、forwards、backwards 或 both。 总之,关键帧动画是一种非常有用的技术,可用于为网页中的元素添各种动画效果。理解它的工作原理以及可用的各种属性非常重要,这样你就可以通过使用 CSS 来制作出令人惊叹的动画效果的。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值