js定时器+简单的动画效果实例


Posted in Javascript onNovember 10, 2017

1.向下滑动

<!DOCTYPE html>
<html lang="en">
<head>
 <meta charset="UTF-8">
 <title>向下滑动</title>
 <style>
  body {
   margin: 0px;
  }
  #show {
   width: 200px;
   /* 高度为 0 */
   height: 100px;
   background-color: lightcoral;
   margin: 0 auto;
   /* 设置为隐藏 */
   /*display: none;*/
  }

 </style>
</head>
<body>
<div id="show"></div>
<script>
 var show = document.getElementById('show');
 /*show.style.display = 'block';

 var t = setInterval(function(){
  var style = window.getComputedStyle(show,null);
  var height = parseInt(style.height);
  // 判断当前的高度是否为 400
  if (height >= 400){
   clearInterval(t);
  } else {
   height++;
   show.style.height = height + 'px';
  }
 },50);*/

 slideDown(show,400);

 /*
  将上述实现的向下滑动效果,封装在一个固定的函数中
  * 设计当前实现向下滑动效果函数的形参
   * elem - 表示实现向下滑动效果的元素
   * maxHeight - 表示元素向下滑动的最大高度值
  * 函数的逻辑与默认设置CSS样式属性的值无关
  */
 function slideDown(elem, maxHeight){
  // 操作的元素默认的display值为none
  elem.style.display = 'block';
  elem.style.height = '0px';

  var t = setInterval(function(){
   var style = window.getComputedStyle(elem,null);
   var height = parseInt(style.height);
   // 判断当前的高度是否为 400
   if (height >= maxHeight){
    clearInterval(t);
   } else {
    height++;
    elem.style.height = height + 'px';
   }
  },50);
 }


</script>
</body>
</html>

2.移动效果

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>移动效果</title>
  <style>
    body {
      margin: 0px;
    }
    #box {
      width: 100px;
      height: 100px;
      background-color: lightcoral;

      position: absolute;
      left: 100px;
      top: 100px;
    }
  </style>
</head>
<body>
<div id="box"></div>
<script>
  var box = document.getElementById('box');
  box.onclick = function(){
    clearInterval(t);
  }
  /*
    * 向右移动
     * 当前元素移动到页面的最右边时 -> 向左移动
    * 向左移动
     * 当前元素移动到页面的最左边时 -> 向右移动
   */
  var flag = false;// 默认表示向右
  var speed = 1;// 表示每次变化的值
  t = setInterval(function(){
    //speed += 0.01;
    // 获取当前页面的宽度
    var WIDTH = window.innerWidth;
    var style = window.getComputedStyle(box,null);
    var left = parseInt(style.left);
    var width = parseInt(style.width);
    // 判断当前元素移动的方向
    if (flag){// 向左移动
      left -= speed;
    } else {// 向右移动
      left += speed;
    }
    // 判断什么情况下,向左移动;判断什么情况下,向右移动
    if ((left + width) >= WIDTH){// 向左移动
      flag = true;
    } else if (left <= 0){// 向右移动
      flag = false;
    }
    box.style.left = left + 'px';
  },10);

</script>
</body>
</html>

3.事件与动画结合

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>事件与动画结合</title>
  <style>
    body {
      margin: 0px;
    }
  </style>
</head>
<body>
<script>
  // 获取<body>元素
  var body = document.body;
  // 当页面加载完毕后,设置当前<body>元素的高度为当前浏览器窗口的高度
  window.onload = function(){
    setHeight(body);
  };
  // 当用户改变浏览器窗口的大小时,重新设置<body>元素的高度(等于当前窗口的高度)
  window.onresize = function(){
    setHeight(body);
  };
  // 定义函数 - 设置<body>元素的高度等于当前窗口的高度
  function setHeight(elem){
    elem.style.height = window.innerHeight + 'px';
  }

  var width = 100,height = 100;
  // 为<body>元素绑定click事件
  body.onclick = function(event){
    var x = event.clientX;
    var y = event.clientY;
    // 创建<div>元素,显示的位置在鼠标当前的坐标值
    var div = document.createElement('div');
    div.setAttribute('class','circle');
    body.appendChild(div);
    // rgb(0,0,0)格式 -> 颜色随机
    var r = parseInt(Math.random()*255);
    var g = parseInt(Math.random()*255);
    var b = parseInt(Math.random()*255);

    div.style.width = width + 'px';
    div.style.height = height + 'px';
    div.style.backgroundColor = 'rgb('+r+','+g+','+b+')';
    div.style.borderRadius = '50%';
    div.style.opacity = 1;
    div.style.position = 'absolute';
    div.style.left = x - width/2 + 'px';
    div.style.top = y - height/2 + 'px';

    animate(div);
  }
  // 定义函数 -> 实现动画效果
  function animate(elem){
    var style = window.getComputedStyle(elem,null);
    /*var width = parseInt(style.width);
    var height = parseInt(style.height);
    var left = parseInt(style.left);
    var top = parseInt(style.top);
    width++;
    height++;
    elem.style.width = width + 'px';
    elem.style.height = height + 'px';
    elem.style.left = (left - 0.5) + 'px';
    elem.style.top = (top - 0.5) +'px';*/

    var opacity = style.opacity;

    if (opacity <= 0){
      clearTimeout(t);
      // 删除当前元素
    }

    opacity -= 0.01;
    elem.style.opacity = opacity;

    // 设定定时器
    var t = setTimeout(function(){
      animate(elem);
    },50);
  }

