当前位置: 移动技术网 > IT编程>开发语言>JavaScript > jquery实现烟花效果(面向对象)

jquery实现烟花效果(面向对象)

2020年05月10日  | 移动技术网IT编程  | 我要评论

本文实例为大家分享了jquery实现烟花效果的具体代码,供大家参考,具体内容如下

<!doctype>
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>烟花效果(面向对象)</title>
 <style type="text/css">
  *{padding: 0;margin: 0}
  body{overflow: hidden;width: 100%;height: 100%;background: #000; }
  div{position: absolute;background: #000;color: #fff}
 </style>
 <script src="jquery-1.8.3.min.js"></script></script>


</head>

<body>
<script type="text/javascript">
 var firworks = {
  init : function(){ //初始化
   var _that = this;
    $(document).bind("click",function(e){
    _that.eventleft = e.pagex;
    _that.eventtop = e.pagey;
    _that.createcylinder();
    });
  },
  createcylinder : function(event){ //创建一个花筒
   var _that = this;
   this.cheight = document.documentelement.clientheight;//浏览器高度
   this.cylinder = $("<div/>");
   $("body").append(this.cylinder);
   this.cylinder.css({"width":4,"height":15,"background-color":"red","top":this.cheight,"left":this.eventleft});
   this.cylinder.animate({top:this.eventtop},600,function(){
    $(this).remove();
    _that.createflower();
   })
  },
  createflower : function(){ //创建很多很多的烟花哇!!
   /*烟花效果
   *1.烟花是很多个div构成
   *2.每个烟花的颜色不一样
   *3.烟花的位置也不一样
   *4.烟花散开方向不一样
   *5.烟花有下坠感觉
   */
   //通过循环可以创建你想要的烟花啦!!!
   var _that = this;
   for(var i = 0 ; i < 30; i++ ){
    $("body").append($("<div class='flower'></div>"));
   };
   $(".flower").css({"width":3,"height":3,"top":this.eventtop,"left":this.eventleft});
   $(".flower").each(function(index, element) {
    var $this = $(this);
    var yhx = math.random()*400-200;
    var yhy = math.random()*600-300;
    _that.changecolor();
    $this.css({"background-color":"#"+_that.randomcolor,"width":3,"height":3}).animate({"top":_that.eventtop-yhy,"left":_that.eventleft-yhx},500);//散开
    for(var i=0;i<30;i++){
     //判断鼠标点击时的右边烟花还是左边烟花
     if(yhx<0){
      _that.downpw($this,"+");//右下坠
     }else{
      _that.downpw($this,"-");//左下坠
     }
    }
   });     
  },
  changecolor : function(){
   /*烟花的颜色是随机的,而且是用16进制表示色值,所以用随机数结合16进制;
   *16进制的最大值ffffff,转换成十进制16777215;
   *math.random()*16777215公式可以得到0-16777215之间的数,因为是小数,所以要用到取整;
   *math.ceil(math.random()*16777215)生成一个在颜色值范围内的,随机的十进制值;
   *math.random()*9+1公式可以得到1-10之间的数,以此类推
   *.tostring(16)方法,是把得到的十进制,转换成16进制,也就是随机的颜色值了;
   */    
   this.randomcolor = "";
   this.randomcolor = math.ceil(math.random()*16777215).tostring(16)//;
   //当这个产生的随机的颜色值,不足6位数的进候,需要补齐,又不改变其值,所以要在这个数的前面加零;
   while(this.randomcolor.length<6){
    this.randomcolor = "0"+this.randomcolor;
   }
  },
  downpw : function(ele,type){ //烟花下坠啦 !!!!
   ele.animate({"top":"+=30","left":type+"=4"},50,function(){
     settimeout(function(){ele.remove()},2000);
   })
  }
 };
 firworks.init();
</script>

</body>
</html>

更多javascript精彩特效分享给大家:

jquery幻灯片特效汇总

jquery焦点图特效汇总

jquery级联菜单特效汇总

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持移动技术网。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网