0 Comments

jQuery 烟花效果(运动相关)(1)

发布于:2013-07-10  |   作者:广州网站建设  |   已聚集:人围观

效果图


  1. $(function(){  
  2.     $(document).click(function(event){  
  3.         /*1.创建DIV并插入到body当中
  4.         *2.设置其初始位置:TOP为屏幕的高度,left为鼠标点击时,鼠标的pageX值;  
  5.         */ 
  6.         //创建DIV  
  7.         var $div = $("<div/>");  
  8.         var eLeft = event.pageX;  
  9.         var etop = event.pageY;  
  10.         var cHeight = document.documentElement.clientHeight;  
  11.         //设定颜色、大小,和其初始化的位置  
  12.         $div.css({"width":4,"height":15,"background-color":"red","top":cHeight,"left":eLeft});  
  13.         //插入到页面的body当中去;  
  14.         $("body").append($div);  
  15.         //不要出现滚动条  
  16.         $("body").css("overflow","hidden");  
  17.         //让DIV向上移动,并且移动到鼠标位置后,删除这个DIV元素,之后执行烟花效果;  
  18.         $div.animate({"top":etop},700,function(){  
  19.             //移除DIV  
  20.             $(this).remove();  
  21.             /*烟花效果
  22.             *1.烟花是很多个DIV构成  
  23.             *2.每个烟花的颜色不一样  
  24.             *3.烟花的位置也不一样  
  25.             *4.烟花散开方向不一样  
  26.             *5.烟花有下坠感觉  
  27.             */ 
  28.             //用循环造建很多个DIV,来表示烟花  
  29.             for(i=0;i<20;i++){  
  30.                 $("body").append($("<div class='yh'></div>"));  
  31.             }  
  32.             /*烟花的颜色是随机的,而且是用16进制表示色值,所以用随机数结合16进制;
  33.             *16进制的最大值ffffff,转换成十进制16777215;  
  34.             *Math.random()*16777215公式可以得到0-16777215之间的数,因为是小数,所以要用到取整;  
  35.             *Math.ceil(Math.random()*16777215)生成一个在颜色值范围内的,随机的十进制值;  
  36.             *Math.random()*9+1公式可以得到1-10之间的数,以此类推  
  37.             *.toString(16)方法,是把得到的十进制,转换成16进制,也就是随机的颜色值了;  
  38.             */ 
  39.               
  40.             var sjColor = "" 
  41.             function changColor(){  
  42.                 sjColor = Math.ceil(Math.random()*16777215).toString(16)//;  
  43.                 //当这个产生的随机的颜色值,不足6位数的进候,需要补齐,又不改变其值,所以要在这个数的前面加零;  
  44.                 while(sjColor.length<6){  
  45.                     sjColor = "0"+sjColor;  
  46.                 }  
  47.             }  
  48.               
  49.  
  50.             //设置烟花DIV的颜色和位置、散开,坠落  
  51.             $(".yh").css({"width":3,"height":3,"top":etop,"left":eLeft});  
  52.             /*烟花散开要设左和上
  53.             *Math.random()*20-20这里要减20,是因为烟花是从中心点的左右散开的,  
  54.             *最小随机数时0-10 = -10,最大随机数时20-10=10;所以就是正负10之间  
  55.             */ 
  56.             $(".yh").each(function(index, element) {  
  57.                 var $this = $(this);  
  58.                 changColor()  
  59.                 var yhX = Math.random()*400-200;  
  60.                 var yhY = Math.random()*600-300;  
  61.                 $this.  
  62.                 css({"background-color":"#"+sjColor,"width":3,"height":3}).  
  63.                 animate({"top":etop-yhY,"left":eLeft-yhX},500);//散开  
  64.                 for(i=0;i<30;i++){  
  65.                     //判断鼠标点击时的右边烟花还是左边烟花  
  66.                     if(yhX<0){  
  67.                         downPw($this,"+");//右下坠  
  68.                     }else{  
  69.                         downPw($this,"-");//左下坠  
  70.                     }  
  71.                 }  
  72.                   
  73.                 //下坠效果,即同时改变烟花元素的X和Y,会有抛物线感觉,然后完成动画后,删除这个烟花元素  
  74.                 function downPw(odiv,f){  
  75.                     odiv.animate({"top":"+=30","left":f+"=4"},50,function(){  
  76.                                 setTimeout(function(){odiv.remove()},2000);  
  77.                     })  
  78.                 }  
  79.             });  
  80.         });          
  81.     })  
  82. }) 

先上代码和效果图,具体的功能详解将在第二页中讲到。
广州网站建设,网站建设,广州网页设计,广州网站设计

飞机