</script>
</body>
</html>

以上这篇js定时器+简单的动画效果实例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持三水点靠木。

Javascript 相关文章推荐
JS延迟加载(setTimeout) JS最后加载
Jul 15 Javascript
常用一些Javascript判断函数
Aug 14 Javascript
JavaScript转换农历类实现及调用方法
Jan 27 Javascript
JS 获取滚动条高度示例代码
Oct 24 Javascript
自己动手实现jQuery Callbacks完整功能代码详解
Nov 25 Javascript
一个获取第n个元素节点的js函数
Sep 02 Javascript
javascript实现滑动解锁功能
Dec 31 Javascript
详解js实现线段交点的三种算法
Aug 09 Javascript
jquery css实现邮箱自动补全
Nov 14 Javascript
vue.js,ajax渲染页面的实例
Feb 11 Javascript
vue实现在一个方法执行完后执行另一个方法的示例
Aug 25 Javascript
vue+element UI实现树形表格
Dec 29 Vue.js
浅谈Node.js CVE-2017-14849 漏洞分析(详细步骤)
Nov 10 #Javascript
angular之ng-template模板加载
Nov 09 #Javascript
深入理解Vue 单向数据流的原理
Nov 09 #Javascript
node.js基于express使用websocket的方法
Nov 09 #Javascript
angular2系列之路由转场动画的示例代码
Nov 09 #Javascript
使用ef6创建oracle数据库的实体模型遇到的问题及解决方案
Nov 09 #Javascript
基于vue配置axios的方法步骤
Nov 09 #Javascript
You might like
php面向对象全攻略 (二) 实例化对象 使用对象成员
2009/09/30 PHP
php编写的简单页面跳转功能实现代码
2013/11/27 PHP
php中通过DirectoryIterator删除整个目录的方法
2015/03/13 PHP
用js解决数字不能换行问题
2010/08/10 Javascript
JavaScript导出Excel实例详解
2014/11/25 Javascript
JQuery中绑定事件(bind())和移除事件(unbind())
2015/02/27 Javascript
浅谈javascript中return语句
2015/07/15 Javascript
vue2.0+ 从插件开发到npm发布的示例代码
2018/04/28 Javascript
jQuery选择器之层次选择器用法实例分析
2019/02/19 jQuery
vue 获取url参数、get参数返回数组的操作
2020/11/12 Javascript
element中Steps步骤条和Tabs标签页关联的解决
2020/12/08 Javascript
Python卸载模块的方法汇总
2016/06/07 Python
python实现的AES双向对称加密解密与用法分析
2017/05/02 Python
Python实现中文数字转换为阿拉伯数字的方法示例
2017/05/26 Python
python实现的二叉树定义与遍历算法实例
2017/06/30 Python
Python数据分析之获取双色球历史信息的方法示例
2018/02/03 Python
Tensorflow 利用tf.contrib.learn建立输入函数的方法
2018/02/08 Python
python使用Flask操作mysql实现登录功能
2018/05/14 Python
python计算列表内各元素的个数实例
2018/06/29 Python
Python3.5内置模块之shelve模块、xml模块、configparser模块、hashlib、hmac模块用法分析
2019/04/27 Python
pandas数据筛选和csv操作的实现方法
2019/07/02 Python
tensorflow常用函数API介绍
2020/04/19 Python
python有几个版本
2020/06/17 Python
python装饰器三种装饰模式的简单分析
2020/09/04 Python
matplotlib阶梯图的实现(step())
2021/03/02 Python
html5跨域通讯之postMessage的用法总结
2013/11/07 HTML / CSS
FORZIERI澳大利亚站:全球顶级奢华配饰精品店
2016/12/31 全球购物
Lampegiganten丹麦:欧洲领先的照明网上商店
2018/04/25 全球购物
外企C语言笔试题
2013/11/10 面试题
有针对性的求职自荐信
2013/11/14 职场文书
农民工工资发放承诺书
2014/03/31 职场文书
PostgreSQL将数据加载到buffer cache中操作方法
2021/04/16 PostgreSQL
超详细教你怎么升级Mysql的版本
2021/05/19 MySQL
Python3 类型标注支持操作
2021/06/02 Python
php去除deprecated的实例方法
2021/11/17 PHP
pandas进行数据输入和输出的方法详解
2022/03/23 Python