资讯专栏INFORMATION COLUMN

css3效果: animate实现点点点loading动画效果(一)

lijy91 / 987人阅读

摘要:规定动画完成一个周期所花费的秒或毫秒。等同于贝塞尔曲线平滑过渡。等同于贝塞尔曲线由慢到快再到慢。等同于贝塞尔曲线等同于等同于接受两个参数的步进函数。特定的贝塞尔曲线类型,个数值需在区间内规定动画何时开始。

实现如图所示的点点点loading效果:

一:CSS3 animation实现代码

html代码:

</>复制代码

  1. 提交订单中...

css代码:

</>复制代码

  1. .ani_dot {
  2. font-family: simsun;
  3. }
  4. :root .ani_dot { /* 这里使用Hack是因为IE6~IE8浏览器下, vertical-align解析不规范,值为bottom或其他会改变按钮的实际高度*/
  5. display: inline-block;
  6. width: 1.5em;
  7. vertical-align: bottom;
  8. overflow: hidden;
  9. }
  10. @-webkit-keyframes dot {
  11. 0% { width: 0; margin-right: 1.5em; }
  12. 33% { width: .5em; margin-right: 1em; }
  13. 66% { width: 1em; margin-right: .5em; }
  14. 100% { width: 1.5em; margin-right: 0;}
  15. }
  16. .ani_dot {
  17. -webkit-animation: dot 3s infinite step-start;
  18. }
  19. @keyframes dot {
  20. 0% { width: 0; margin-right: 1.5em; }
  21. 33% { width: .5em; margin-right: 1em; }
  22. 66% { width: 1em; margin-right: .5em; }
  23. 100% { width: 1.5em; margin-right: 0;}
  24. }
  25. .ani_dot {
  26. animation: dot 3s infinite step-start;
  27. }

出现的就是如图所示的结果。
注意点:

</>复制代码

  1. 1.IE10+以及其他浏览器,点点点动画消失,IE6-IE9是普通的点点点文字。
    2.animate动画是连续的,但是我们这儿是一帧一帧的,一卡一卡的,不是那么连续的效果,用到step-start
    3.上面代码还用到了css3的选择器:root:rootIE9+以及其他现代浏览器Hack, IE6-7甚至IE8下,inline-block水平元素的vertical-align:bottom解析与inline水平是有差异的,会导致高度撑开,因此,display: inline-blockhack处理。

二:动画(animation)的参数详解

由于上面用到了animation动画,这里详细介绍下这个animation的参数。

简介

CSS动画(Animations)简单说就是在一段固定的动画时间内暗中在某一频率内改变其CSS某个或某些值,从而达到视觉上的转换动画效果。Animations的很多方面都是可以控制的,包括动画运行时间,开始值和结束值,还有动画的暂停和延迟其开始时间等。

语法

= ||

<" animation-name ">:检索或设置对象所应用的动画名称
<" animation-duration ">:检索或设置对象动画的持续时间
<" animation-timing-function ">:检索或设置对象动画的过渡类型
<" animation-delay ">:检索或设置对象动画延迟的时间
<" animation-iteration-count ">:检索或设置对象动画的循环次数
<" animation-direction ">:检索或设置对象动画在循环中是否反向运动
<" animation-fill-mode ">:检索或设置对象动画时间之外的状态
<" animation-play-state ">:检索或设置对象动画的状态。w3c正考虑是否将该属性移除,因为动画的状态可以通过其它的方式实现,比如重设样式

animation

所有动画属性的简写属性,除了 animation-play-state 属性。

animation-name

规定 @keyframes 动画的名称。就是@keyframes后面跟着的动画名称,本demo本文中名为dot,意思为“点”。

animation-duration

规定动画完成一个周期所花费的秒或毫秒。默认是 0。

animation-timing-function

规定动画的速度曲线。默认是 "ease"。

常见的动画速度参数:

linear:线性过渡。等同于贝塞尔曲线(0.0, 0.0, 1.0, 1.0)

ease:平滑过渡。等同于贝塞尔曲线(0.25, 0.1, 0.25, 1.0)

ease-in:由慢到快。等同于贝塞尔曲线(0.42, 0, 1.0, 1.0)

ease-out:由快到慢。等同于贝塞尔曲线(0, 0, 0.58, 1.0)

ease-in-out:由慢到快再到慢。等同于贝塞尔曲线(0.42, 0, 0.58, 1.0)

step-start:等同于 steps(1, start)

step-end:等同于 steps(1, end)

steps([, [ start | end ]
]?):接受两个参数的步进函数。第一个参数必须为正整数,指定函数的步数。第二个参数取值可以是start或end,指定每一步的值发生变化的时间点。第二个参数是可选的,默认值为end。

cubic-bezier(, , ,
):特定的贝塞尔曲线类型,4个数值需在[0, 1]区间内

animation-delay

规定动画何时开始。默认是 0。也即是指动画延时执行时间。

