JS实现div的抖动:缓动式抖动
代码如下:
<!DOCTYPE html>
<html> <head>
<meta charset="UTF-8">
<title>Title</title>
<style>
#div1 {
width: 100px;
height: 100px;
position: absolute;
left: 400px;
top: 200px;
background: red;
}
</style>
</head> <body>
<div id="div1"></div> <script>
var div1 = document.querySelector('#div1'); document.onclick = function() { /*
* 抖动:
* 1. 每次改变一下元素的位置
* 按照一个中心点进行偏移,假设中心点left原始是400,那么每次就以left:400为中心做位置的移动
* 380 -> 420
* */ // div1.style.left = '380px';
// div1.style.left = '420px'; var a = true; setInterval(function() { /*
* 根据a的值,做不同的设置
* */
div1.style.left = (a ? 380 : 420) + 'px';//这样可以实现小幅度的抖动效果,但是大幅度的抖动就会显得很生硬
a = !a; }, 30); }
/*缓动代码*/
var tween = {
linear:function(t,b,c,d){
return c*t/d + b;
},
easeIn:function(t,b,c,d){
return c * ( t /= d ) * t + b;
},
strongEaseIn:function(t,b,c,d){
return c * ( t /= d ) * t * t * t * t + b;
},
strongEaseOut:function(t,b,c,d){
return c * ( ( t = t / d -1 ) * t * t * t * t +1 ) + b;
},
sineaseIn:function(t,b,c,d){
return c * ( t /= d ) * t * t + b;
},
sineaseOut:function(t,b,c,d){
return c * ( ( t = t / d -1 ) * t * t *t +1 ) + b;
}
}; var Animate = function(dom){
this.dom = dom;
this.startTime = 0;
this.startPos = 0;
this.endPos = 0;
this.propertyName = null;
this.easing = null;
this.duration = null;
} Animate.prototype.start = function(propertyName,endPos,duration,easing){
this.startTime = +new Date;
this.startPos = this.dom.getBoundingClientRect()[propertyName];
this.propertyName = propertyName;
this.endPos = endPos;
this.duration = duration;
this.easing = tween[easing]; var self = this;
var timeId = setInterval(function(){
if(self.step() === false){
clearInterval(timeId);
}
},19);
} Animate.prototype.step = function(){
var t = +new Date;
if(t>=this.startTime + this.duration){
this.update(this.endPos);
return false;
}
var pos = this.easing(t-this.startTime, this.startPos, this.endPos - this.startPos, this.duration);
this.update(pos);
} Animate.prototype.update = function(pos){
this.dom.style[this.propertyName] = pos + 'px';
}
var b = true;
var div = document.getElementById('div');
var animate = new Animate(div);
setInterval(function() {
animate.start('left',(a ? 380 : 420),1000,'strongEaseOut');//如果用缓动的效果来实现较大幅度的抖动,那视觉上就可以看到更舒适了
a = !a;
}, 30);
</script> </body> </html>
JS实现div的抖动:缓动式抖动的更多相关文章
- js多物体多方向缓动动画加带有回调机制
一.获取一组div元素 var boxs = document.getElementsByTagName('div'); 二.封装获取属性值的函数 function getStyle(dom, att ...
- JS特效@缓动框架封装及应用
| 版权声明:本文为博主原创文章,未经博主允许不得转载. 一.变量CSS样式属性获取/赋值方法 给属性赋值:(既能获取又能赋值) 1)div.style.width 单个赋值:点语法,这个方法比较固定 ...
- JS —— 轮播图中的缓动函数的封装
轮播图的根本其实就是缓动函数的封装,如果说轮播图是一辆跑动的汽车,那么缓动函数就是它的发动机,今天本文章就带大家由简入繁,封装属于自己的缓动函数~~ 我们从需求的角度开始,首先给出一个简单需求: 1. ...
- GSAP JS基础教程--使用缓动函数
今天来了解一下缓动easeing函数. 开始,如果你还没有GSAP的类包,可以到GreenSock的官网去下载最新版本的类包,或者直接点击这里来下载 学习之前,先来准备一下: <!DO ...
- tween.js缓动(补间动画)
一.理解tween.js 如果看到上面的已经理解了,可以跳过下面的部分.下面为对Tween.js的解释 下面就介绍如何使用这个Tween了,首先b.c.d三个参数(即初始值,变化量,持续时间)在缓动开 ...
- JS-特效 ~ 04. client对象、网页可视区域的宽高、client / offset / scroll 三大家族的区别、冒泡事件、事件委托、获取内嵌式和外链式属性getStyle(ele,attr) ;、缓动动画封装
知识点: 模拟滚动条的解除事件问题 : event内置对象,包含 了大量事件: page兼容性: pageX || clientX + scool().top : if (true === a)tr ...
- JS实现可用滑块滑动的缓动图
尝试模仿京东的"发现好货"模块的可用滑块滑动的缓动图 JS代码 function $(id) { return document.getElementById(id); } //缓 ...
- 利用tween.js算法生成缓动效果
在讲tween类之前,不得不提的是贝塞尔曲线了.首先,贝塞尔曲线是指依据四个位置任意的点坐标绘制出的一条光滑曲线.它在作图工具或动画中中运用得比较多,例如PS中的钢笔工具,firework中的画笔等等 ...
- animation js控制 缓动效果
<!DOCTYPE html><html><head><meta charset="utf-8" /><title>缓动 ...
随机推荐
- jsp 自定义标签解决jsp页面中int时间戳的时间格式化问题
jsp 自定义标签解决jsp页面中int时间戳的时间格式化问题 之前在项目中根据需求,需要自定义标签,经过查询w3c文档,自己也踩了一些坑,特此记录自定义标签的步骤,下面就以我之前的一个例子中的定义一 ...
- Kotlin从入门到放弃
1.eclipse kotlin安装: http://blog.csdn.net/u014134488/article/details/50684922 2.Android Studio kotlin ...
- VBA - ONE
1. Grouping our code (1) Modules
- Python Learning - Two
1. Built-in Modules and Functions 1) Function def greeting(name): print("Hello,", name) g ...
- EF Core HasQueryFilter 的小坑
这是今天在实际项目中遇到的一个问题,Entity Framework Core 2.2 生成了下面的 SQL 语句,INNER JOIN 部分丑陋的 SQL 语句让人无法忍受. SELECT TOP( ...
- Java中“==”和“equals()”的区别
“==”比较的是变量所指向的对象,当S1在内存中定义以后,再定义s2的时候s2所指向的值是定义s1时候所创建的,而不是又在内存创建了一个“Monday”. “equals”比较的是对象所指向的值,所以 ...
- asp.net 使用rabbitmq事例
本例asp.net 使用rabbitmq需求背景:为了提升用户体验,用户点击下单按钮,后台先做一些简单必要的操作,返回给用户一个友好提示(比如提示处理中,或者订单状态为处理中),然后发通过发消息给队列 ...
- linux cp 拷贝文件或目录
cp 拷贝文件或目录 默认不能拷贝目录 常用来备份: [root@MongoDB ~]# cp a.txt /tmp/ [root@MongoDB ~]# cp /root/a.txt /tmp/ c ...
- linux centos7磁盘格式化挂载之parted
parted /dev/xvde mklabel gpt //划分为gpt分区 mkpart logical //创建逻辑分区 ext4 //开始大小 537G //结束大小 quit blkid l ...
- IP代理网址
http://www.kuaidaili.com/free/ http://www.66ip.cn/ http://www.xicidaili.com/nn/ http://www.ip3366.ne ...