JavaScript仿flash遮罩动画效果


Posted in Javascript onJune 15, 2016

本文实例为大家分享了JavaScript仿flash遮罩动画的具体实现代码,供大家参考,具体内容如下

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>仿flash遮罩动画</title>
<meta name="keywords" content="">
<meta name="description" content="">
<script charset="utf-8" src="jquery.js"></script>
<style media="screen">
body{margin:0;}
#banner{position:relative;width:858px;height:238px;overflow:hidden;}
</style>
</head>
<body>
<div id="banner">
  <a href="javascript:void(0);"><img src="1.jpg" width="858" height="238"/></a>
</div>
<script type="text/javascript">
function setMaskingAnimation(container,width,height,time,pixel,color){
  var __left=mtRand(parseInt(width*0.25),parseInt(width*0.75));
  var __top=mtRand(parseInt(height*0.25),parseInt(height*0.75));
  if(width>=height){
    var widthSpeed=parseInt((width/height)*10);
    var heightSpeed=10;
    var __width=widthSpeed;
    var __height=heightSpeed;
  }
  else{
    var widthSpeed=10;
    var heightSpeed=parseInt((height/width)*10);
    var __width=widthSpeed;
    var __height=heightSpeed;
  }
  var hander;
  //
  function getPosition(_width,_height,_left,_top){
    var div1={
      "width":_left,
      "height":_top,
      "left":0,
      "top":0
    };
    var div2={
      "width":_width,
      "height":_top,
      "left":_left,
      "top":0
    };
    var div3={
      "width":width-_left-_width,
      "height":_top,
      "left":_left+_width,
      "top":0
    };
    var div4={
      "width":_left,
      "height":_height,
      "left":0,
      "top":_top
    };
    var div5={
      "width":_width,
      "height":_height,
      "left":_left,
      "top":_top
    };
    var div6={
      "width":width-_left-_width,
      "height":_height,
      "left":_left+_width,
      "top":_top
    };
    var div7={
      "width":_left,
      "height":height-_top-_height,
      "left":0,
      "top":_top+_height
    };
    var div8={
      "width":_width,
      "height":height-_top-_height,
      "left":_left,
      "top":_top+_height
    };
    var div9={
      "width":width-_left-_width,
      "height":height-_top-_height,
      "left":_left+_width,
      "top":_top+_height
    };
    return {
      "div1":div1,
      "div2":div2,
      "div3":div3,
      "div4":div4,
      "div5":div5,
      "div6":div6,
      "div7":div7,
      "div8":div8,
      "div9":div9,
    };
  }
  //
  function mtRand(n1,n2){
    return parseInt(Math.random()*(n2-n1+1)+n1);
  }
  //
  function setDiv(i,position){
    var has=$(container).find("div.mask"+i);
    if(has.length){
      has.css("left",position.left);
      has.css("top",position.top);
      has.css("width",position.width);
      has.css("height",position.height);
    }
    else{
      var html='<div class="mask mask{@i}" style="position:absolute;left:{@left}px;top:{@top}px;width:{@width}px;height:{@height}px;background-color:{@backgroundColor};"></div>';
      html=html.replace('{@i}',i);
      html=html.replace('{@left}',position.left);
      html=html.replace('{@top}',position.top);
      html=html.replace('{@width}',position.width);
      html=html.replace('{@height}',position.height);
      if(i==5){
        html=html.replace('{@backgroundColor}',"transparent");
      }
      else{
        html=html.replace('{@backgroundColor}',color);
      }
      $(container).append(html);
    }
  }
  //
  function play(){
    __width+=pixel*widthSpeed;
    __height+=pixel*heightSpeed;
    __left-=pixel*widthSpeed/2;
    __top-=pixel*heightSpeed/2;
    var position=getPosition(__width,__height,__left,__top);
    for(var i=1;i<=9;i++){
      setDiv(i,position["div"+i]);
    }
    if(position.div1.width<=0 && position.div1.height<=0 && position.div9.width<=0 && position.div9.height<=0){
      window.clearInterval(hander);
      $(container).find("div.mask").remove();
    }
  }
  //
  hander=window.setInterval(play,time);
}
 
$(function(){
  setMaskingAnimation("#banner",858,238,100,2,"#ff0000");
  //第4个参数和第5个参数分别设置20和2效果会比较好
  //第5个参数必须是偶数
});
</script>
</body>
</html>

