JS section
1' Use strict ';2 functionRedenvelope (options) {3 if( This===window) {4 return Newredenvelope (options);5 }6 varDefaults = {7Imgwidth:60,//the width of the red envelope8Position: ' Absolute ',9IMGENVSRC: '. /images/game/redenv/redenv.png ',TenContainerclass: '. Redenv-contain ' One }; A if(!options) { -Options =defaults; -}Else{ the for(varOptioninchdefaults) { - if(typeofOptions[option] = = =undefined) { -Options[option] =Defaults[option]; - } + } - } + This. options =options; A This. createenv (); at } -REDENVELOPE.PROTOTYPE.CREATEENV =function(){ - varimgenv = document.createelement (' img '); -IMGENV.SRC = This. options.imgenvsrc; -ImgEnv.style.width = This. options.imgwidth+ ' px '; - This. Evnposition (imgenv); in } -RedEnvelope.prototype.evnPosition =function(IMG) { to varContainerwidth = $ ( This. Options.containerclass). width (); + varContainerheight =Window.screen.height; -$( This. Options.containerclass). Prepend (IMG); the //the initial position of the red envelope *Img.style.position = This. options.position; $ varStarttop = parseint (Math.random () *10+ (-10))Panax Notoginseng varStartleft = parseint (Math.random () *containerwidth); - //location of the move the varMoveLeft = parseint (Math.random () *containerwidth+ (-30)); + varT=parseint (Math.random () * 1000+1200); A This. evnanimation ({' Startleft ': startleft, ' starttop ': starttop, ' moveLeft ': moveLeft, ' movetop ': containerheight},t); the } + /*adding elements to the main content*/ -RedEnvelope.prototype.evnAnimation =function(posobject,time) { $$( This. Options.containerclass). Children (' Img:first '). css ({' Left ':p osobject.startleft, ' top ':p osobject.starttop}); $$( This. Options.containerclass). Children (' Img:first '). Animate ( - { -' Left ':p osobject.startleft-Posobject.moveleft, the' Top ':p osobject.movetop - },Wuyi Time , the' Linear ', - function(){ Wu$( This). Remove (); - } About ); $
View Code
HTML section
<div class= "Redenv-contain" ></div><script>setinterval (redenvelope,500);</script>
Recently wrote a red envelope rain small function, but feel their JS there are many places can improve, hope that the gods can help pointing twos