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 学习之旅 (1)
Feb 05 Javascript
Jquery 复选框取值兼容FF和IE8(测试有效)
Oct 29 Javascript
二叉树先序遍历的非递归算法具体实现
Jan 09 Javascript
jquery和js实现对div的隐藏和显示方法
Sep 26 Javascript
使用Node.js为其他程序编写扩展的基本方法
Jun 23 Javascript
在JavaScript应用中实现延迟加载的方法
Jun 25 Javascript
jQuery常用且重要方法汇总
Jul 13 Javascript
promise和co搭配生成器函数方式解决js代码异步流程的比较
May 25 Javascript
详解js加减乘除精确计算
Mar 19 Javascript
jQuery分组选择器简单用法示例
Apr 04 jQuery
vue跳转同一个组件,参数不同,页面接收值只接收一次的解决方法
Nov 05 Javascript
Vue使用鼠标在Canvas上绘制矩形
Dec 24 Vue.js
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防注
2007/01/15 PHP
php运行出现Call to undefined function curl_init()的解决方法
2010/11/02 PHP
php防注入,表单提交值转义的实现详解
2013/06/10 PHP
python进程与线程小结实例分析
2018/11/11 PHP
javascript[js]获取url参数的代码
2007/10/17 Javascript
ejs v9 javascript模板系统
2012/03/21 Javascript
Lazy Load 延迟加载图片的jQuery插件中文使用文档
2012/10/18 Javascript
JavaScript String.replace函数参数实例说明
2013/06/06 Javascript
js Dialog 去掉右上角的X关闭功能
2014/04/23 Javascript
javascript实现Table排序的方法
2015/05/15 Javascript
jquery实现带渐变淡入淡出并向右依次展开的多级菜单效果实例
2015/08/22 Javascript
JQuery实现图片轮播效果
2017/05/08 jQuery
Angularjs自定义指令Directive详解
2017/05/27 Javascript
js实现左右两侧浮动广告
2018/07/09 Javascript
使用异步controller与jQuery实现卷帘式分页
2019/06/18 jQuery
layui中的switch开关实现方法
2019/09/03 Javascript
layer实现登录弹框,登录成功后关闭弹框并调用父窗口的例子
2019/09/11 Javascript
Vue2.0 ES6语法降级ES5的操作
2020/10/30 Javascript
python连接mongodb操作数据示例(mongodb数据库配置类)
2013/12/31 Python
使用Mixin设计模式进行Python编程的方法讲解
2016/06/21 Python
对Python 简单串口收发GUI界面的实例详解
2019/06/12 Python
python实现两个经纬度点之间的距离和方位角的方法
2019/07/05 Python
Python利用Scrapy框架爬取豆瓣电影示例
2020/01/17 Python
python集合能干吗
2020/07/19 Python
canvas学习笔记之绘制简单路径
2019/01/28 HTML / CSS
赫里福德的一家乡村零售商店:Philip Morris & Son
2017/06/25 全球购物
俄罗斯化妆品和香水网上商店:Iledebeaute
2019/01/03 全球购物
马来西亚最大的在线隐形眼镜商店:MrLens
2019/03/27 全球购物
英国名牌男装店:Standout
2021/02/17 全球购物
Exception类的常用方法
2012/06/16 面试题
行政秘书工作自我鉴定
2014/09/15 职场文书
2014年校长工作总结
2014/12/11 职场文书
实习介绍信模板
2015/01/30 职场文书
时尚女魔头观后感
2015/06/04 职场文书
python 用递归实现通用爬虫解析器
2021/04/16 Python
聊聊Python中关于a=[[]]*3的反思
2021/06/02 Python