CSS 实现打字效果
2014-11-11 12:45
239 查看
原文:CSS 实现打字效果
具体用法可以看看项目地址,带注释的源码200多行,不算复杂
实现方法也不神奇,大多数人肯容易可以想到,用js逐个向容器内添加字符,作者做了很多字符的出来还有速度神马的,我们可以撸一个简单的
JS Bin
其实完整版的animation参数很多,也可以写成分别的属性
animation-name
animation-duration
animation-timing-function
animation-delay
animation-iteration-count
animation-direction
今天我们就要关注一下
ease
linear
ease-in
ease-out
ease-in-out
step-start
step-end
steps
cubic-bezier
每种动画效果都可以对应一种贝塞尔曲线 cubic-bezier可以帮我直观的看一下贝塞尔曲线效果,这里不多说了
steps的语法
看个科学的图片帮助理解
JS Bin
JS Bin
Understanding CSS Timing Functions
JS实现
最近做项目的时候需要实现一个字符逐个出现的打字效果,在网上一搜有个不错的jQuery插件Typed.js,效果很赞<div class="element"></div> <script src="typed.js"></script> <script> $(function(){ $(".element").typed({ strings: ["First sentence.", "Second sentence."], typeSpeed: 0 }); }); </script>
具体用法可以看看项目地址,带注释的源码200多行,不算复杂
实现方法也不神奇,大多数人肯容易可以想到,用js逐个向容器内添加字符,作者做了很多字符的出来还有速度神马的,我们可以撸一个简单的
var s = 'Hello World! Hello World! Hello World!'; var con = $('.container'); var index = 0; var length = s.length; var tId = null; function start(){ con.text(''); tId=setInterval(function(){ con.append(s.charAt(index)); if(index++ === length){ clearInterval(tId); index = 0; start() } },100); } start();
JS Bin
CSS实现
如果对细节和浏览器兼容性要求的不是很严格,我们可以通过CSS3实现animation-timing-function
CSS3的动画都接触过,我们平时这么用animation: animation-name animation-duration animation-iteration-count animation: name 5s infinite;
其实完整版的animation参数很多,也可以写成分别的属性
animation-name
animation-duration
animation-timing-function
animation-delay
animation-iteration-count
animation-direction
今天我们就要关注一下
animation-timing-function,大多数动画在时间轴上时线性变化的,jQuery动画的时候我们用的
liner参数就是这意思,但CSS3提供了一些其它的变化方式由
animation-timing-function属性指定
ease
linear
ease-in
ease-out
ease-in-out
step-start
step-end
steps
cubic-bezier
每种动画效果都可以对应一种贝塞尔曲线 cubic-bezier可以帮我直观的看一下贝塞尔曲线效果,这里不多说了
steps
我们看一下steps的效果,其实顾名思义就可以想到steps什么意思,就像俄罗斯方块的小格子往下掉也是动画,但是不是连续的,更像是逐帧的,steps就是实现这种效果的
steps的语法
steps(number_of_steps, [start|end])
number_of_steps动画分为多少步执行
direction动画显示状态,end:默认值,第一帧开始前显示,start:第一帧结束后显示
看个科学的图片帮助理解
走两步
有了这些我们就能做个好玩儿的效果了JS Bin
.walk { width: 125px; height: 150px; background: url(http://lsly1989.qiniudn.com/xxxasddbgfbwalk.jpg) left; -webkit-animation:anima 1s steps(16) infinite ; } @-webkit-keyframes anima{ from { background-position:2000px 0;} to {background-position:0px 0;} }
打字效果
打字效果也就可想而知了,改变容器宽度即可(只能单行使用,还得做到每步增加长度和字母宽度一致,还是js好其实).typing{ width:250px; white-space:nowrap; overflow:hidden; -webkit-animation: type 3s steps(50, end) infinite; animation: type 3s steps(50, end) infinite; } @-webkit-keyframes type{ from { width: 0;} } @keyframes type{ from { width: 0;} }
JS Bin
参考
MDN: timing-functionUnderstanding CSS Timing Functions
相关文章推荐
- CSS 实现打字效果
- js+css实现打字效果
- CSS 实现打字效果
- CSS实现打字效果
- 用DIV+CSS如何实现这种表格效果
- CSS实现光滑圆角效果
- 用css实现孤形表格效果
- css实现圆角效果
- CSS实现简单的图文互绕排版效果
- Css利用js的expression实现的效果
- 用DIV+CSS如何实现这种表格效果
- 如何在CSS中实现图片交替效果
- 用js实现的打字效果的带链接的新闻标题
- 用CSS实现表格背景颜色渐变效果
- 用CSS实现图片渐显的效果
- 用CSS实现链接的虚线下划线效果
- css实现页面对象的阴影效果,也就是dropshadow
- 用 CSS 实现的阴影效果
- CSS Filter 代替 图片 实现 渐变背景效果。
- CSS实现阴影文字效果