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浏览器兼容教程之事件处理
Jun 09 Javascript
javascript消除window.close()的提示窗口
May 20 Javascript
SublimeText自带格式化代码功能之reindent
Dec 27 Javascript
微信小程序 textarea 组件详解及简单实例
Jan 10 Javascript
node+express+ejs使用模版引擎做的一个示例demo
Sep 18 Javascript
如何让你的JS代码更好看易读
Dec 01 Javascript
jquery实现企业定位式导航效果
Jan 01 jQuery
iview table高度动态设置方法
Mar 14 Javascript
vue+vue-router转场动画的实例代码
Sep 01 Javascript
微信小程序内拖动图片实现移动、放大、旋转的方法
Sep 04 Javascript
详解vue中axios请求的封装
Apr 08 Javascript
使用refresh_token实现无感刷新页面
Apr 26 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
URL Rewrite的设置方法
2007/01/02 PHP
php将图片保存入mysql数据库失败的解决方法
2014/12/27 PHP
PHP输出九九乘法表代码实例
2015/03/27 PHP
PHP SESSION机制的理解与实例
2019/03/22 PHP
PHP进阶学习之Geo的地图定位算法详解
2019/06/19 PHP
javascipt基础内容--需要注意的细节
2013/04/10 Javascript
js控制表单不能输入空格的小例子
2013/11/20 Javascript
nodejs分页类代码分享
2014/06/17 NodeJs
Jquery仿IGoogle实现可拖动窗口示例代码
2014/08/22 Javascript
js实现window.open不被拦截的解决方法汇总
2014/10/30 Javascript
使表格的标题列可左右拉伸jquery插件封装
2014/11/24 Javascript
JS实现超过长度限制后自动跳转下一款文本框的方法
2015/02/23 Javascript
jquery处理页面弹出层查询数据等待操作实例
2015/03/25 Javascript
jQuery插件简单实现方法
2015/07/18 Javascript
深入理解JavaScript中的箭头函数
2015/07/28 Javascript
js实现YouKu的漂亮搜索框效果
2015/08/19 Javascript
JavaScript中的原型prototype完全解析
2016/05/10 Javascript
jQuery监听浏览器窗口大小的变化实例
2017/02/07 Javascript
js变量值传到php过程详解 将php解析成数据
2019/06/26 Javascript
vue 实现v-for循环回来的数据动态绑定id
2019/11/07 Javascript
Vue脚手架编写试卷页面功能
2020/03/17 Javascript
JavaScript/TypeScript 实现并发请求控制的示例代码
2021/01/18 Javascript
浅析Python中元祖、列表和字典的区别
2016/08/17 Python
python下如何查询CS反恐精英的服务器信息
2017/01/17 Python
浅析python3字符串格式化format()函数的简单用法
2018/12/07 Python
python通过TimedRotatingFileHandler按时间切割日志
2019/07/17 Python
Python3中urlencode和urldecode的用法详解
2019/07/23 Python
opencv实现简单人脸识别
2021/02/19 Python
HTML5之SVG 2D入门1—SVG(可缩放矢量图形)概述
2013/01/30 HTML / CSS
租租车:国际租车、美国租车、欧洲租车、特价预订国外租车(中文服务)
2018/03/28 全球购物
网络方面基础面试题
2012/11/16 面试题
电大会计学自我鉴定
2014/02/06 职场文书
毕业班联欢会主持词
2014/03/27 职场文书
借款担保书范文
2014/05/13 职场文书
煤矿安全生产月活动总结
2014/07/05 职场文书
四查四看自我剖析材料
2014/09/19 职场文书