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参数很多,也可以写成分别的属性

  1. animation-name
  2. animation-duration
  3. animation-timing-function
  4. animation-delay
  5. animation-iteration-count
  6. animation-direction

今天我们就要关注一下animation-timing-function,大多数动画在时间轴上时线性变化的,jQuery动画的时候我们用的liner参数就是这意思,但CSS3提供了一些其它的变化方式由animation-timing-function属性指定

  1. ease
  2. linear
  3. ease-in
  4. ease-out
  5. ease-in-out
  6. step-start
  7. step-end
  8. steps
  9. cubic-bezier

每种动画效果都可以对应一种贝塞尔曲线 cubic-bezier可以帮我直观的看一下贝塞尔曲线效果,这里不多说了

steps

我们看一下steps的效果,其实顾名思义就可以想到steps什么意思,就像俄罗斯方块的小格子往下掉也是动画,但是不是连续的,更像是逐帧的,steps就是实现这种效果的

steps的语法

steps(number_of_steps, [start|end])
  • number_of_steps 动画分为多少步执行
  • direction 动画显示状态,end:默认值,第一帧开始前显示,start:第一帧结束后显示

看个科学的图片帮助理解

![image](http://lsly1989.qiniudn.com/屏幕快照 2014-11-10 17.38.25.png)

走两步

有了这些我们就能做个好玩儿的效果了

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-function

Understanding CSS Timing Functions

最新文章

  1. oracle 学习笔记(三)
  2. Quartz.NET配置
  3. 搭建angularjs API文档站点
  4. LeetCode:Gray Code(格雷码)
  5. 解决mac的日历问题:服务器响应一个错误
  6. C#数据上传方法
  7. jquery 定时器
  8. http://www.cnblogs.com/fczjuever/archive/2013/04/05/3000680.html
  9. awk内置变量 awk有许多内置变量用来设置环境信息,这些变量可以被改变,下面给出了最常用的一些变量。
  10. python密码强口令检测
  11. Gson解析数据
  12. ajax天气查询
  13. 案例学习总结:原生JS实现表格排序
  14. H5与企业微信jssdk集成
  15. iOS开发基础-图片切换(4)之懒加载
  16. Debian Security Advisory(Debian安全报告) DSA-4407-1 xmltooling
  17. Confluence 6 编辑一个站点装饰文件
  18. [转]Angular 4|5 Material Dialog with Example
  19. Alpha冲刺之事后诸葛亮
  20. 2018ACM-ICPC焦作区域赛【反思总结】

热门文章

  1. Angular2入门系列教程7-HTTP(一)-使用Angular2自带的http进行网络请求
  2. 戏说HTML5
  3. 透过WinDBG的视角看String
  4. SQL必备知识点
  5. 启用 Open vSwitch - 每天5分钟玩转 OpenStack(127)
  6. UWP开发之Template10实践二:拍照功能你合理使用了吗?(TempState临时目录问题)
  7. Oracle碎碎念~2
  8. Mac上MySQL忘记root密码且没有权限的处理办法&amp;workbench的一些tips (转)
  9. WebGIS项目中利用mysql控制点库进行千万条数据坐标转换时的分表分区优化方案
  10. nodejs操作arduino入门(javascript操作底层硬件)