JS实现div的抖动:缓动式抖动

摘要:
380:420)+'px';//这样可以实现小幅度的抖动效果,但是大幅度的抖动就会显得很生硬            a=!380:420),1000,'strongEaseOut');//如果用缓动的效果来实现较大幅度的抖动,那视觉上就可以看到更舒适了        a=!a;      },30);

代码如下:

<!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>
            vardiv1 =document.querySelector('#div1');
            document.onclick = function() {
                /*
                 * 抖动:
                 * 1. 每次改变一下元素的位置
                 * 按照一个中心点进行偏移,假设中心点left原始是400,那么每次就以left:400为中心做位置的移动
                 * 380 -> 420
                 * */
                //div1.style.left = '380px';
                //div1.style.left = '420px';
                vara = true;
                setInterval(function() {
                    /*
                     * 根据a的值,做不同的设置
                     * */
                    div1.style.left =(a ? 380: 420) + 'px';//这样可以实现小幅度的抖动效果,但是大幅度的抖动就会显得很生硬
            
                    a = !a;
                }, 30);
            }
      /*缓动代码*/            
      vartween ={
          linear:function(t,b,c,d){
              returnc*t/d + b;
},
          easeIn:function(t,b,c,d){
              returnc *( t /= d ) * t + b;
},
          strongEaseIn:function(t,b,c,d){
              returnc *( t /= d ) * t * t * t * t + b;
},
          strongEaseOut:function(t,b,c,d){
              returnc *( ( t =t /d -1) *t *t *t *t +1) +b;
          },
          sineaseIn:function(t,b,c,d){
              returnc *( t /= d ) * t * t + b;    
},
          sineaseOut:function(t,b,c,d){
              returnc *( ( t =t /d -1) *t *t *t +1) +b;
          }
      };
      varAnimate = 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 = +newDate;
          this.startPos = this.dom.getBoundingClientRect()[propertyName];
          this.propertyName =propertyName;
          this.endPos =endPos;
          this.duration =duration;
          this.easing =tween[easing];
          varself = this;
          vartimeId =setInterval(function(){
              if(self.step() === false){
                  clearInterval(timeId);
              }
          },19);
      }
      Animate.prototype.step = function(){
          vart = +newDate;
          if(t>=this.startTime + this.duration){
              this.update(this.endPos);
              return false;
          }
          varpos = 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;
      vardiv =document.getElementById('div');
      varanimate = newAnimate(div);
      setInterval(function() {
animate.start('left',(a ? 380 : 420),1000,'strongEaseOut');//如果用缓动的效果来实现较大幅度的抖动,那视觉上就可以看到更舒适了
        a = !a;
      }, 30);
</script> 
</body>
</html>

免责声明:文章转载自《JS实现div的抖动:缓动式抖动》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇CentOS搭建Git服务器及权限管理第9章 微信登陆与令牌下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

解决IE、firefox浏览器下JS的new Date()的值为Invalid Date、NaN-NaN的问题

当我们需要将一串日期字符串转换为具体的Date格式的时候,往往需要用到new Date("xxxx")方法。 当时在IE浏览器下,会遇到这种问题: new Date('2016-01-01 00:00:00') //却返回这个值Invalid Date,转换失败 但是这个方法却在谷歌浏览器上可以返回正确的结果。 解决方式:最终发现是字符...

基于jQuery实现简单的js模块化

在多人合作完成网页,经常遇到大家的js代码相互影响的问题。现在有许多模块化的前端框架,应该是可以解决这个问题。但本人并非前端开发人员,那些框架都没用过,只对jQuery相对熟悉,就想用jQuery来解决这个问题。 首先相互影响主要有两个方面,一是js代码中定义的function名重复,导致覆盖问题;另一个是js操作页面元素时,大家使用的id、name、cl...

JS按回车键实现登录的方法

按回车实现登录的方法可用js来进行实现,有以下三种方法:   <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">(此代码为浏览器以中文的形式呈现) 方法一: <html> <head> <title>Check S...

JS和JSP的区别

js是javascript的缩写。以下是JSP与baiJS的区别和联系:du 名字:JS:JavaScript JSP:Java Server Pages 执行过程:zhiJSP先翻译,翻译成Servlet执行如: test.jsp 要变成 test_jsp.java 然后dao编译成 test_jsp.class而 test_jsp.java 本身就是一...

全面理解js面向对象

前言 当今 JavaScript 大行其道,各种应用对其依赖日深。web 程序员已逐渐习惯使用各种优秀的 JavaScript 框架快速开发 Web 应用,从而忽略了对原生 JavaScript 的学习和深入理解。所以,经常出现的情况是,很多做了多年 JS 开发的程序员对闭包、函数式编程、原型总是说不清道不明,即使使用了框架,其代码组织也非常糟糕。这都是对...

js设置自动刷新

如何实现刷新当前页面呢?借助js你将无所不能。 1,reload 方法,该方法强迫浏览器刷新当前页面。语法:location.reload([bForceGet])   参数: bForceGet, 可选参数, 默认为 false,从客户端缓存里取当前页。true, 则以 GET 方式,从服务端取最新的页面, 相当于客户端点击 F5("刷新") 2,re...