animation-iteration-count

规定动画被播放的次数。默认是 1。当然,我们可以设置2次,3次,依次递推。还有个无线循环关键字infinite,也即是反复循环播放动画。

animation-direction

规定动画是否在下一周期逆向地播放。默认是 "normal"。当然还有下列值:

reverse:反方向运行

alternate:动画先正常运行再反方向运行,并持续交替运行

alternate-reverse:动画先反运行再正方向运行,并持续交替运行

animation-fill-mode

规定对象动画时间之外的状态。

none:默认值。不设置对象动画之外的状态

forwards:设置对象状态为动画结束时的状态

backwards:设置对象状态为动画开始时的状态

both:设置对象状态为动画结束或开始的状态,动画开始之前是"from"或"0%"关键帧;动画完成之后是"to"或"100%"关键帧状态。

animation-play-state

规定动画是否正在运行或暂停。默认是 "running"。还有个值paused:暂停。

三:animation动画实例 实例一使用from to

</>复制代码

  1. div{
  2. width:100px;
  3. height:100px;
  4. background:red;
  5. position:relative;
  6. animation:mymove 5s infinite;
  7. -moz-animation:mymove 5s infinite; /*Firefox*/
  8. -webkit-animation:mymove 5s infinite; /*Safari and Chrome*/
  9. }
  10. @keyframes mymove{
  11. from {left:0px;}
  12. to {left:200px;}
  13. }
  14. @-moz-keyframes mymove { /*Firefox*/
  15. from {left:0px;}
  16. to {left:200px;}
  17. }
  18. @-webkit-keyframes mymove{ /*Safari and Chrome*/
  19. from {left:0px;}
  20. to {left:200px;}
  21. }
实例二使用百分比:

</>复制代码

  1. @keyframes myfirst{
  2. 0% {background: red; left:0px; top:0px;}
  3. 25% {background: yellow; left:200px; top:0px;}
  4. 50% {background: blue; left:200px; top:200px;}
  5. 75% {background: green; left:0px; top:200px;}
  6. 100% {background: red; left:0px; top:0px;}
  7. }
  8. @-moz-keyframes myfirst{ /* Firefox */
  9. 0% {background: red; left:0px; top:0px;}
  10. 25% {background: yellow; left:200px; top:0px;}
  11. 50% {background: blue; left:200px; top:200px;}
  12. 75% {background: green; left:0px; top:200px;}
  13. 100% {background: red; left:0px; top:0px;}
  14. }
  15. @-webkit-keyframes myfirst{ /* Safari 和 Chrome */
  16. 0% {background: red; left:0px; top:0px;}
  17. 25% {background: yellow; left:200px; top:0px;}
  18. 50% {background: blue; left:200px; top:200px;}
  19. 75% {background: green; left:0px; top:200px;}
  20. 100% {background: red; left:0px; top:0px;}
  21. }
  22. @-o-keyframes myfirst {/* Opera */
  23. 0% {background: red; left:0px; top:0px;}
  24. 25% {background: yellow; left:200px; top:0px;}
  25. 50% {background: blue; left:200px; top:200px;}
  26. 75% {background: green; left:0px; top:200px;}
  27. 100% {background: red; left:0px; top:0px;}
  28. }
实例三,利用js+Transform和Animation实现3D动画

示例地址:https://webkit.org/blog-files...
只有webkit内核的浏览器才能看到相关3D动画效果。
实现效果如图所示:

css代码:

</>复制代码

  1. body {
  2. font-family: "Lucida Grande", Verdana, Arial;
  3. font-size: 12px;
  4. }
  5. #stage {
  6. margin: 150px auto;
  7. width: 600px;
  8. height: 400px;
  9. -webkit-perspective: 800;
  10. }
  11. #rotate {
  12. margin: 0 auto;
  13. width: 600px;
  14. height: 400px;
  15. -webkit-transform-style: preserve-3d;
  16. -webkit-animation-name: x-spin;
  17. -webkit-animation-duration: 7s;
  18. -webkit-animation-iteration-count: infinite;
  19. -webkit-animation-timing-function: linear;
  20. }
  21. .ring {
  22. margin: 0 auto;
  23. height: 110px;
  24. width: 600px;
  25. -webkit-transform-style: preserve-3d;
  26. -webkit-animation-iteration-count: infinite;
  27. -webkit-animation-timing-function: linear;
  28. }
  29. .ring > :nth-child(odd) {
  30. background-color: #995C7F;
  31. }
  32. .ring > :nth-child(even) {
  33. background-color: #835A99;
  34. }
  35. .poster {
  36. position: absolute;
  37. left: 250px;
  38. width: 100px;
  39. height: 100px;
  40. opacity: 0.7;
  41. color: rgba(0,0,0,0.9);
  42. -webkit-border-radius: 10px;
  43. }
  44. .poster > p {
  45. font-family: "Georgia", serif;
  46. font-size: 36px;
  47. font-weight: bold;
  48. text-align: center;
  49. margin-top: 28px;
  50. }
  51. #ring-1 {
  52. -webkit-animation-name: y-spin;
  53. -webkit-animation-duration: 5s;
  54. }
  55. #ring-2 {
  56. -webkit-animation-name: back-y-spin;
  57. -webkit-animation-duration: 4s;
  58. }
  59. #ring-3 {
  60. -webkit-animation-name: y-spin;
  61. -webkit-animation-duration: 3s;
  62. }
  63. @-webkit-keyframes x-spin {
  64. 0% { -webkit-transform: rotateX(0deg); }
  65. 50% { -webkit-transform: rotateX(180deg); }
  66. 100% { -webkit-transform: rotateX(360deg); }
  67. }
  68. @-webkit-keyframes y-spin {
  69. 0% { -webkit-transform: rotateY(0deg); }
  70. 50% { -webkit-transform: rotateY(180deg); }
  71. 100% { -webkit-transform: rotateY(360deg); }
  72. }
  73. @-webkit-keyframes back-y-spin {
  74. 0% { -webkit-transform: rotateY(360deg); }
  75. 50% { -webkit-transform: rotateY(180deg); }
  76. 100% { -webkit-transform: rotateY(0deg); }
  77. }

