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

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.