A little practice of JS

Source: Internet
Author: User

The goal is to have a div grid run up and color in the page

1  <style>2         * {3 margin:0px;4 padding:0px;5         }6 7 #DIV1{8 position:relative;9 width:50px;Ten Height:50px; One Background-color:Red; A         } -     </style> - </Head> the <Body> -     <formID= "Form1"runat= "Server"> -         <DivID= "DIV1"></Div> -     </form> + </Body> - </HTML> + <Script> A     varC= NewArray ();//defines an array placement color at c[0] = 'Green'; - c[1] = 'Yellow'; - c[2] = 'Black'; - c[3] = 'Navy'; - c[4] = 'Blue'; - c[5] = 'Red'; in     varNum= 0;//sets a variable to record the number of times the color changes and the index of the color in the array -     varNUM1= 0;//set several variables to record the number of times the function was executed to     varDiv=document.getElementById ('DIV1');//Get Div +     functionF () { -         if(NUM1% Ten == 0) {//control the speed of color change the Div.style.backgroundColor=C[num];//Change Color * Num++; $             if(Num==c.length) {Panax Notoginseng Num= 0;//re-record the number of changes in color when the array length reaches zero -             } the         } + Div.style.left=Div.offsetleft+ Ten + 'px';//Div moves 10 pixels across the landscape A         if(Div.offsetleft> 1300) {//move 1300 pixels when line breaks plus transverse position zeroing the Div.style.left= 0 + 'px'; + Div.style.top=Div.offsettop+  - + 'px'; -         } $         if(Div.offsettop>  -) {//The div goes back to the origin when it reaches 500 vertically. $ Div.style.left= 0 + 'px'; - Div.style.top= 0 + 'px'; -         } the NUM1++; -     }Wuyi Window.setinterval ('F ()',  -);//execute function once every 20 milliseconds the </Script>

A little practice of JS

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.