html代码:

</>复制代码

js代码:

</>复制代码

  1. const POSTERS_PER_ROW = 12;
  2. const RING_RADIUS = 200;
  3. function setup_posters (row){
  4. var posterAngle = 360 / POSTERS_PER_ROW;
  5. for (var i = 0; i < POSTERS_PER_ROW; i ++) {
  6. var poster = document.createElement("div");
  7. poster.className = "poster";
  8. var transform = "rotateY(" + (posterAngle * i) + "deg) translateZ(" + RING_RADIUS + "px)";
  9. poster.style.webkitTransform = transform;
  10. var content = poster.appendChild(document.createElement("p"));
  11. content.textContent = i;
  12. row.appendChild(poster);
  13. }
  14. }
  15. function init (){
  16. setup_posters(document.getElementById("ring-1"));
  17. setup_posters(document.getElementById("ring-2"));
  18. setup_posters(document.getElementById("ring-3"));
  19. }
  20. window.addEventListener("load", init, false);

参考地址:
CSS参考手册:animation
小tip: CSS3 animation渐进实现点点点等待提示效果

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/111949.html

相关文章

  • 「前端早读君006」移动开发必备:那些玩转H5的小技巧

    摘要:今日励志语录有志者自有千计万计,无志者只感千难万难。三动画技术越来越不陌生,使用门槛也渐渐降低,而且动画还可以使用控制。扫一扫查看效果打开微扫一扫关注早读君,每天早晨为你推送前端知识,度过挤地铁坐公交的时光 今日励志语录有志者自有千计万计,无志者只感千难万难。 文章原出处:腾讯ISUX 开始阅读之前你可以先扫一扫体验demoshowImg(https://segmentfault.co...

    LittleLiByte 评论0 收藏0
  • 「前端早读君006」移动开发必备:那些玩转H5的小技巧

    摘要:今日励志语录有志者自有千计万计,无志者只感千难万难。三动画技术越来越不陌生,使用门槛也渐渐降低,而且动画还可以使用控制。扫一扫查看效果打开微扫一扫关注早读君,每天早晨为你推送前端知识,度过挤地铁坐公交的时光 今日励志语录有志者自有千计万计,无志者只感千难万难。 文章原出处:腾讯ISUX 开始阅读之前你可以先扫一扫体验demoshowImg(https://segmentfault.co...

    liuyix 评论0 收藏0
  • SegmentFault 技术周刊 Vol.38 - 神奇的 CSS

    摘要:层叠即表示允许以多种方式来描述样式,一个元素可以被渲染呈现出多种样式。可以让属性的变化过程持续一段时间,而不是立即生效。比如,将元素的颜色从白色改为黑色,通常这个改变是立即生效的,使用后,将按一个曲线速率变化。 showImg(https://segmentfault.com/img/bVZwyL?w=900&h=385); CSS 的全称是 Cascading Style Sheet...

    elliott_hu 评论0 收藏0
  • css3动画属性详解之transform、transition、animation

    摘要:动画属性详解关于制作动画的几个属性变形转换和动画。一属性旋转中心为原点扭曲倾斜缩放移动矩阵变形。各个属性的用法旋转其中表示度。承载动画的另一个属性。定义动画的名称。一个或多个合法的样式属性。 css3动画属性详解: 关于CSS3制作动画的几个属性:变形(transform)、转换(transition)和动画(animation)。 一、transform 属性: 旋转rotate(中...

    Riddler 评论0 收藏0

发表评论

0条评论

lijy91

|高级讲师

TA的文章

阅读更多
最新活动
阅读需要支付1元查看
<