以上就是本文的全部内容,希望对大家学习JavaScript程序设计有所帮助。

Javascript 相关文章推荐
javascript检查日期格式的函数[比较全]
Oct 17 Javascript
Jquery Autocomplete 结合asp.net使用要点
Oct 29 Javascript
javascript 函数及作用域总结介绍
Nov 12 Javascript
javascript分页代码实例分享(js分页)
Dec 13 Javascript
Javascript优化技巧之短路表达式详细介绍
Mar 27 Javascript
JavaScript中的冒泡排序法
Aug 03 Javascript
JavaScript输入框字数实时统计更新
Jun 17 Javascript
基于angular-utils-ui-breadcrumbs使用心得(分享)
Nov 03 Javascript
vue父组件向子组件动态传值的两种方法
Nov 11 Javascript
angularjs 页面自适应高度的方法
Jan 17 Javascript
关于微信小程序获取小程序码并接受buffer流保存为图片的方法
Jun 07 Javascript
vue动态加载SVG文件并修改节点数据的操作代码
Aug 17 Javascript
jquery中的常见问题及快速解决方法小结
Jun 14 #Javascript
使用递归遍历对象获得value值的实现方法
Jun 14 #Javascript
浅谈js里面的InttoStr和StrtoInt
Jun 14 #Javascript
JS遍历数组和对象的区别及递归遍历对象、数组、属性的方法详解
Jun 14 #Javascript
JS递归遍历对象获得Value值方法技巧
Jun 14 #Javascript
全面解析JavaScript中的valueOf与toString方法(推荐)
Jun 14 #Javascript
JavaScript函数中关于valueOf和toString的理解
Jun 14 #Javascript
You might like
PHP 强制性文件下载功能的函数代码(任意文件格式)
2010/05/26 PHP
php文本转图片自动换行的方法
2013/03/13 PHP
如何使用PHP计算上一个月的今天
2013/05/23 PHP
PHP中list方法用法示例
2016/12/01 PHP
运用jquery实现table单双行不同显示并能单行选中
2009/07/25 Javascript
js获取网页高度(详细整理)
2012/12/28 Javascript
jQuery中bind与live的用法及区别小结
2014/01/27 Javascript
NodeJS实现阿里大鱼短信通知发送
2016/01/17 NodeJs
jQuery form插件之ajaxForm()和ajaxSubmit()的可选参数项对象
2016/01/23 Javascript
javascript类型系统_正则表达式RegExp类型详解
2016/06/24 Javascript
jQuery插件EasyUI设置datagrid的checkbox为禁用状态的方法
2016/08/05 Javascript
JS实现简单获取最近7天和最近3天日期的方法
2018/04/18 Javascript
validform表单验证的实现方法
2019/03/08 Javascript
基于Vue的侧边目录组件的实现
2020/02/05 Javascript
Vue发布订阅模式实现过程图解
2020/04/30 Javascript
通过实例解析jQ Ajax操作相关原理
2020/09/23 Javascript
Vue3.0的优化总结
2020/10/16 Javascript
[42:34]VP vs VG 2018国际邀请赛小组赛BO2 第一场 8.19
2018/08/21 DOTA
Python socket网络编程TCP/IP服务器与客户端通信
2017/01/05 Python
教大家玩转Python字符串处理的七种技巧
2017/03/31 Python
Python数据分析之如何利用pandas查询数据示例代码
2017/09/01 Python
Python通过paramiko远程下载Linux服务器上的文件实例
2018/12/27 Python
Django使用redis缓存服务器的实现代码示例
2019/04/28 Python
Python3 pandas 操作列表实例详解
2019/09/23 Python
Python之关于类变量的两种赋值区别详解
2020/03/12 Python
工商管理专业实习大学生自我鉴定
2013/09/19 职场文书
项目专员岗位职责
2013/12/04 职场文书
大学生职业规划论文
2014/01/11 职场文书
事务机电主管工作职责
2014/02/25 职场文书
教师专业自荐信
2014/05/31 职场文书
欢迎领导标语
2014/06/27 职场文书
党员干部廉洁自律承诺书
2015/04/28 职场文书
2015年大学教师工作总结
2015/05/20 职场文书
首都博物馆观后感
2015/06/05 职场文书
年终奖金发放管理制度,中小企业适用,拿去救急吧!
2019/07/12 职场文书
十一月早安语录:把心放轻,人生就是一朵自在的云
2019/11/04 职场文书